-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-email-as-from-address.php
140 lines (122 loc) · 5.1 KB
/
admin-email-as-from-address.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Plugin Name: Use Admin Email as From Address
* Plugin URI: https://burobjorn.nl
* Description: Use the admin email address and from name set in Settings->General as From sender email name and address
* Author: Björn Wijers <[email protected]>, Ramon Fincken <[email protected]>
* Version: 1.2
* Author URI: https://burobjorn.nl
* License: GPL2 or later
**/
/**
* NOTE:
* This plugin fixes a bug in WordPress 4.6 (and possible version 4.5.3):
*
* If a From email address is *NOT* set *AND* the $_SERVER[ 'SERVER_NAME' ] is empty
* WordPress will generate an invalid email address 'wordpress@'
*
* by explicitly setting the From email address we prevent this bug from
* happening.
*
* See also issue #25239:
* https://core.trac.wordpress.org/ticket/25239
**/
if( ! class_exists( 'AdminEmailAsFromAddress' ) ) {
class AdminEmailAsFromAddress {
function __construct() {
add_filter( 'wp_mail_from', array( $this, 'set_from_email' ) );
add_filter( 'wp_mail_from_name', array( $this, 'set_from_name' ) );
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
/**
* Called by constructor
* Registers the two fields to override sender From email address
* and sender From name
*
* @param void
* @return void
*
**/
function register_fields() {
register_setting( 'general', 'aeafa_mail_from', 'is_email' );
add_settings_field('aeafa_mail_from', '<label for="aeafa_mail_from">'.__('WordPress email sender From address' , 'aeafa' ).'</label>' , array(&$this, 'aeafa_mail_from_field') , 'general' );
register_setting( 'general', 'aeafa_mail_name', 'esc_html' );
add_settings_field('aeafa_mail_name', '<label for="aeafa_mail_name">'.__('WordPress email sender From name' , 'aeafa' ).'</label>' , array(&$this, 'aeafa_mail_name_field') , 'general' );
}
/**
* Called by settings field
* Shows field to override sender From email address
*
* @param void
* @return void
*
**/
function aeafa_mail_from_field() {
$value = get_option( 'aeafa_mail_from', '' );
echo '<input type="text" id="aeafa_mail_from" name="aeafa_mail_from" value="' . $value . '" />';
echo '<p class="description" id="aeafa-mail-from-description">'. __( 'This address is used as From emailaddress when WordPress sends out an email. When empty .. the From address will be the admin purposes email address used in the general settings.' ).'</p>';
}
/**
* Called by settings field
* Shows field to override sender From name
*
* @param void
* @return void
*
**/
function aeafa_mail_name_field() {
$value = get_option( 'aeafa_mail_name', '' );
echo '<input type="text" id="aeafa_mail_name" name="aeafa_mail_name" value="' . $value . '" />';
echo '<p class="description" id="aeafa-mail-name-description">'. __( 'This value is used as From emailaddress sender name when WordPress sends out an email. When empty .. the default "WordPress" sender name will be used.' ).'</p>';
}
/**
* Called by 'wp_mail_from' filter
* Allows setting the From email address
* uses the admin_email option by design.
* If the email address needs to be changed
* you need to change the 'admin_email' in Settings->General.
* You can even keep the admin_mail and create a seperate sender
* from in Settings->General using the 'WordPress email sender
* From address' field.
*
* @param string current email address used as from address
* @return string new email address used for from address
*
**/
function set_from_email( $email ) {
// Grab default admin mail address first
$admin_email = get_bloginfo( 'admin_email' );
$mail = empty( $admin_email ) ? $email : $admin_email;
// Replace with override mail address?
$email_from_field = get_option( 'aeafa_mail_from', '' );
$mail = empty( $email_from_field ) ? $mail : $email_from_field;
// Apply further filters
return apply_filters( 'aeafa_mail_from', $mail);
}
/**
* Called by filter 'wp_mail_from_name'
* Allows setting the name used in the From email header
* defaults to 'WordPress'
* You can create a seperate sender name
* from in Settings->General using the 'WordPress email
* sender From name' field.
*
* @param string current name
* @return string new name, defaults to WordPress
*
**/
function set_from_name( $name ){
// Grab default admin from name first
$name = empty ( $name ) ? 'WordPress' : $name;
// Replace with override mail address sender?
$email_name_field = get_option( 'aeafa_mail_name', '' );
$name = empty( $email_name_field ) ? $name : $email_name_field;
// Apply further filters
return apply_filters( 'aeafa_mail_from_name', $name);
}
}
$admin_email_as_from_address = new AdminEmailAsFromAddress();
} else {
error_log( 'Class AdminEmailAsFromAddress already exists. Plugin activation failed' );
}
?>