Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I create metaboxes but i can´t save data and also no show data insert into field , this is my code

    <?php
function reaction_buttons_meta2() 
{
global $post;
echo $_POST['reaction_buttons_off2'];
update_post_meta($post->ID, 'reaction_buttons_off2', $_POST['reaction_buttons_off2']);
$meta=get_post_meta($post->ID, $field['reaction_buttons_off2'], true); 
?>
<input type="text" id="reaction_buttons_off2" name="reaction_buttons_off2" value="<?php echo $meta[reaction_buttons_off2][0]; ?>">
<?php
}
?>
<?php
function reaction_buttons_meta_box2() 
{
add_meta_box('reaction_buttons2','Reaction Buttons','reaction_buttons_meta2','post','side');
add_meta_box('reaction_buttons2','Reaction Buttons','reaction_buttons_meta2','page','side');
}
add_action('admin_menu', 'reaction_buttons_meta_box2');
?>
share|improve this question
    
Have you read wordpress.stackexchange.com/questions/47828/…? –  toscho May 12 '13 at 19:53
    
I change the code and contionue no work me –  Fran May 12 '13 at 22:02
    
Please update your question with your changed code. –  toscho May 12 '13 at 22:25

1 Answer 1

Your code:

add_action('admin_menu', 'reaction_buttons_meta_box2');

Use 'add_meta_boxes' to add meta boxes. It fires in the wp-admin/edit-form-advanced.php file and passes the $post object. The 'admin_menu' action fires much more often.

add_action( 'add_meta_boxes', 'reaction_buttons_meta_box2' );

Your code:

function reaction_buttons_meta2()
{
global $post;
echo $_POST['reaction_buttons_off2'];

There is nothing in $_POST when the the edit page loads. You can test this with wp_die().

add_action( 'add_meta_boxes', 'reaction_buttons_meta_box2' );

function reaction_buttons_meta_box2() {
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'post', 'side' );
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'page', 'side' );
}

function reaction_buttons_meta2() {
    wp_die( '<pre>' . var_export( $_POST, true ) . '</pre>' );
}

In the meta box you should see this:

array(
)

Which means nothing is in $_POST.

Your code:

update_post_meta($post->ID, 'reaction_buttons_off2', $_POST['reaction_buttons_off2']);

reaction_buttons_meta_box2() is not run during post / page save. To add a function on save use this:

add_action( 'add_meta_boxes', 'reaction_buttons_meta_box2' );
add_action( 'save_post', 'wpse_47828_reaction_buttons_meta_box2_save' );

function wpse_47828_reaction_buttons_meta_box2_save() {
    wp_die( '<pre>' . var_export( $_POST['reaction_buttons_off2'], true ) . '</pre>' );
}

function reaction_buttons_meta2() {
    printf( '<input type="text" id="%1$s" name="%1$s" value="%2$s" />', 'reaction_buttons_off2', 12 );
}

function reaction_buttons_meta_box2() {
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'post', 'side' );
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'page', 'side' );
}

Refresh the editor page and Update the post. The web page should produce an error with '12' in it. The value added in the printf line above.

Your code:

$meta=get_post_meta($post->ID, $field['reaction_buttons_off2'], true);

This code used a variable named $field which has never been defined. It should produce a PHP error.

Your code:

<input type="text" id="reaction_buttons_off2" name="reaction_buttons_off2" value="<?php echo $meta[reaction_buttons_off2][0]; ?>">

This code uses an array key named reaction_buttons_off2 without quotes. Since $field didn't exist in the line above, $meta has nothing in it and the corrected code $meta['reaction_buttons_off2'][0] should produce a PHP missing index warning.

reaction_buttons_meta2() should probably look like this:

function reaction_buttons_meta2( $post ) {

    $field_name = 'reaction_buttons_off2';

    if ( get_post_meta( $post->ID, $field_name, true ) )
        $reaction_buttons_off2 = get_post_meta( $post->ID, $field_name, true );
    else
        $reaction_buttons_off2 = '';

    printf( '<input type="text" id="%1$s" name="%1$s" value="%2$s" />', $field_name, $reaction_buttons_off2 );
}

Saving the data requires some tests:

function wpse_47828_reaction_buttons_meta_box2_save( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( current_user_can( 'edit_post', $post_id ) || current_user_can( 'edit_pages', $post_id ) ) {

        $field_name = '_reaction_buttons_off2';

        if ( isset( $_POST[$field_name] ) )
            update_post_meta( $post_id, $field_name, $_POST[$field_name] );
    }
}

The field name, reaction_buttons_off2 will show up as a custom field and can be edited there as well. If you wish to avoid that add an underscore to the beginning of the field name in both the wpse_47828_reaction_buttons_meta_box2_save() and in reaction_buttons_meta2().

$field_name = '_reaction_buttons_off2';

Putting it all together:

add_action( 'add_meta_boxes', 'reaction_buttons_meta_box2' );
add_action( 'save_post', 'wpse_47828_reaction_buttons_meta_box2_save' );

function reaction_buttons_meta_box2() {
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'post', 'side' );
    add_meta_box( 'reaction_buttons2', 'Reaction Buttons', 'reaction_buttons_meta2', 'page', 'side' );
}

function reaction_buttons_meta2( $post ) {

    $field_name = '_reaction_buttons_off2';

    if ( get_post_meta( $post->ID, $field_name, true ) )
        $reaction_buttons_off2 = get_post_meta( $post->ID, $field_name, true );
    else
        $reaction_buttons_off2 = '';

    printf( '<input type="text" id="%1$s" name="%1$s" value="%2$s" />', $field_name, $reaction_buttons_off2 );
}

function wpse_47828_reaction_buttons_meta_box2_save( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( current_user_can( 'edit_post', $post_id ) || current_user_can( 'edit_pages', $post_id ) ) {

        $field_name = '_reaction_buttons_off2';

        if ( isset( $_POST[$field_name] ) )
            update_post_meta( $post_id, $field_name, $_POST[$field_name] );
    }
}

Note: You should add some validation to the field during save and it may be a good idea to also add a nonce to check that this form really came from WordPress admin. I don't know if current_user_can() will be secure enough.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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