admin管理员组

文章数量:1312751

I have a page with a shortcode that displays a dropdown. I just can't figure out how to return that value to a function/page to show the resulting information. The second page would take that value and show a list. I've tried everything I can think of. Any suggestion will be appreciated. Sample code would be wonderful. Thank you,

I have a page with a shortcode that displays a dropdown. I just can't figure out how to return that value to a function/page to show the resulting information. The second page would take that value and show a list. I've tried everything I can think of. Any suggestion will be appreciated. Sample code would be wonderful. Thank you,

Share Improve this question asked Dec 31, 2020 at 0:52 BudBud 438 bronze badges 1
  • Thank you. That was so helpful. – Bud Commented Dec 31, 2020 at 18:51
Add a comment  | 

1 Answer 1

Reset to default 0

There are plenty of ways this can be done. Here is one rudimentary version of doing this using the GET method and two different shortcodes for the two pages. You have to place the code in your theme's functions.php and to create two different pages and place [first-page] shortcode on the first page, and [second-page] shortcode on the second page and use the form on the first page.

<?php

/**
 * Shortcode for the First page.
 * 
 * Show form on the first page to grab information.
 */
function wpse380676_first_page_shortcode() {
    $second_page_id = 1832;

    ob_start();
    ?>

    <form action="<?php echo get_the_permalink($second_page_id) ?>" method="GET">
        <label><input type="radio" name="question" value="yes"> Yes</label>
        <label><input type="radio" name="question" value="no"> No</label>
        <button type="submit">Submit</button>
    </form>

    <?php
    echo ob_get_clean();
}

add_shortcode( 'first-page', 'wpse380676_first_page_shortcode' );

/**
 * Shortcode for the Second page.
 * 
 * Handle data from the previous page and do something.
 */
function wpse380676_second_page_shortcode() {
    $question = filter_input(INPUT_GET, 'question', FILTER_SANITIZE_STRING);

    if (!$question) {
        return;
    }

    if ('yes' === $question) {
        echo 'The answer is Yes';
    } else {
        echo 'The answer is No';
    }
}

add_shortcode( 'second-page', 'wpse380676_second_page_shortcode' );

Tested in WordPress 5.6 with the default theme 'Twenty Twenty One'

本文标签: From a shortcode I want to pass a value to display a different page