Deven Kumar
Deven Kumar

Reputation: 53

How can I use the output of a shortcode inside of another shortcode in Wordpress?

I have written a shortcode that returns a variable that was set in my php file, and I need to use the value of that variable in another shortcode which I got from a plugin, "Insert Pages". I understand that I cannot do [insert page='[left]' display='content'] because the Wordpress parser ends the shortcode at the first ], but I am wondering if there is a way around this.

Here is the code of my page, apologies for the bad indentation. The shortcodes from my php file are [right] and [left] which return a number corresponding to the value of the option the user chose. The other shortcode is from the Insert Pages plugin and takes the desired ID as the value for which page to show.

The code in question is below the first 'select' tag. 143380 is an example of a value, that is where I need the result of my shortcode to go.

<form id="form" action="" method="get">
[one_half]
<h3>Select a Country</h3>
<div class="dropdown"><select id="dropdownl" name="dropdownl">
<option disabled="disabled">Country</option>
<option disabled="disabled">North America</option>
<option value="62292">Canada</option>
<option value="72808">Mexico</option>
<option value="144586">Puerto Rico</option>
<option value="163668">United States</option>
<option disabled="disabled">South America</option>
<option value="63839">Argentina</option>
<option value="67922">Brazil</option>
<option value="78702">Colombia</option>
<option value="60670">Peru</option>
<option disabled="disabled">Oceania</option>
<option value="32137">Australia &amp; New Zealand</option>
<option disabled="disabled">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="disabled">Asia</option>
<option value="66137">China</option>
<option value="49932">India</option>
<option value="40308">Israel</option>
<option value="78709">Japan</option>
<option value="223252">Philippines</option>
<option value="60438">Republic of Korea</option>
<option value="69605">Singapore</option>
<option value="70391">Taiwan</option>
<option value="71971">Thailand</option>
<option disabled="disabled">Europe</option>
<option value="32142">Austria</option>
<option value="32146">Belgium</option>
<option value="78707">Bulgaria</option>
<option value="72763">Czech Republic</option>
<option value="31277">Denmark</option>
<option value="62393">Finland</option>
<option value="31223">France</option>
<option value="31026">Germany</option>
<option value="63208">Greece</option>
<option value="63016">Hungary</option>
<option value="183519">Ireland</option>
<option value="122492">Italy</option>
<option value="142792">Lithuania</option>
<option value="162411">Netherlands</option>
<option value="67164">Norway</option>
<option value="56320">Poland</option>
<option value="62344">Portugal</option>
<option value="67210">Romania</option>
<option value="68895">Russian Federation</option>
<option value="143380">Serbia</option>
<option value="78705">Slovakia</option>
<option value="25372">Spain</option>
<option value="25520">Sweden</option>
<option value="25408">Switzerland</option>
<option value="67375">Turkey</option>
<option value="63212">Ukraine</option>
<option value="25482">United Kingdom</option>
</select></div>

[left]

[insert page='143380' display='content']

[/one_half]

[one_half_last]
<h3>Select a Country</h3>
<div class="dropdown">

<select id="dropdownr" name="dropdownr">
<option disabled="disabled">Country</option>
<option disabled="disabled">North America</option>
<option value="62292">Canada</option>
<option value="72808">Mexico</option>
<option value="144586">Puerto Rico</option>
<option value="163668">United States</option>
<option disabled="disabled">South America</option>
<option value="63839">Argentina</option>
<option value="67922">Brazil</option>
<option value="78702">Colombia</option>
<option value="60670">Peru</option>
<option disabled="disabled">Oceania</option>
<option value="32137">Australia &amp; New Zealand</option>
<option disabled="disabled">Africa</option>
<option value="63210">Egypt</option>
<option value="78700">South Africa</option>
<option disabled="disabled">Asia</option>
<option value="66137">China</option>
<option value="49932">India</option>
<option value="40308">Israel</option>
<option value="78709">Japan</option>
<option value="223252">Philippines</option>
<option value="60438">Republic of Korea</option>
<option value="69605">Singapore</option>
<option value="70391">Taiwan</option>
<option value="71971">Thailand</option>
<option disabled="disabled">Europe</option>
<option value="32142">Austria</option>
<option value="32146">Belgium</option>
<option value="78707">Bulgaria</option>
<option value="72763">Czech Republic</option>
<option value="31277">Denmark</option>
<option value="62393">Finland</option>
<option value="31223">France</option>
<option value="31026">Germany</option>
<option value="63208">Greece</option>
<option value="63016">Hungary</option>
<option value="183519">Ireland</option>
<option value="122492">Italy</option>
<option value="142792">Lithuania</option>
<option value="162411">Netherlands</option>
<option value="67164">Norway</option>
<option value="56320">Poland</option>
<option value="62344">Portugal</option>
<option value="67210">Romania</option>
<option value="68895">Russian Federation</option>
<option value="143380">Serbia</option>
<option value="78705">Slovakia</option>
<option value="25372">Spain</option>
<option value="25520">Sweden</option>
<option value="25408">Switzerland</option>
<option value="67375">Turkey</option>
<option value="63212">Ukraine</option>
<option value="25482">United Kingdom</option>
</select></div>

[right]
[/one_half_last]
<input type="submit" value="Submit" />

</form>

This is my template php file that I wrote the shortcode in. They are defined in left_shortcode and right_shortcode. The file is based off my theme's page.php:

<?php
/*
Template Name: Deven Template
*/
?>

<?php
/**
 * The template for displaying pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of paages and that
 * other "pages" on your WordPress site will use a different template.
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */

get_header(); ?>
<?php
    $left = "";
    $right = "";
?>

<?php
    if(isset($_GET['dropdownl']) and isset($_GET['dropdownr'])) {
        global $left, $right;

        $left = $_GET['dropdownl'];
        $right = $_GET['dropdownr'];
        //echo $left;
    }
?>


<?php
function left_shortcode( $atts, $content = null )  {
    global $left; // if $unique is global var add this line too
    return $left;
}

add_shortcode( 'left', 'left_shortcode' );
?>

<?php
function right_shortcode( $atts, $content = null )  {
    global $right; // if $unique is global var add this line too
    return $right;
}

add_shortcode( 'right', 'right_shortcode' );
?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        // Start the loop.
        while ( have_posts() ) :
            the_post();

            // Include the page content template.
            get_template_part( 'template-parts/content', 'page' );

            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) {
                comments_template();
            }

            // End of the loop.
        endwhile;
        ?>

    </main><!-- .site-main -->

    <?php get_sidebar( 'content-bottom' ); ?>

</div><!-- .content-area -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Upvotes: 1

Views: 2283

Answers (1)

Deven Kumar
Deven Kumar

Reputation: 53

This answer was given to me on reddit:

"It may be possible to get nested shortcode to work, but you would probably need to process the content twice or something and I would imagine it isn't going to be straightforward.

I think the easiest thing to do would just create a new shortcode as a wrapper and call the original shortcode in it.

So, if the original looks like this: [original_shortcode page=1]

You could create one like this: [original_shortcode_new page_alignment='left']

Then write a shortcode, escape/validate user input, and call the main shortcode.

Essentially:

function original_shortcode_new( $args ) {

    $args = shortcode_atts( 
        array(
            'page_alignment' => ''
        ), $args, 'original_shortcode_new' );


    $page_id = '';

    if($args['page_alignment'] == 'left' && isset($_GET['dropdownl'])){
        $page_id = intval($_GET['dropdownl']);
    } else if($args['page_alignment'] == 'right' && isset($_GET['dropdownr'])){
        $page_id = intval($_GET['dropdownr']);
    }

    if(empty($page_id)){
        return "<p>Error, bad page</p>";
    }

    return do_shortcode("[original_shortcode page={$page_id}]");

}

add_shortcode( 'original_shortcode_new', 'original_shortcode_new' );

Again, when doing the above, I would get rid of the extra shortcodes and globals and just use some logic inside a single shortcode."

This is the part of the answer that answers my question above, but to see the full post click here: Reddit Question

Upvotes: 3

Related Questions