I'm having a little problem figuring out how to execute shortcodes stored in a mail content option of a plugin.
The thing is, I have a field in my options page named '_message' that works with WYSIWYG Editor, and I want to save shortcodes there and then execute them before send the email.
There is some way of detect shortcodes in strings?. Ex:
$message = 'blablabla [sale_products] blablaba';
do_shortcode($message);
If you have another suggestion of how can this be done, that would be great!
I'm having a little problem figuring out how to execute shortcodes stored in a mail content option of a plugin.
The thing is, I have a field in my options page named '_message' that works with WYSIWYG Editor, and I want to save shortcodes there and then execute them before send the email.
There is some way of detect shortcodes in strings?. Ex:
$message = 'blablabla [sale_products] blablaba';
do_shortcode($message);
If you have another suggestion of how can this be done, that would be great!
Share Improve this question edited Nov 3, 2018 at 17:52 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 3, 2018 at 17:31 Renan A. DiazRenan A. Diaz 11 bronze badge2 Answers
Reset to default 0Have you tried
$message = apply_filters('the_content', $your_data);
I think this filter could handle the shortcode. (not tested)
While this is not very obvious, do_shortcode
is usually applied as a filter on a function that generates a certain text. It is, for instance, defined as a standard filter on the_content
like this:
add_filter ('the_content','do_shortcode');
So, the trick is to define it as a filter on get_option
in your plugin. If you look at the source code of get_option
you'll see that on the last line there is a filter applied, the name of which depends on the name of the option. Suppose the name of the option that stores the message is my_message
You would have something like this:
add_filter ('option_my_message','do_shortcode');
That should mean that if you have $message = get_option('my_message')
in your plugin, you would get it with shortcodes evaluated.