Execute Shortcode within Shortcode

0 down vote favorite

Hello there guys, newbie here :slight_smile:

Working with wordpress. Not sure if this is the right forum to post.

I am using this plugin

Shortcode syntax:

[worldcurrency cur=“EUR” value=“25”]

However, i am using Advanced custom fields and the shortcode for custom fields is not being executed within the shortcode for worldcurrency.

[worldcurrency curr=“[acf field=“fl_currency”]” value=“[acf field=“fl_value”]”]

If you look at the ACF shortcode in source (api.php), it just uses the API function get_field on the shortcode’s field attribute.

If you look at the world currency shortcode in source (worldcurrency.php), it outputs a span with the worldcurrency class and a couple of custom attributes, which then get converted on the user side via javascript.

I could probably write my own shortcode or custom function that combines the two, using the output of world currency with ACF’s get_field to fetch the attributes from fields I specify.

Any ideas on how to achieve this?

thanks
Andy

You want to enable recursive shortcodes for the worldcurrency plugin
http://wordpress.stackexchange.com/questions/29657/nested-shortcode-doesnt-work-even-when-recursively-called
http://codex.wordpress.org/Shortcode_API#Nested_Shortcodes

thanks cpradio but the wordpress API states:

Square Brackets

The shortcode parser does not accept square brackets within attributes. Thus the following will fail:

[tag attribute=“[Some value]”]

I would think something like this would work, but maybe it doesn’t.

function my_shortcode_handler( $atts, $content = null ) {
   extract( shortcode_atts( array(
      'attr_1' => 'attribute 1 default',
      'attr_2' => 'attribute 2 default',
      // ...etc
      ), $atts ) );

  foreach ($atts as $key => $attValue)
  {
    $atts[$key] = do_shortcode($attValue);
  }

  // continue with rest of wordcurrency shortcode call
}

hmm, i quickly tested this but doesn’t seem to work… I am kinda stuck here :frowning:

Hmm…

Just want to make sure the code you tried matches what I think the dt_wc_shortcode should look like (I don’t have a WP installation that I can work with until i get home)

	function dt_wc_shortcode($attr) {
		global $post;

		foreach ($attr as $key => $attValue)
		{
			$attr[$key] = do_shortcode($attValue);
		}

		if (!isset($attr['curr']) || !isset($attr['value']))
			return '[worldcurrency error: curr="" and value="" parameters needed]';
		
		$out = '<span class="worldcurrency" postId="'.$post->ID.'" curr="'.$attr['curr'].'" value="'.$attr['value'].'" ';
		
		if (isset($attr['historic']))	$out .= 'historic="'.$attr['historic'].'" ';
		if (isset($attr['target']))		$out .= 'target="'.$attr['target'].'" ';

		$out .= '></span>';
			
		return $out;
	}

yes this is the shortcode for worldcurrency:


	function dt_wc_shortcode($attr) {
		global $post;
		
		if (!isset($attr['curr']) || !isset($attr['value']))
			return '[worldcurrency error: curr="" and value="" parameters needed]';
		
		$out = '<span class="worldcurrency" postId="'.$post->ID.'" curr="'.$attr['curr'].'" value="'.$attr['value'].'" ';
		
		if (isset($attr['historic']))	$out .= 'historic="'.$attr['historic'].'" ';
		if (isset($attr['target']))		$out .= 'target="'.$attr['target'].'" ';

		$out .= '></span>';
			
		return $out;
	}

Still not sure how to combine with the above code you mentioned.

You should be able to just copy what I wrote in Post #6 and replace the code you have shown in #7. It will either work, or continue to not work.

Shortcodes in attributes won’t work if you are using WordPress’ Shortcode API. They will work in content between opening & closing shortcodes but for that the outer shortcode will have to parse the content of the one inside.

So this will work:


add_shortcode( 'test', 'parse_test_tag' );
add_shortcode( 'test_more', 'parse_test_more_tag' );

function parse_test_tag( $attrs, $content = '' ) {
    extract( shortcode_atts( array(
        'att_a' => '',
        'att_b' => '',
    ), $attrs ) );

    $content = do_shortcode( $content );

    //do some more stuff here

    return $content;
}

function parse_test_more_tag( $attrs, $content = '' ) {
    extract( shortcode_atts( array(
        'att_y' => '',
        'att_z' => '',
    ), $attrs ) );

    //do some more stuff here
    //do some more stuff here

    return $content;
}

But this won’t work:


add_shortcode( 'test', 'parse_test_tag' );
add_shortcode( 'test_more', 'parse_test_more_tag' );

function parse_test_tag( $attrs, $content = '' ) {
    $attrs = array_map( 'do_shortcode', $attrs );    //this & the foreach to go through all values in $attrs is one & same
    //but this won't work as the tags will come up as broken & muxed up with attr values, so nothing will be here to parse
    //for the do_shortcode()

    extract( shortcode_atts( array(
        'att_a' => '',
        'att_b' => '',
    ), $attrs ) );

    //once $attrs is passed through shortcode_atts() then the remnants of broken tags will be gone as well

    return $content;
}

function parse_test_more_tag( $attrs, $content = '' ) {
    extract( shortcode_atts( array(
        'att_y' => '',
        'att_z' => '',
    ), $attrs ) );

    //do some more stuff here
    //do some more stuff here

    return $content;
}

If you want to parse shortcodes in attributes then the only way is to write your own parser for the parent tag and use Shortcode API on the value of parent tag’s attributes or write your own parser for them as well.