44 * Description: Configure your "Reply-To:" for WP_Mail with validation and admin settings.
55 * Requires at least: 4.1
66 * Requires PHP: 5.6
7- * Version: 1.0.2
7+ * Version: 1.0.3
88 * Author: Javier Casares
99 * Author URI: https://www.javiercasares.com/
1010 * License: GPL-2.0-or-later
2020defined ( 'ABSPATH ' ) || die ( 'No script kiddies please! ' );
2121
2222/**
23- * Configure the "Reply-To" header for emails sent by WP Mail .
23+ * Modifies the "Reply-To" header in emails sent using wp_mail() .
2424 *
25- * @param array $args Email arguments.
26- * @return array Modified email arguments.
25+ * Retrieves the "Reply-To" email address from the plugin settings, validates it,
26+ * and injects it into the email headers if appropriate.
27+ *
28+ * @param array $args The email arguments passed to wp_mail().
29+ * @return array Modified email arguments with adjusted "Reply-To" header.
2730 */
2831function wp_mail_replyto ( $ args ) {
29- // Get the "Reply-To" email from plugin settings.
32+ // Retrieve the "Reply-To" email address from plugin settings.
3033 $ reply_to_email = get_option ( 'wp_mail_replyto_email ' );
3134
32- // Define the new "Reply-To" if configured.
33- if ( ! empty ( $ reply_to_email ) ) {
34- $ new_reply_to = sprintf (
35- /* translators: %s: email address */
36- __ ( 'Reply-To: <%s> ' , 'replyto ' ),
37- sanitize_email ( $ reply_to_email )
38- );
35+ // Construct the new "Reply-To" header if a valid email address is set.
36+ if ( ! empty ( $ reply_to_email ) && is_email ( $ reply_to_email ) ) {
37+ $ new_reply_to = 'Reply-To: < ' . sanitize_email ( $ reply_to_email ) . '> ' ;
3938 }
4039
41- // Initialize variables for "From" and existing "Reply-To".
40+ // Initialize variables to track the "From" and existing "Reply-To" headers .
4241 $ from = '' ;
4342 $ existing_reply_to = '' ;
4443
45- // Check if there are existing headers.
44+ // Check if email headers exist .
4645 if ( ! empty ( $ args ['headers ' ] ) ) {
47- // Normalize headers to an array.
46+ // Normalize headers into an array if they are not already .
4847 if ( ! is_array ( $ args ['headers ' ] ) ) {
4948 $ args ['headers ' ] = array_filter ( explode ( "\n" , str_replace ( "\r\n" , "\n" , $ args ['headers ' ] ) ) );
5049 }
5150
52- // Loop through headers to find "From" and "Reply-To".
51+ // Loop through the headers to locate "From" and "Reply-To" values .
5352 foreach ( $ args ['headers ' ] as $ header ) {
5453 if ( stripos ( $ header , 'from: ' ) === 0 ) {
5554 $ from = trim ( substr ( $ header , strlen ( 'from: ' ) ) );
@@ -59,31 +58,39 @@ function wp_mail_replyto( $args ) {
5958 }
6059 }
6160
62- // Check if "Reply-To" exists and compare with "From".
61+ // If "Reply-To" exists and matches the "From" address, replace it with the new one .
6362 if ( ! empty ( $ existing_reply_to ) && strtolower ( $ existing_reply_to ) === strtolower ( $ from ) ) {
64- // If they are the same, replace "Reply-To" with the new one .
65- $ args ['headers ' ] = array_filter (
63+ // Remove the existing "Reply-To" header .
64+ $ args ['headers ' ] = array_filter (
6665 $ args ['headers ' ],
6766 function ( $ header ) {
6867 return stripos ( $ header , 'reply-to: ' ) !== 0 ;
6968 }
7069 );
70+
71+ // Add the new "Reply-To" header.
7172 $ args ['headers ' ][] = $ new_reply_to ;
73+
7274 } elseif ( isset ( $ new_reply_to ) ) {
73- // If "Reply-To" does not exist and new_reply_to is set, add it .
75+ // If no conflicting "Reply-To" is found, append the new one .
7476 $ args ['headers ' ][] = $ new_reply_to ;
7577 }
7678 } elseif ( isset ( $ new_reply_to ) ) {
77- // If there are no headers and new_reply_to is set , create headers with "Reply-To".
79+ // If no headers exist at all , create a new headers array with the "Reply-To".
7880 $ args ['headers ' ] = array ( $ new_reply_to );
7981 }
8082
83+ // Return the modified email arguments.
8184 return $ args ;
8285}
86+
8387add_filter ( 'wp_mail ' , 'wp_mail_replyto ' );
8488
8589/**
86- * Add a settings page to the WordPress admin menu.
90+ * Registers a settings page for the plugin in the WordPress admin menu.
91+ *
92+ * Adds a new page under the "Settings" menu where administrators can configure
93+ * the "Reply-To" email address used in outgoing emails.
8794 */
8895function wp_mail_replyto_add_settings_page () {
8996 add_options_page (
@@ -94,12 +101,19 @@ function wp_mail_replyto_add_settings_page() {
94101 'wp_mail_replyto_render_settings_page '
95102 );
96103}
104+
97105add_action ( 'admin_menu ' , 'wp_mail_replyto_add_settings_page ' );
98106
99107/**
100- * Register the plugin settings.
108+ * Registers the plugin settings, section, and field in the WordPress Settings API.
109+ *
110+ * This function defines:
111+ * - A setting for the "Reply-To" email address with sanitization.
112+ * - A settings section with a description callback.
113+ * - A field for entering the "Reply-To" email address.
101114 */
102115function wp_mail_replyto_register_settings () {
116+ // Register the "Reply-To" email setting with sanitization.
103117 register_setting (
104118 'wp_mail_replyto_settings_group ' ,
105119 'wp_mail_replyto_email ' ,
@@ -110,13 +124,15 @@ function wp_mail_replyto_register_settings() {
110124 )
111125 );
112126
127+ // Add a section to the settings page for organizing fields.
113128 add_settings_section (
114129 'wp_mail_replyto_main_section ' ,
115130 esc_html__ ( 'Reply-To Configuration ' , 'replyto ' ),
116131 'wp_mail_replyto_main_section_callback ' ,
117132 'replyto '
118133 );
119134
135+ // Add the input field for the "Reply-To" email address.
120136 add_settings_field (
121137 'wp_mail_replyto_email_field ' ,
122138 esc_html__ ( 'Reply-To Email Address ' , 'replyto ' ),
@@ -125,39 +141,54 @@ function wp_mail_replyto_register_settings() {
125141 'wp_mail_replyto_main_section '
126142 );
127143}
144+
128145add_action ( 'admin_init ' , 'wp_mail_replyto_register_settings ' );
129146
130147/**
131- * Callback for the settings section description.
148+ * Outputs the description for the main settings section.
149+ *
150+ * This callback is used by the Settings API to display a description
151+ * under the "Reply-To Configuration" section on the plugin settings page.
132152 */
133153function wp_mail_replyto_main_section_callback () {
134154 echo '<p> ' . esc_html__ ( 'Set the email address to be used in the "Reply-To" header of outgoing emails. ' , 'replyto ' ) . '</p> ' ;
135155}
136156
137157/**
138- * Callback to render the email address field.
158+ * Renders the input field for the "Reply-To" email address.
159+ *
160+ * This callback is used by the Settings API to display the input field
161+ * where administrators can enter the email address to be used in the "Reply-To" header.
139162 */
140163function wp_mail_replyto_email_field_callback () {
164+ // Get the current "Reply-To" email address from the plugin settings.
141165 $ email = get_option ( 'wp_mail_replyto_email ' , '' );
166+
167+ // Output the input field HTML.
142168 echo '<input type="email" id="wp_mail_replyto_email" name="wp_mail_replyto_email" value=" ' . esc_attr ( $ email ) . '" size="50" /> ' ;
143169 echo '<p class="description"> ' . esc_html__ ( 'Enter the email address to be used as "Reply-To". ' , 'replyto ' ) . '</p> ' ;
144170}
145171
146172/**
147- * Render the plugin settings page.
173+ * Renders the plugin's settings page in the WordPress admin area.
174+ *
175+ * This function outputs the HTML structure and form elements for the plugin settings page.
176+ * It includes the registered settings fields and sections for configuring the "Reply-To" email.
148177 */
149178function wp_mail_replyto_render_settings_page () {
179+ // Check if the current user has the required capability.
150180 if ( ! current_user_can ( 'manage_options ' ) ) {
151181 return ;
152182 }
153183
184+ // Display settings errors, if any.
154185 settings_errors ( 'wp_mail_replyto_messages ' );
155186 ?>
156187 <div class="wrap">
157188 <h1><?php echo esc_html ( get_admin_page_title () ); ?> </h1>
158189 <form action="options.php" method="post">
159190 <?php
160- wp_nonce_field ( ' update-options ' );
191+ // Output security fields and registered settings sections.
161192 settings_fields ( 'wp_mail_replyto_settings_group ' );
162193 do_settings_sections ( 'replyto ' );
163194 submit_button ( esc_html__ ( 'Save Settings ' , 'replyto ' ) );
@@ -166,11 +197,3 @@ function wp_mail_replyto_render_settings_page() {
166197 </div>
167198 <?php
168199}
169-
170- /**
171- * Load plugin textdomain for translations.
172- */
173- function wp_mail_replyto_load_textdomain () {
174- load_plugin_textdomain ( 'replyto ' , false , basename ( __DIR__ ) . '/languages ' );
175- }
176- add_action ( 'plugins_loaded ' , 'wp_mail_replyto_load_textdomain ' );
0 commit comments