Shortcodes Inside a Shortcode - Wordpress

Shortcodes inside a shortcode - wordpress

Shortcodes do not automatically nest -- you have to call do_shortcode($content) yourself. See the caption_shortcode() example on http://codex.wordpress.org/Shortcode_API.

Shortcode inside a shortcode

Just for update :

This was because wp doenst allow shortcode nested code.

Shortcode inside a WordPress shortcode

You could manually call do_shortcode on the string your shortcode callback returns. Something like the following should work:

// Add Shortcode
function person_shortcode( $atts , $content = null ) {

// Attributes
extract( shortcode_atts(
array(
'name' => 'name',
'numb' => 'numb',
), $atts )
);

// Code
return do_shortcode( '<p style="text-align: left;">[su_lightbox type="inline" src=".'.$numb.'"]'. $name .'<i class="fa fa-chevron-right float-right"></i>[/su_lightbox]</p>
<div class="'. $numb .' mfp-hide">
'. $content . '
</div>' );
}
add_shortcode( 'person', 'person_shortcode' );

You also have a syntax error in this section of code:

src=".'.$numb'"]'

You probably intended this:

src=".'.$numb.'"]'

I've fixed this in the sample above.

WordPress: Calling a shortcode for a value in a different shortcode

As @Jeppe mentioned, you can do Nested Shortcodes:

[shortcode_1][shortcode_2][/shortcode_1]

But the parser does not like shortcode values as attributes in other shortcodes.

It sounds like you're reliant on a few plugins and their shortcodes, so I don't suggest editing those shortcodes - but if you look at the Shortcode API it's pretty easy to add you own. For simplicity's sake, this example won't contain the "proper" methods of making sure the shortcodes exist/plugins are installed etc, and will just assume they are.

// Register a new shortcode called [calendar_with_latlng]
add_shortcode( 'calendar_with_latlng', 'calendar_with_latlng_function' );

// Create the function that handles that shortcode
function calendar_with_latlng_function( $atts ){
// Handle default values and extract them to variables
extract( shortcode_atts( array(
'near_distance' => 20
), $atts, 'calendar_with_latlng' ) );

// Get the current latlng from the gmw shortcode
$gmw_current_latlng = do_shortcode( '[gmw_current_latlng]' );

// Drop that value into the fullcalendar shortcode, and add the default (or passed) distance as well.
return do_shortcode( '[fullcalendar near='. $gmw_current_latlng .' near_distance='. $near_distance .']' );
}

Provided [gmw_current_latlng] returns a useable format for your [fullcalendar] shortcode, you should now be able to use your new shortcode that combines the two: [calendar_with_latlng] or you can also add the near_distance attribute: [calendar_with_latlng near_distance=44].

You would just need to put the above functions into your functions.php, create a Simple Plugin, or save them to a file and add it in your Must-Use Plugins directory.

How can I use the output of a shortcode inside of another shortcode in Wordpress?

This answer was given to me on reddit:

"It may be possible to get nested shortcode to work, but you would probably need to process the content twice or something and I would imagine it isn't going to be straightforward.

I think the easiest thing to do would just create a new shortcode as a wrapper and call the original shortcode in it.

So, if the original looks like this: [original_shortcode page=1]

You could create one like this: [original_shortcode_new page_alignment='left']

Then write a shortcode, escape/validate user input, and call the main shortcode.

Essentially:

function original_shortcode_new( $args ) {

$args = shortcode_atts(
array(
'page_alignment' => ''
), $args, 'original_shortcode_new' );

$page_id = '';

if($args['page_alignment'] == 'left' && isset($_GET['dropdownl'])){
$page_id = intval($_GET['dropdownl']);
} else if($args['page_alignment'] == 'right' && isset($_GET['dropdownr'])){
$page_id = intval($_GET['dropdownr']);
}

if(empty($page_id)){
return "<p>Error, bad page</p>";
}

return do_shortcode("[original_shortcode page={$page_id}]");

}

add_shortcode( 'original_shortcode_new', 'original_shortcode_new' );

Again, when doing the above, I would get rid of the extra shortcodes and globals and just use some logic inside a single shortcode."

This is the part of the answer that answers my question above, but to see the full post click here: Reddit Question

Wordpress do_shortcode with content and shortcodes inside adds shortcode before the content instead of inline

This is because you are displaying the HTML directly within your shortcode callback. You need to return the HTML back through the shortcode. Think of this like a WordPress filter. This will render your short code and output it wherever in the content it was placed.

function add_entry_content_func( $atts, $content = '' ) { 

$html = '<div class="entry-content content">';
$html .= apply_filters('the_content', $content);
$html .= '</div>';

return $html;
}

Or try this with ob_start();

<?php function add_entry_content_func( $atts, $content = '' ) { 

ob_start(); ?>

<div class="entry-content content">
<?php echo apply_filters('the_content', $content); ?>
</div><!-- .entry-content -->

<?php

return ob_get_clean();
} ?>


Related Topics



Leave a reply



Submit