0

I was playing with metaboxes following one tutorial. I came to a point where I could add any code and it would be displayed in the metabox. I tried echoing something and it was ok. Then I wanted to set a select form that store user's choice.

 <form name="ss_on_off_form_submit" method="post" action="">
    <input type="hidden" name="ss_on_off_form_submit" value="Y">
    <select name="ss_on_off" id="ss_on_off">
        <option value="Off">Off</option>
        <option value="On">On</option>
    </select>
    <p>
        <input class="button-primary" type="submit" name="ss_on_off_submit" value="Save" />
    </p>
</form>
<?php
if( isset( $_POST['ss_on_off_form_submitt'] ) ) {
    echo '7.<hr />';} 

The same code used in plain php environment is working as expected. I am sorry to bother the community with such easy question, but why the metabox doesn't render php as supposed?

1 Answer 1

0

The whole page/post/custom post type post screen is a big form. That means that whenever you hit "publish" on a post type edit screen, the whole form - and that means every content on the page - gets processed and WordPress executes everything. And that "everything" includes all kinds of hooks that you should use to save, sanitize, etc. your meta box contents. Use to following plugin to give it a try:

<?php
/* Plugin Name: WPSE (#168617) Dump $_POST on post edit screen */

add_action( 'save_post', function()
{
    # Uncomment to see $_POST data
    // exit( var_dump( $_POST ) );

    // Save as Post Meta Data
    update_post_meta();
} );

For more info on post meta data handling, see update_post_meta() and the Related Section in Codex.

0

Your Answer

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

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