SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Execute Shortcode within Shortcode

    0 down vote favorite

    Hello there guys, newbie here

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

    I am using this plugin

    http://wordpress.org/extend/plugins/worldcurrency/

    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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    You want to enable recursive shortcodes for the worldcurrency plugin
    http://wordpress.stackexchange.com/q...rsively-called
    http://codex.wordpress.org/Shortcode...ted_Shortcodes
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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]"]

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    I would think something like this would work, but maybe it doesn't.

    PHP Code:
    function my_shortcode_handler$atts$content null ) {
       
    extractshortcode_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

    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, i quickly tested this but doesn't seem to work.. I am kinda stuck here

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    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)
    PHP Code:
        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;
        } 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes this is the shortcode for worldcurrency:

    Code:
    	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.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    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.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    add_shortcode'test''parse_test_tag' );
    add_shortcode'test_more''parse_test_more_tag' );

    function 
    parse_test_tag$attrs$content '' ) {
        
    extractshortcode_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 '' ) {
        
    extractshortcode_atts( array(
            
    'att_y' => '',
            
    'att_z' => '',
        ), 
    $attrs ) );

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

        
    return $content;

    But this won't work:

    PHP Code:
    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()

        
    extractshortcode_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 '' ) {
        
    extractshortcode_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.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •