I recently stumbled upon this and was wondering if calling wp_mail()
in a theme is allowed or not as per WordPress standards. I should clarify that I'm not overriding it as a pluggable function in the theme, I am just calling it if it exists.
I'm asking here because I've been searching for this but did not find any clear answer stating anything like this.
I recently stumbled upon this and was wondering if calling wp_mail()
in a theme is allowed or not as per WordPress standards. I should clarify that I'm not overriding it as a pluggable function in the theme, I am just calling it if it exists.
I'm asking here because I've been searching for this but did not find any clear answer stating anything like this.
Share Improve this question edited Dec 21, 2016 at 9:11 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Dec 21, 2016 at 9:01 Alex CAlex C 1013 Answers
Reset to default 0While wp_mail()
is not exactly forbidden in a theme, it is very likely misplaced there. The purpose of a theme is presentation. It should not change existing data, and it should always be easy to replace.
That's the reason why contact forms, polls, shops, tracking and similar functionality is pure plugin territory. There is no context for wp_mail()
in a theme.
WordPress provides a lot of functions you can use, including the wp_mail()
function. The function is not only a plugin territory, the core uses it often.
If you will change the behavior, use the filter and actions hooks that are inside the core to influence the behavior of the function. As example for this function give it the filter hook wp_mail
, see here in the source. That is a possibility to change the behavior of wp_mail()
.
The wp_mail()
function is also pluggable and can be replaced by a new function with the same name. All functions in wp-includes/pluggable.php
have a check if the function exists. That gives developers the chance to replace the function with changes to the functionality.
There's absolutely nothing wrong with calling wp_mail()
in a theme. It's a core function that is meant to be used.