[resolved] [closed] Shortcodes in PHP code widget? (5 posts)

  1. johnsalomon
    Posted 4 years ago #


    I am trying to create a shortcode inside a php code widget by calling a custom field, like so:

    1. I have a shortcode in my post, with format [flickrgallery setid="<number>"] -- for example:

    [flickrgallery setid="72157626892022672"]

    (This part works and places a Flickr thumbnail grid from that particular set in my post).

    2. I have defined a custom field, per_post_widget_content, in this particular post it has value 72157626892022672 (same as above)

    3. I am trying to figure out how to use a php code widget to not just output the same as in step #1, i.e. [flickrgallery setid="72157626892022672"] but to actually process it and display my thumbnails in the widget. So far I have tried:

    <?php echo '[flickrgallery setid="'; echo get_post_meta($post->ID, 'per_post_widget_content', true); echo '"]'; ?>

    However, the best I can get it to do is to output the shortcode inside the widget, but not actually do anything with it.

    When I use a plain text widget, however, and just put in the full shortcode, it works (but that won't let me use the custom field on a per post basis...)

    I don't think eval would do it, since I don't think shortcodes are actually php code, but at this point I'm lost.

    Anyone? I stink at PHP...

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    you could try to use do_shortcode() http://codex.wordpress.org/Function_Reference/do_shortcode

    although some things with square brackets are not actual shortcode, but get rendered by a filter function on 'the_content' and with regular expressions.

    what plugin is this [flickrgallery setid="72157626892022672"] coming from?
    check if this plugin actually has add_shortcode('flickrgallery', in its code.
    otherwise you might neet to look for the main function in the plugin.
    double check the documentation of the plugin, if any.

  3. johnsalomon
    Posted 4 years ago #


    I will give that a shot. This is actually defined in a theme - Autofocus Pro+ by Allan Cole. Documentation is non-existent, I'm afraid.

    I've tried

    <?php do_shortcode(echo '[flickrgallery setid="'; echo get_post_meta($post->ID, 'per_post_widget_content', true); echo '"]';); ?>

    and it's complaining about expecting ')' (where?!?) - apologies for the boneheaded question, I assume I'd need the echo inside the parentheses?

    Thanks very much for the advice.

  4. Michael
    Forum Moderator
    Posted 4 years ago #

    there is also:

    add this to functions.php:
    add_filter('widget_text', 'do_shortcode');

    (not sure if this will work with the php code widget)

  5. johnsalomon
    Posted 4 years ago #

    the add_filter didn't work, but this is ok:

    <?php echo do_shortcode('[flickrgallery setid="72157626892022672"]'); ?>

    That means do_shortcode is what I'm looking for, thanks!

    However, I can't explicitly specify the shortcode in the widget, as I'd have to have a widget per post (each post has a Flickr gallery of its own).

    Any chance you can give me a clue how I'd get all three parts of the shortcode into the do_shortcode parentheses?

    Again, thank you kindly, this is already a huge help.

    [continued here: http://wordpress.org/support/topic/complex-do_shortcode-statements?replies=2#post-2199313 ]

Topic Closed

This topic has been closed to new replies.

About this Topic