修改WordPress的默认发件人名称

本文转载自WordPress啦!

通过WordPress的后台可以轻松添加一个新的用户。2.8之后的版本,还可以通过邮箱发布信息。这个功能不错,不过却没有可修改寄件人的设置。例如,邮件必须以administrator的名义发送。

不过,可通过两个hooks 来修改寄件人。你可以通过一个小插件轻松地实现修改寄件人的姓名及邮箱地址。

可通过下面的代码获得该插件,不会在数据库中留下任何数据。想要修改寄件人的用户都可以用哦!

<?php
/**
* @package WP Mail From
* @author Frank B&uuml;ltge
* @version 0.1
*/

/*
Plugin Name: WP Mail From
Plugin URI: http://bueltge.de/
Description: Change the default address that WordPress sends it&rsquo;s email from.
Version: 0.1
Author: Frank B&uuml;ltge
Author URI: http://bueltge.de/
Last Change: 11.08.2009 08:41:06
*/

if ( !function_exists(‘add_action’) ) {
header(‘Status: 403 Forbidden’);
header(‘HTTP/1.1 403 Forbidden’);
exit();
}

if ( !class_exists(‘wp_mail_from’) ) {
class wp_mail_from {

function wp_mail_from() {
add_filter( ‘wp_mail_from’, array(&$this, ‘fb_mail_from’) );
add_filter( ‘wp_mail_from_name’, array(&$this, ‘fb_mail_from_name’) );
}

// new name
function fb_mail_from() {
$name = ‘My Blog is my Blog’;
// alternative the name of the blog
// $name = get_option(‘blogname’);
$name = esc_attr($name);
return $name;
}

// new email-adress
function fb_mail_from_name() {
$email = ‘info@example.com’;
$email = is_email($email);
return $email;
}

}

$wp_mail_from = new wp_mail_from();
}
?>

每个相关函数中的姓名和邮箱地址必需保持一致。此后,检查这两个值,这一步不是必需的。

注意:函数esc_attr()是在2.8版本后才出现的,原来的函数是attribute_escape()。如果更早版本的用户想要使用此方案,需先修改这个函数。

这样修改后,用户就不会再看到寄件人是“WordPress”了!

附原文:

Change WordPress Mail Sender

August 19th, 2009 by Frank • WordPress Plugins2 Comments

WordPress makes it easy and fast to add new users in the backend. Since version 2.8 of WordPress, it can send the access information via email. A nice feature, with no additional settings to change the sender of this email. For example the email should be send from the administrator instead from WordPress.

Nevertheless, there is a possibility and with the help of two hooks, the sender can be changed. I created a small Plugin, where you can easy and simple adjust the sender name and sender email.
-
The Plugin is available in the following source code and has no options for the backend and doesn’t leave any data in the database. Anyone who wants can simply extend the Plugin.

<?php
/**
 * @package WP Mail From
 * @author Frank B&uuml;ltge
 * @version 0.1
 */

/*
Plugin Name: WP Mail From
Plugin URI: http://bueltge.de/
Description: Change the default address that WordPress sends it&rsquo;s email from.
Version: 0.1
Author: Frank B&uuml;ltge
Author URI: http://bueltge.de/
Last Change: 11.08.2009 08:41:06
*/

if ( !function_exists('add_action') ) {
	header('Status: 403 Forbidden');
	header('HTTP/1.1 403 Forbidden');
	exit();
}

if ( !class_exists('wp_mail_from') ) {
	class wp_mail_from {

		function wp_mail_from() {
			add_filter( 'wp_mail_from', array(&$this, 'fb_mail_from') );
			add_filter( 'wp_mail_from_name', array(&$this, 'fb_mail_from_name') );
		}

		// new name
		function fb_mail_from_name() {
			$name = 'My Blog is my Blog';
			// alternative the name of the blog
			// $name = get_option('blogname');
			$name = esc_attr($name);
			return $name;
		}

		// new email-adress
		function fb_mail_from() {
			$email = 'info@example.com';
			$email = is_email($email);
			return $email;
		}

	}

	$wp_mail_from = new wp_mail_from();
}
?>

The values for name and email have to be maintain in each of the associated function. After that, the two values are getting examined, but is not necessarily needed.

As a note: the function esc_attr() is only since version 2.8 available and replaces the function attribute_escape(). Should the solution be used in an earlier version, then you have to change the function.

For suggestions and improvements, I am grateful, as always. You can use this Plugin to improve WordPress a bit and the user is not surprised about the sender “WordPress”.

Leave a Reply