首页 问答 正文

如何给WordPress网站添加自定义邮件模板?

注册会员 / 悠然自学 / 2023-06-12/ 浏览 128 次

要给WordPress网站添加自定义邮件模板,可以通过以下步骤:

  1. 在你的主题文件夹中创建一个新的文件夹,例如"custom-email-templates",用于存放自定义邮件模板的相关文件。

  2. 在该文件夹中创建一个HTML文件,例如"custom-template.html",用于定义自定义邮件模板的样式和内容。

  3. 在functions.php文件中添加一个过滤器函数,用于指定使用自定义邮件模板以替换原始邮件模板。例如:

    function custom_email_templates( $args ) {
      $args['message'] = file_get_contents( get_stylesheet_directory() . '/custom-email-templates/custom-template.html' );
      return $args;
    }
    add_filter( 'wp_mail', 'custom_email_templates' ); 

    该代码会将自定义邮件模板文件(custom-template.html)的内容替换原始邮件模板的内容。

  4. 在需要发送邮件的页面,使用wp_mail()函数发送邮件,例如:

    wp_mail( 'user@example.com', 'Subject', 'Message' ); 

    当使用wp_mail()函数发送邮件时,自定义邮件模板将代替默认模板。

示例代码:

function custom_email_templates( $args ) {
  $args['message'] = file_get_contents( get_stylesheet_directory() . '/custom-email-templates/custom-template.html' );
  return $args;
}
add_filter( 'wp_mail', 'custom_email_templates' ); 

返回格式:

要给WordPress网站添加自定义邮件模板,可以通过以下步骤:

  1. 在你的主题文件夹中创建一个新的文件夹,例如"custom-email-templates",用于存放自定义邮件模板的相关文件。
  2. 在该文件夹中创建一个HTML文件,例如"custom-template.html",用于定义自定义邮件模板的样式和内容。
  3. 在functions.php文件中添加一个过滤器函数,用于指定使用自定义邮件模板以替换原始邮件模板。例如:
function custom_email_templates( $args ) {
  $args['message'] = file_get_contents( get_stylesheet_directory() . '/custom-email-templates/custom-template.html' );
  return $args;
}
add_filter( 'wp_mail', 'custom_email_templates' ); 

该代码会将自定义邮件模板文件(custom-template.html)的内容替换原始邮件模板的内容。

  1. 在需要发送邮件的页面,使用wp_mail()函数发送邮件,例如:
wp_mail( 'user@example.com', 'Subject', 'Message' ); 

当使用wp_mail()函数发送邮件时,自定义邮件模板将代替默认模板。

大家谈论
    我的见解