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

前端 未结 2 832
北恋
北恋 2021-01-28 18:57

I\'m very new to this, so please go easy on me if this is a dumb question.

I am building a site with events manager and geo my WordPress plugins. I want a user to be abl

2条回答
  •  梦如初夏
    2021-01-28 19:47

    Of course you can pass a shortcode as an attribute of another shortcode. The only problem is, attributes doesn't pass [ or ]. So you have replace convert those bracket them with their html entry.

    Replace [ with [ and ] with ] and you should be fine. Here is an example.

    function foo_shortcode( $atts ) {
    
        $a = shortcode_atts( array(
            'foo' => "Something",
            'bar' => '',
        ), $atts );
    
        $barContent = html_entity_decode( $atts['bar'] );
        $barShortCodeOutput = do_shortcode($barContent);
    
        return sprintf("Foo = %s and bar = %s", $a['foo'], $barShortCodeOutput);
    }
    add_shortcode( 'foo', 'foo_shortcode' );
    
    
    function bar_shortcode( $atts ) {
        return "Output from bar shortcode";
    }
    add_shortcode( 'bar', 'bar_shortcode' );
    

    Then put this on your editor

    [foo bar=[bar] ]
    

    See we are passing a shortcode [bar] as an attribute of [foo]. So the output should be - Foo = Something and bar = Output from bar shortcode

    I know it looks a bit nasty but it will do the trick.

提交回复
热议问题