0

I haven't made any meta-box's in quite some time so quickly threw together this one for a website I'm developing.

My problem is that when I input some text in the box's and hit save the text disappears so I'm left with empty text box's and no saved data.

I'm guessing this is something to do with my save_post function

updated* Now the title field works but the content one doesn't.

//intialising the metabox and using the callback function



add_action( 'add_meta_boxes', 'owl_mb_create' );

function owl_mb_create() {

        add_meta_box( 'owl-meta', ' Inner Page Box', 'owl_mb_function', 'page', 'normal', 'high' );

}

?>


<?php

function owl_mb_function( $post ) {

        //retrieve the metadata values if they exist
        $owl_mb_content_one = get_post_meta( $post->ID, 'owl_mb_content_one', true);
        $owl_mb_title_one = get_post_meta( $post->ID, 'owl_mb_title_one', true);
        $owl_mb_content_two = get_post_meta( $post->ID, 'owl_mb_content_two', true);
        $owl_mb_title_two = get_post_meta( $post->ID, 'owl_mb_title_two', true);
        $owl_mb_content_three = get_post_meta( $post->ID, 'owl_mb_content_three', true);
        $owl_mb_title_three = get_post_meta( $post->ID, 'owl_mb_title_three', true);

        echo 'Please fill in the forms below';
?>

<p>title:<input type="text" name="owl_mb_title_one" value=" <?php echo esc_attr( $owl_mb_title_one ); ?>" /></p>
<p>content:</p><textarea cols="1" rows="8" name="owl_mb_content_one" id="excerpt"value=" <?php echo esc_attr( $owl_mb_content_one ); ?>"></textarea>
<br />
<p>title:<input type="text" name="owl_mb_title_two" value=" <?php echo esc_attr( $owl_mb_title_two ); ?>" /></p>
<p>content:</p><textarea cols="1" rows="8" name="owl_mb_content_two" id="excerpt"value=" <?php echo esc_attr( $owl_mb_content_two ); ?>"></textarea>
<br />
<p>title:<input type="text" name="owl_mb_title_three" value=" <?php echo esc_attr( $owl_mb_title_three ); ?>" /></p>
<p>content:</p><textarea cols="1" rows="8" name="owl_mb_content_three" id="excerpt"value=" <?php echo esc_attr( $owl_mb_content_three ); ?>"></textarea>


<?php } ?>


<?php

//hook to the save the meta box data

add_action( 'save_post', 'owl_mb_save_meta' );

        function owl_mb_save_meta ( $post_id ) {

                        //save the metadata

                update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_one'] ) );
                update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_one'] ) );
                update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_two'] ) );
                update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_two'] ) );
                update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_three'] ) );
                update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_three'] ) );

        }

?>

Many Thanks :)

5
  • Take a look at your database using phpmyadmin see if the meta was actually written to the table.
    – Anagio
    Sep 9, 2013 at 13:57
  • I cudn't find it in my database, if it vanish's it wouldn't be in the database anyway would it Anagio?
    – dannyw24
    Sep 9, 2013 at 14:14
  • Post your code here please. A question should not be dependent upon a third party site.
    – s_ha_dum
    Sep 9, 2013 at 14:23
  • turn on wp_debug and try posting the meta again
    – Anagio
    Sep 9, 2013 at 14:30
  • already had that on before anagio nothing seems to appear.
    – dannyw24
    Sep 9, 2013 at 14:31

1 Answer 1

0

Look at this code:

update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_one'] ) );
update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_one'] ) );
update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_two'] ) );
update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_two'] ) );
update_post_meta( $post_id, 'owl_mb_content', strip_tags( $_POST['owl_mb_content_three'] ) );
update_post_meta( $post_id, 'owl_mb_title', strip_tags( $_POST['owl_mb_title_three'] ) );

The second value is the key. Each update overwrites the previous. If you check the database, you should see the *three values. Comment out the *three lines and you will see the *two values.

I don't know exactly what you are trying to achieve, but as written update_post_meta is overwriting your values.

Perhaps you want owl_mb_content and owl_mb_title in a serialized array, or as multiple keys but I don't know how you'd distinguish the different content that way. So, I am assuming you want three different keys.

update_post_meta( $post_id, 'owl_mb_content_one', strip_tags( $_POST['owl_mb_content_one'] ) );
update_post_meta( $post_id, 'owl_mb_title_one', strip_tags( $_POST['owl_mb_title_one'] ) );
update_post_meta( $post_id, 'owl_mb_content_two', strip_tags( $_POST['owl_mb_content_two'] ) );
update_post_meta( $post_id, 'owl_mb_title_two', strip_tags( $_POST['owl_mb_title_two'] ) );
update_post_meta( $post_id, 'owl_mb_content_three', strip_tags( $_POST['owl_mb_content_three'] ) );
update_post_meta( $post_id, 'owl_mb_title_three', strip_tags( $_POST['owl_mb_title_three'] ) );

And that matches how you are trying to retrieve the values.

4
  • Thanks s_hadum, I'm trying to pull in 3 different pieces of content and a title. I'm sure this can be done using an array and some case/switch loop.
    – dannyw24
    Sep 9, 2013 at 14:46
  • Yes, there are more elegant ways to do this, certainly. I'd lean toward an array and a switch. The above identifies the problem and the code does work.
    – s_ha_dum
    Sep 9, 2013 at 14:56
  • Many Thanks hadum, I'm on a very tight timescale so had to try and minimize errors I guess :D still a novice at some parts of php so thanks again!
    – dannyw24
    Sep 9, 2013 at 15:05
  • the content one doesn't seem to work for some reason s_hadum?
    – dannyw24
    Sep 9, 2013 at 15:17

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.