最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Input select option Change Content in Metabox

matteradmin10PV0评论

I'm making a "Input select option" in Custom Metabox for my plugin, I'm not Expert in PHP I don;t know How to Save user "Input select option". Working perfectly plain PHP.

public function zon_featuress_boxx( $post ) {
    wp_nonce_field( 'zonpackk_testimonial', 'zonpackk_testimonial_nonce' );
        $data = get_post_meta( $post->ID, '_zonpackk_testimonial_key', true );
        $dataoption = isset($data['dataoption']) ? $data['dataoption'] : '';
        ?>

<?php
if (isset($_POST['dataoption'])) {
    $page = $_POST['dataoption'];
    $_SESSION['dataoption'] = $page;
} else {
    $page = $_SESSION['dataoption'];
}
?>

    <form name="myform" action="" method="post">
        <select name="dataoption" onchange="this.form.submit()">
            <option value="page1"<?php if ($page == "page1") {
                echo " selected";
            } ?>>Page 1
            </option>
            <option value="page2"<?php if ($page == "page2") {
                echo " selected";
            } ?>>Page 2
            </option>
        </select>
    </form>

<?php
switch ($page) {
    case 'page2':
        echo "string2";
        break;
    case 'page1':
        echo "string1";
        break;
}
?>

I'm making a "Input select option" in Custom Metabox for my plugin, I'm not Expert in PHP I don;t know How to Save user "Input select option". Working perfectly plain PHP.

public function zon_featuress_boxx( $post ) {
    wp_nonce_field( 'zonpackk_testimonial', 'zonpackk_testimonial_nonce' );
        $data = get_post_meta( $post->ID, '_zonpackk_testimonial_key', true );
        $dataoption = isset($data['dataoption']) ? $data['dataoption'] : '';
        ?>

<?php
if (isset($_POST['dataoption'])) {
    $page = $_POST['dataoption'];
    $_SESSION['dataoption'] = $page;
} else {
    $page = $_SESSION['dataoption'];
}
?>

    <form name="myform" action="" method="post">
        <select name="dataoption" onchange="this.form.submit()">
            <option value="page1"<?php if ($page == "page1") {
                echo " selected";
            } ?>>Page 1
            </option>
            <option value="page2"<?php if ($page == "page2") {
                echo " selected";
            } ?>>Page 2
            </option>
        </select>
    </form>

<?php
switch ($page) {
    case 'page2':
        echo "string2";
        break;
    case 'page1':
        echo "string1";
        break;
}
?>
Share Improve this question edited Dec 27, 2018 at 9:21 Noufal Binu asked Dec 27, 2018 at 9:00 Noufal BinuNoufal Binu 2713 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is totally wrong. Metaboxes are shown on post/page editing screen, so if you submit your own form, the whole page will be submitted/refreshed. Look up what a metabox is here => SO Thread

You should use WP's own functions to deal with this. Look add_meta_box() and check this tutorial to know how you should think to this code. The example on the tut. page shows exactly what you need.

code from WP dev

function wporg_add_custom_box()
{
    $screens = ['post', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Custom Meta Box Title',  // Box title
            'wporg_custom_box_html',  // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}
add_action('add_meta_boxes', 'wporg_add_custom_box');

function wporg_custom_box_html($post)
{
    $value = get_post_meta($post->ID, '_wporg_meta_key', true);
    ?>
    <label for="wporg_field">Description for this field</label>
    <select name="wporg_field" id="wporg_field" class="postbox">
        <option value="">Select something...</option>
        <option value="something" <?php selected($value, 'something'); ?>>Something</option>
        <option value="else" <?php selected($value, 'else'); ?>>Else</option>
    </select>
    <?php
}

To save your field you need to catch it when the post/page is saved by the user, hence when the save_post action is fired.

function wporg_save_postdata($post_id)
{
    if (array_key_exists('wporg_field', $_POST)) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far