How can I get content inside one shortcode to another shortcode?

The question:

I have 2 shortcodes, [ref] and [references].
I need to get the content inside the [ref] shortcode and output it when the [references] shortcode is called.

For an example;

  • INPUT –
    This is some content in the webpage outside the shortcode. [ref] This is some content inside the shortcode “ref”. [/ref]
    This is some other content.
    References : [references]

  • EXPECTED OUTPUT –
    This is some content in the webpage outside the shortcode.
    This is some other content.
    References : This is some content inside the shortcode “ref”.

Here’s the code that I’ve tried using, but I can’t get it to display the content.

function ref_shortcode( $atts , $content = null ){
    ob_start();
    static $i=1;
    $return_value = '<a href="https://localhost/reference-list/?page_id=12&preview=true#references"><sup>['.$i.']</sup></a>';
    $i++;
    return $return_value;
    return ob_get_clean();
}
global $my_content;
$my_content = add_shortcode( 'ref', 'ref_shortcode' );

function references_shortcode( $atts , $content = null ){
    ob_start();
    global $my_content;
    return '<li>' . $my_content . '</li>';
    return ob_get_clean();
}
add_shortcode( 'references' , 'references_shortcode');

I have no issue with the function for the “ref” shortcode, if there’s some way I can get content inside it to the “references” shortcode that would be great!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

There is no definitive way of doing that in WordPress, but here are some options for you:

  1. Use a global variable to store the reference list, i.e. the content as in [ref]content here[/ref]. But I personally don’t recommend and wouldn’t be using global variable.

  2. Use a custom PHP class and add the shortcode functions as methods in that class, then use a property (e.g. private $ref_list = [];) to store the reference list. So for example inside the ref_shortcode() method, you could do $this->ref_list[] = $content;.

  3. Use the object caching API in WordPress, e.g. use wp_cache_set() to store the reference list.

And I’m not asking you to use the third option above, but since your code originally not in a PHP class, then here’s a working example using the object caching API:

function ref_shortcode( $atts = array(), $content = null ) {
    if ( ! empty( $content ) ) {
        $post_id = get_the_ID();

        // Retrieve current list of references for the current post.
        $refs = wp_cache_get( "post_{$post_id}_references" );
        $refs = is_array( $refs ) ? $refs : array();

        // Then add the current reference to the list.
        $refs[] = $content;
        wp_cache_set( "post_{$post_id}_references", $refs );

        $j = count( $refs );
        return "<a href='#ref-$post_id-$j'><sup>[$j]</sup></a>";
    }

    return '';
}
add_shortcode( 'ref', 'ref_shortcode' );

function references_shortcode( $atts = array(), $content = null ) {
    $post_id = get_the_ID();
    $refs = (array) wp_cache_get( "post_{$post_id}_references" );
    $output = '';

    if ( ! empty( $refs ) ) {
        $output = '<h3>References</h3>';
        $output .= '<ul>';

        foreach ( $refs as $i => $ref ) {
            $j = $i + 1;
            $output .= "<li id='ref-$post_id-$j'>$ref</li>";
        }

        $output .= '</ul>';
    }

    return $output;
}
add_shortcode( 'references' , 'references_shortcode' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment