Nested shortcodes

I’m using 2 plugins in my wordpress blog – WP-Members and Dropbox Folder Share. I want to do something like this:

[dropbox-foldershare-hyno link="[wp-members field="some_link"]" ver_como='lista']

Is this possible?

Related posts

3 comments

  1. You cannot use shortcodes like this. The parser would not read that like you want.

    But there is a workaround: Hijack the shortcode dropbox-foldershare-hyno, run the callback function for the wp-members on the link and pass the result to the original dropbox-foldershare-hyno callback.

    Sample code, not tested:

    // wait until the other plugins are loaded
    add_action( 'wp_loaded', 'wpse_100100_shortcode_magic' );
    
    function wpse_100100_shortcode_magic()
    {
        add_shortcode(
            'dropbox-foldershare-hyno',  
            'wpse_100100_shortcode_replacement' 
        );
    }
    
    function wpse_100100_shortcode_replacement( $atts )
    {
        global $bvwidget;
    
        if ( isset ( $atts['link'] ) )
            $atts['link'] = wpmem_shortcode( array( 'field' => $atts['link'] ) );
    
        return $bvwidget->replace_shortcode( $atts );
    }
    

    Now you can use the shortcode [dropbox-foldershare-hyno] and pass a value for the link attribute that should be converted from WP-Members before Dropbox Folder Share gets its hands on it.

  2. How we can do it in the near future 😉

    When the dropbox-foldershare-hyno plugin becomes WordPress 3.6 ready, we can do this:

    add_filter('shortcode_atts_dfh','overwrite_dfh_atts',10,3);
    function overwrite_dfh_atts($out, $pairs, $atts){
        if($atts['link'])
            $out['link'] = do_shortcode( sprintf( '[wp-members field="%s"]', esc_attr( $atts['link'] ) ) ); 
    
        return $out;
    }
    

    to overwrite the link attribute of shortcode:

    [dropbox-foldershare-hyno link="some value for the wp-member field attribute"] 
    

    shortcode, if the corresponding shortcode_atts_{$shortcode} filter is shortcode_atts_dfh.

    You can read more about it here.

  3. Nested shortcodes only work in certain specific circumstances:

    • Only enclosed shortcodes can be nested. In other words, the style of [shortcode] content [/shortcode]. Self-enclosing shortcodes like [shortcode attribute=”foo”] cannot be nested.
    • Even in that case, the outer shortcode must be set up properly by calling do_shortcodes() on the content that’s being returned. (ref: Shortcodes API: Nested Shortcodes)

    So, in your example, the answer is probably no, due to the first point above. But to be sure, you’d have to check the documentation for your specific plugins to see if the author gives any other options for how to use their particular shortcodes.

Comments are closed.