javascript - 是否可以在 WordPress 中编辑所有已发送电子邮件的消息正文

标签 javascript php html wordpress

我正在测试是否可以编辑从 wordpress 发送的所有电子邮件的 html 代码。这是我到目前为止想到的:

     add_filter('wp_mail_content_type', function( $content_type ) {
                    return 'text/html';
        });

    add_filter('wp_mail', 'my_wp_mail');

    function my_wp_mail($atts) {
    $atts['message'] .= '  <br/><div id="result">my text line here</div>';
    return $atts;
    } 

上面的代码将在底部添加“我的文本行”行,我正在寻找一种方法,能够使用 JavaScript 将 div 替换为另一个页面的内容:

    <script>
            jQuery(document).ready(function(){
                jQuery('#result').load('https://develop2020.000webhostapp.com/divdiv.html .myclass');
            });

    </script> 

然后是要发送的电子邮件。是否可以使用 JavaScript 来完成此操作,还是必须使用 PHP?如果可能的话,如何从 url 中提取 div 并将其添加到所有电子邮件中?

最佳答案

欢迎来到 Stack Overflow!

首先,在电子邮件中使用 JS 是不可能的。

使用 Javascript 发送或确定电子邮件内容看起来确实很麻烦,因为 Wordpress 通过 PHP 发送电子邮件。 Javascript 在浏览器中运行(当涉及到 Wordpress 时),而不是在服务器上运行,因此如果您尝试使用 JS 来发送电子邮件,则必须在浏览器中打开一个页面才能发送电子邮件。如果在服务器上执行某些操作,例如发送电子邮件,您需要使用 PHP。

至于解决方案,使用curl ( https://www.php.net/manual/en/curl.examples-basic.php ) 或 file_get_contents ( https://www.php.net/manual/en/function.file-get-contents.php ) 将页面放入 .load() 中更有意义,然后使用您用于将内容添加到电子邮件底部的过滤器来制作您在电子邮件的该部分中加载的内容。

如果您只想使用通过curl/file_get_contents 检索到的页面的一部分,您可以使用PHP 中的DOMdocument 选择该部分:

https://www.tutorialspoint.com/php/php_dom_parser_example.htm https://www.php.net/manual/en/class.domdocument.php

关于javascript - 是否可以在 WordPress 中编辑所有已发送电子邮件的消息正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015581/

相关文章:

javascript - Bootstrap 3 : add confirm modal box for jQuery Inline delete row

php - 比较字符串的意外结果

javascript - 加载 Google JS api 时跨源请求被阻止

javascript - JQuery Mobile 列表没有获得正确的样式

php - 将路径数组转换为 UL 列表

php - 从 url 获取真实的视频 URL

html - 带有 Bootstrap 的 Bxslider 旋转木马向左对齐

html - 如何正确对齐标签和跨度

html - 如何在多行 flexbox 布局中指定换行符?

拖动期间释放鼠标时忽略 JavaScript mouseUp 事件