0

I dont know where i am going wrong.I want to save the values of my radio buttons in custom meta box.Here is my code.

<?php

    if (!function_exists('custom_meta_box')) {
        function custom_meta_box(){
            add_meta_box(
                'custom-meta-box',
                __('Post Options', 'spectacularpixels'),
                'post_options_callback',
                'post',
                'normal',
                'high'              
            );
        }
    }

    if (!function_exists('post_options_callback')) {
        function post_options_callback($post_id){

                wp_nonce_field( 'action_layout_nonce', 'name_layout_nonce' );
                $value = get_post_meta( $post_id, 'my_key', true );

            ?>
                <label for="layout-none">None</label>
                <input type="radio" id="layout-none" name="layout" value="layout-none" <?php checked($value, 'layout-none') ?>>
                <label for="layout-left">Left</label>
                <input type="radio" id="layout-left" name="layout" value="layout-left" <?php checked($value, 'layout-left') ?>>
                <label for="layout-right">Right</label>
                <input type="radio" id="layout-right" name="layout" value="layout-right" <?php checked($value, 'layout-right') ?>>

            <?php
        }
    }

    if (!function_exists('save_meta_data')) {
        function save_meta_data($post_id){

            // Bail if we're doing an auto save
            if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

            // if our nonce isn't there, or we can't verify it, bail
            if( !isset( $_POST['name_layout_nonce'] ) || !wp_verify_nonce( $_POST['name_layout_nonce'], 'action_layout_nonce' ) ) {echo "nonce error";};

            // if our current user can't edit this post, bail
            if( !current_user_can( 'edit_post' ) ) return;

            update_post_meta($post_id, 'my_key', $_POST['layout']);
        }
    }
    add_action('add_meta_boxes', 'custom_meta_box');
    add_action('save_post', 'save_meta_data');
?>
0

Your values are actually being saved - you can always check it in your database.

The problem is in your metabox callback function post_options_callback - it is not being called with Post.ID value as an argument, but WP Post object is being passed. Here's revisited piece of code:

 function post_options_callback( $post ) {
      $post_id = $post->ID;

I also recommend you to not wrap your function with function_exists check, but prefix them with unique prefix. Eg: my_plugin_ (replacing it with your plugin name) or binda_ (replacing it with you nickname or name).

That way you can be sure that your plugin won't stop working when you install some other plugin defining a function with same name.

Another approach would be to encapsulate your plugin function into a class.

Note: use unique meta_key names as well (prefix them).

0

Replace your code with:

echo ($value[0] == 'layout-none')? 'checked="checked"':'';
echo ($value[0] == 'layout-left')? 'checked="checked"':'';
echo ($value[0] == 'layout-right')? 'checked="checked"':'';

Complete Updated code:

<label for="layout-none">None</label>
                <input type="radio" id="layout-none" name="layout" value="layout-none" <?php echo ($value[0] == 'layout-none')? 'checked="checked"':''; ?>>
                <label for="layout-left">Left</label>
                <input type="radio" id="layout-left" name="layout" value="layout-left" <?php echo ($value[0] == 'layout-left')? 'checked="checked"':''; ?>>
                <label for="layout-right">Right</label>
                <input type="radio" id="layout-right" name="layout" value="layout-right" <?php echo ($value[0] == 'layout-right')? 'checked="checked"':''; ?>>

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.