0

What I want to do is grab the input from a metabox — a number to be exact — in the post editor area and save it to a custom table I have previously created.

Here it is my code for declaring the metabox for the post type only:

function silly_metabox()
{
    $post_types = array( 'post' );

    foreach( $post_types as $type )
    {
        add_meta_box(
            'silly_metabox',
            'Silly Metabox',
            'silly_display_metabox',
            $type
        );
    }
}

add_action( 'add_meta_boxes', 'silly_metabox' );

meanwhile this is where the markup for the metabox is:

function silly_display_metabox( $post )
{
    wp_nonce_field( basename( __FILE__ ), 'silly_extrnum_nonce' );

    ?>

    <label for="sillyNum">Silly Number</label>
    <input type="number" id="sillyNum" placeholder="Write a number...">

    <?php
}

and last but not least the function to save it all:

function silly_save_metabox( $post_id )
{
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );

    $is_valid_nonce = false;

    if ( isset( $_POST[ 'silly_extrnum_nonce' ] ) )
    {
        if ( wp_verify_nonce( $_POST[ 'silly_extrnum_nonce' ], basename( __FILE__ ) ) )
        {
            $is_valid_nonce = true;
        }
    }

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) return;

    if (array_key_exists( 'sillyNum', $_POST ) )
    {
        global $wpdb;
        $wpdb->insert($wpdb->prefix . 'sillydata', array(
            'post_id' => $post_id,
            'silly_number' => $_POST['sillyNum']
        ));
    }
}

add_action( 'save_post', 'silly_save_metabox' );

Putting the field sanitization aside for a minute, why the $_POST array doesn't contain the sillyNum key and the insert operation either fails or doesn't get executed?

  • 4
    Your input missing the name attribute... – Shibi Dec 18 '17 at 18:42
0

The $_POST[''] has to contain the name attribute of the input field and not the ID.

So to modify your code:

function silly_display_metabox( $post )
{
    wp_nonce_field( basename( __FILE__ ), 'silly_extrnum_nonce' );

    ?>

    <label for="sillyNum">Silly Number</label>
    <input type="number" id="sillyNum" name="sillyNum" placeholder="Write a number...">

    <?php
}

And

function silly_save_metabox( $post_id )
{
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );

    $is_valid_nonce = false;

    if ( isset( $_POST[ 'silly_extrnum_nonce' ] ) )
    {
        if ( wp_verify_nonce( $_POST[ 'silly_extrnum_nonce' ], basename( __FILE__ ) ) )
        {
            $is_valid_nonce = true;
        }
    }

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) return;

    if (array_key_exists( 'sillyNum', $_POST ) )
    {
        global $wpdb;
        $wpdb->insert($wpdb->prefix . 'sillydata', array(
            'post_id' => $post_id,
            'silly_number' => $_POST['sillyNum']
        ));
    }
}

add_action( 'save_post', 'silly_save_metabox' );

Let me know if this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.