Search code examples
wordpressshortcodepost-meta

Use Shortcode in Shortcode (insert post meta field value)


i have a shortcode from a plugin which i cant modify... This shortcode has some arguments ex. [some_shortcode value=""] - I tried to input the value from post meta as argument for this shortcode, but its not working - here is the code...

This is the code from shortcode i created ( it returns values from post meta )

function test_shortcode( $string ) {
    extract( shortcode_atts( array(
        'string' => 'string'
    ), $string));

    // check what type user entered
    switch ( $string ) {
        case 'first':
            return get_post_meta( get_the_ID(), 'post_meta_one', true );
            break;
        case 'second':
            return get_post_meta( get_the_ID(), 'post_meta_two', true );
            break;
    }
}
add_shortcode('test', 'test_shortcode');

Now i want to insert this shortcode in the existing shortcode from the plugin on my page.

For example: [some_shortcode value='[test string="first"]']

It isnt working like this. Thanks for help!


Solution

  • It will not work to insert the shortcode in the existing shortcode like you provided. Your shortcode should have opportunity to process the provided shortcode as attribute.

    You should to use the do_shortcode() in your shortcode. You have

    [some_shortcode value='[test string="first"]']
    

    and want to use returned value of [test string="first"] which is first in your shortcode. Your code will be:

    function some_shortcode($atts){
        $atts = shortcode_atts(array(
            'value' => ''
        ), $atts);
    
        $second_shortcode_value = do_shortcode($atts['value']);
    
        //some code
    
        return $something;
    }
    add_shortcode('some_shortcode', 'some_shortcode');
    

    The variable $second_shortcode_value will containt the output of the [test string="first"] shortcode.

    P.S. Avoid of using exctract() function just because it can make your code hard readable.

    EDIT:

    Here is solution dinamically add attributes to [some_shortcode] value attribute.

    function my_shortcode($atts){
        $atts = shortcode_atts(array(
            'str' => ''
        ), $atts);
    
    
        switch ( $atts['str'] ) {
            case 'first':
                $modified = get_post_meta( get_the_ID(), 'leweb_gender', true );
                break;
            default:
                $modified = '';
        }
    
        if(!empty($modified)) {
            $second_shortcode_with_value = do_shortcode("[some_shortcode value='$modified']");
        }else{
            $second_shortcode_with_value = do_shortcode('[some_shortcode]');
        }
    
        return $second_shortcode_with_value;
    }
    add_shortcode('some_shrt', 'my_shortcode');
    

    What we are doing: instead of calling [some_shortcode value='something'] we are generating something into our shortcode and getting content like

    [some_shrt str="first"]