Nested Shortcodes
-
I’m trying to nest
[groups_group_info group=”MD2015″ show=”users”]
inside
[upme group=HERE view=compact users_per_page=10]
where it says here.
I’ve been struggling with do_shortcode for hours and can’t for the life of me get anything to work.
-
Hi jwsmith300
From what plugins are the shortcodes? If they are from one plugin ask the plugin author if it can be done. You can’t nest them if from different plugin’s as the shortcodes are not aware of each others attributes.
You also can’t nest the way you want them to as they are self-closing shortcodes.
https://codex.wordpress.org/Shortcode_API#Enclosing_vs_self-closing_shortcodesThis is how you would nest enclosing shortcodes
https://codex.wordpress.org/Shortcode_API#Nested_Shortcodes.Yeh they are from different plugins
the first simply returns a list of numbers separated by commas.
the second is expecting a list of numbers separated by commas where I wrote HERE.
I just want it to get the list of numbers and use it as the attribute.
Can you post the code that doesn’t work. I assume you don’t use this in the post editor as you mentioned the do_shortcode function.
[upme group=do_shortcode(‘[groups_group_info group=”MD2015″ show=”users”]’ ) view=compact users_per_page=10]
The UPME plugin allows you to select a range of profiles to show based on a list of user IDs. I am trying to use the groups_group_info shortcode to return a list of User IDs to show.
Both shortcodes work okay when not nested.
Before we proceed do you put this in the post editor or in one of your theme template files?
do_shortcode() is a php function and doesn’t work in the post editor. It’s either stripped from the content (as php is not allowed) or interpreted as text.
What are the plugins. Provide links to where you downloaded them from.
in the post editor :/ so that is why it doesn’t work. I guess I code code it into the upme group plugin but then I wouldn’t be able to change what group I was calling.
https://wordpress.org/plugins/groups/
http://codecanyon.net/item/user-profiles-made-easy-wordpress-plugin/4109874Can you code it anyway so that it retrieves the list of IDs first. Calls this phrase X
and then it runs the second shortcode and uses X as the attribute?
Try it with this [untested]:
[upme group='[groups_group_info group="MD2015" show="users"]' view="compact" users_per_page="10"]I don’t think that will work, but why not try.
It’s difficult because the user profiles plugin is a commercial plugin. I can’t see in the code.
You can filter a shortcode if the plugin uses this function and provides the third parameter. https://codex.wordpress.org/Function_Reference/shortcode_atts
Can you put this in your theme’s functions.php file for testing:
add_filter ( 'shortcode_atts_upme', 'my_shortcode_atts', 10, 3 ); function my_shortcode_atts( $out, $pairs, $atts ) { echo '<pre>'; print_r( $out ); echo '</pre>'; return $out; }Create a test post with the upme shortcode.
If the filter is used it will print an array with atrributes to the screen when on the post’s page in the front end of your site.Remove the shortcode and code after testing.
You mean just add [upme] and see what it does after adding that code in the functions file?
Yes. Or use it with some of the attributes. It should print the array regardless of the attributes on the single post page.
it just displays the user who is logged in profile as no group attribute is specified
It doesn’t seem the upme shortcode uses the shortcode_atts() function.
Remove the testing code from your functions.phpAnother way is to create your own shortcode with the attributes from both the groups_group_info and upme shortcode combined.
Try it with this in your functions.php file:
function upme_group_shortcode( $atts ) { $args = shortcode_atts( array( // defaults 'group' => '', 'show' => 'users', 'view' => 'compact', 'users_per_page' => 10, ), $atts, 'upme_group' ); // check if a group was provided if ( empty( $args['group'] ) ) { return ''; } // get the group with the groups_group_info shortcode $group_info = "[groups_group_info group='{$args['group']}' show='{$args['show']}']"; $group = do_shortcode( $group_info ); // check if a group was found if ( empty( $group ) ) { return ''; } $upme_shortcode = "[upme group='{$group}' view='{$args['view']}'"; $upme_shortcode .= " users_per_page='{$args['users_per_page']}']"; // return the upme shortcode return do_shortcode( $upme_shortcode ); } // create a new shortcode 'upme_group' add_shortcode( 'upme_group', 'upme_group_shortcode' );Now you can use the new ‘upme_group’ shortcode like this:
[upme_group group="MD2015" show="users" view="compact" users_per_page="10"]This code is untested as I don’t have access to the
User Profiles shortcode.
The topic ‘Nested Shortcodes’ is closed to new replies.