0

I've just created a metabox for some custom text on a website I'm developing.

However when I input something into it and click update it doesn't seem to save it. Also its not saving on the MYSQL DB either.

here is the code for my metabox below.

<?php

add_action( 'add_meta_boxes', 'dd_hctb_create' );

function dd_hctb_create() {

add_meta_box ( 'dd_meta', 'Heading Content Text Box', 'dd_hctb_function', 'page', 'normal', 'high' );

}

function dd_hctb_function( $post ) {

echo '<input type="hidden" name="dd_mbe_content_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

//retrieve the metadata values if they exist

$dd_mbe_content = get_post_meta( $post->ID, 'dd_mbe_content', true );

echo 'Write any content in here that you want to appear above the main text.';

echo '<textarea name="middle" id="dd_mbe_content" value="" cols="100%" rows="10">'.$dd_mbe_content.'</textarea>';

}

function dd_hctb_save($post_id) {


    //attempt to verify nonce
    if (!wp_verify_nonce($_POST['dd_mbe_content_nonce'], basename(__FILE__)))
        return $post_id;
    // check the autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id; 
       // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id; 
        }
    //finally verify the postmeta is set

            if (isset ( $_POST['dd_mbe_content'] ) ) {
                //now save if if the data exists
                update_post_meta( $post_id, 'dd_mbe_content', strip_tags( $_POST['dd_mbe_content']));
            }

    }  // end save function

 add_action('save_post', 'dd_hctb_save' );

?>

thanks for any help guys - I'm sure this is a typo somewhere or something overriding a setting. I'm new to using wp_nonce aswell so any help or advice is taken on board.

1 Answer 1

2

I optimized your function.

<?php
function dd_hctb_create() {
    add_meta_box ('dd_meta', 'Heading Content Text Box', 'dd_hctb_cb', 'page', 'normal', 'high');
} // function dd_hctb_create
add_action('add_meta_boxes', 'dd_hctb_create');

function dd_hctb_cb($post) {
    wp_nonce_field(basename(__FILE__), 'dd_mbe_content_nonce');

    //retrieve the metadata values if they exist
    $value = get_post_meta($post->ID, 'dd_mbe_content', true);
    ?>
    <p>
        Write any content in here that you want to appear above the main text.
    </p>
    <textarea id="dd_mbe_content" name="dd_mbe_content" cols="100%" rows="10"><?php echo wp_kses_post($value); ?></textarea>
    <?php
} // function dd_hctb_cb

function dd_hctb_save($id, $post) {
    // check the autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $id; 

    if (
        'page' == $post->post_type
        && current_user_can('edit_page', $id)
        && wp_verify_nonce($_POST['dd_mbe_content_nonce'], basename(__FILE__))
        && isset($_POST['dd_mbe_content'])
    ) update_post_meta($id, 'dd_mbe_content', strip_tags($_POST['dd_mbe_content']));
} // function dd_hctb_save
add_action('save_post', 'dd_hctb_save', 1, 2);

The problem was that your textarea was given id="dd_mbe_content" and name="middle", which took precedence.

6
  • Oh thanks so much tf! Just another quick question seen as you seem very knowledgeable have i done the whole nonce thing correct and checked permissions correctly? Thanks again!
    – Dannyw24
    Sep 27, 2013 at 23:35
  • Well, basically, yes, you did. I just used the wp_nonce_field function instead of your input field containing a wp_create_nonce. The rest looks fine to me.
    – tfrommen
    Sep 27, 2013 at 23:37
  • great! i noticed you used wp_kses_post does this just allow for content to be pulled in from the relevant post type?
    – Dannyw24
    Sep 27, 2013 at 23:39
  • I don't know what you will be using the textarea for. I just used wp_kses_post as some sanitizer function, which allows for all the HTML tags also allowed for standard posts. Of course, you could use any other means to sanitize.
    – tfrommen
    Sep 27, 2013 at 23:41
  • Oh - I thought i sanatized the data with the strip tags function
    – Dannyw24
    Sep 27, 2013 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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