Run Shortcode of post’s custom field in functions.php / Plugin

After 3 or 4 hours of searching the web for a solution, I abandoned it and hopefully you can help me?

  1. I added a sub-headline as a custom field (subheadlineclass). That works pretty. I can save, edit and open it in WordPress’ post-editor.

  2. There is a shortcode in this custom field to generate this sub-title dynamically. (I use PHP Code for posts to do so.)

3.1. Also, it’s no problem to call this custom field (as a executed shortcode) in my template with:

if (!empty(get_post_meta(get_the_ID(), "subheadlineclass", true))) {
$subheadline = get_post_meta(get_the_ID(), "subheadlineclass", true);
$subheadline = do_shortcode(subheadline);

3.2. This lines are placed in the index.php (inside the loop). After this, I can use the variable $subheadline to place the sub-headline, wherever I want (for example below the title).

3.3. Of course you need to add this to the functions.php:

add_filter( "the_content", 'do_shortcode');
  1. Anyway, now to the problem: I outsourced many snippets from the functions.php to a “plugin” (actually it doesn’t matter, if it’s the functions.php or placed in a plugin to solve this issue). At this place I cannot use this snippet any longer. The string is empty.

BUT this var_dump tells me, the shortcode isn’t just executed:

$shortcode = get_post_meta(get_the_ID(),'subheadlineclass',true);


string(40) "[php snippet=1 param="subheadline=1"]"

Soooo I cann call the variable of the custom field, but do_shortcode (like in 3.1.) doesn’t work. But why? And how can this problem be solved? Thanks!

Read more here: Run Shortcode of post’s custom field in functions.php / Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *