• Resolved Jer Clarke

    (@jeremyclarke)


    Hey, this is a wonderful idea and it’s great to see you made it work in such a simple way. Congrats!

    My issue is that I really want to use this in image captions because when documenting shortcodes I like to insert screenshots showing the output you’d get with various attributes and having the shortcode example in the caption is the obvious way to display it.

    I know it’s an annoying edge case (yo dawg, I heard you like shortcodes) but at the same time it seems like a common need for people using your plugin for it’s main purpose: documenting their shortcodes.

    Right now it seems that using [shortcode] in the core [caption] doesn’t work. What I get is the caption with the unprocessed shortcode in it.

    I dug around and it seems that the core img_caption_shortcode() runs do_shortcode() recursively on $content, but only after extracting the caption text and storing it in $atts[‘caption’], so shortcodes in caption text aren’t affected. This isn’t crazy since most shortcodes probably screw up the formatting.

    I figured out a filter on ‘shortcode_atts_caption’ (filter atts for only caption shortcode) that very carefully enables only the [shortcode] shortcode inside image captions and only runs do_shortcode() if [shortcode] is present!

    It seems like a pretty safe thing to use and makes your shortcode work in my captions. Please consider adding it to your plugin since I bet a lot of people would find it useful. Also please let me know if you discover any unintended side effects!

    Here’s the code as a Gist in case the code below is broken:
    https://gist.github.com/jeremyclarke/213de46aa04c95592d3e

    /**
     * Filter Caption shortcode attributes to enable the [shortcode] shortcode inside caption text
     *
     * WP doesn't run do_shortcode on the 'caption' text value parsed out of [caption], which means
     * the [shortcode] shortcode doesn't work.
     *
     * We want [shortcode] to work but not necessarily any other shortcodes, so we will run do_shortcode()
     * only if [shortcode] is present. Running do_shortcode() this way will process all shortcodes
     * so beware any captions that for some reason also have a bad shortcode that will break formatting.
     *
     * @param array $out atts array as determined by WP to be returned after filtering
     * @param array $pairs
     * @param array $atts
     * @return filtered $out atts
     */
    function gv_filter_shortcode_atts_caption_shortcode_shortcode($out, $pairs, $atts) {
    	/**
    	 * If the shortcode shortcode is present then process all shortcodes
    	 * Note: This will process ALL shortcodes which could give insane results
    	 */
    	if (has_shortcode($out['caption'], 'shortcode')) :
    		$out['caption'] = do_shortcode($out['caption']);
    	endif;
    	return $out;
    }
    add_filter('shortcode_atts_caption', 'gv_filter_shortcode_atts_caption_shortcode_shortcode', 10, 3);

    https://wordpress.org/plugins/shortcode-shortcode/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author cubecolour

    (@numeeja)

    Thanks for this suggestion, and for the one in your other topic. Again this is something I never considered when the plugin was created. I’ll have a look at this when I next work on the plugin.

    Thread Starter Jer Clarke

    (@jeremyclarke)

    Thanks for replying and considering my ideas 🙂

    I guess it is a niche plugin but I’m so glad I found it. Will be extremely useful in converting our sad old MediaWiki into WP based documentation. (FWIW MediaWiki also choked on shortcode formatting 😉

    Plugin Author cubecolour

    (@numeeja)

    Hi Jeremy. I’ve just looked at my task list and it is likely to be a while before I get a chance to do anything with this plugin as I have a lot of more pressing tasks.

    I would be happy to add you as a contributor if you would like to make the updates to get your changes in quicker. Let me know if you are interested?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode Shortcode use inside image captions’ is closed to new replies.