Home Forums General Issues Execute a shortcode from custom field in the sidebar


Execute a shortcode from custom field in the sidebar

  • I have a shortcode in a custom field that I want to execute in a sidebar widget. I’m using the PHP Code Widget for this. I’ve had good luck in getting my other custom field data displayed in the sidebar this way, but a shortcode has some unique challenges.

    The shortcode itself is generated dynamically during a post import process so the shortcode for each post is different and custom for that individual post. (it’s a YouTube API call via the TubePress plugin).

    I thought I could some sort of nested PHP function, but I’m having trouble doing it. So far, this is what I have:

    global $wp_query;
    $postid = $wp_query->post->ID;
    echo do_shortcode () {

    I’m pretty sure though that nested PHP functdfsaions are bad idea. Is there a way to do this? Should the custom field data first be called into an array and then do_shortcode() should just call the array?

    I’m over my head on this one, but I’m thinking my use case isn’t totally unique and if this can figured, it may be helpful to others too.


  • For the record, I did attempt using [acf field="{$trailer}"] In a text widget by supporting it with add_filter( 'widget_text', 'do_shortcode'); in functions.php but it doesn’t work. Likely because the sidebar is outside the loop and there is not data.

  • OKay, taking some guidance from here:

    I did this:

    global $wp_query;
    $postid = $wp_query->post->ID;
    $trailer = get_field( "trailer" ); 
     echo do_shortcode("$trailer");

    Now it is outputting the shortcode from the custom field and printing it, but it does not execute it. I changed the Formatting for the field to No Formatting, but all it did is return the shortcode’s square brackets as HTML entities.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Execute a shortcode from custom field in the sidebar’ is closed to new replies.