0

I'm having a strange problem with same code not working on localhost vs production server.

The code is simple and I am trying to save a custom post type, but none of the data in my metaboxes are saved. I have some echo statements in my save_custom_type for debugging to see how the data looks like. Not even those print statements are echoed when I press "Publish" on production environment.

The custom post type is created in my theme, I hook into the action 'init'.

Code for creation and first lines up to the first echo statement in the save method.

function create_omgang_post_type() {
    $labels = array(
            'name' => 'Omgång',
            'add_new' => 'Lägg till ny omgång',
            'add_new_item' => 'Lägg till ny omgång',
            'not_found' => 'Inga omgångar inlagda'
            );
    $args = array(
            'description' => 'En omgång för denna säsong',
            'public' => true,
            'hierarchical' => true,
            'show_in_menu' => false,
            'show_in_admin_bar' => true,
            'label'=>'Omgångar',
            'labels' => $labels,
            'supports' => array(
                    'title', 'page-attributes') 
            );
    register_post_type('omgang', $args); 
}
add_action('init', 'create_omgang_post_type'); 

/*-------------------------------------*/

function save_custom_post_omgang($post_id) {
    $slug = 'omgang';
    $post = get_post($post_id); 

    if($post->post_type == "omgang" ) {
        if(! $_POST)
            return;

        if(!wp_post_revision_title($post_id)) { 
            $lag = get_all_lag(); 

            $data = array();
            foreach($_POST as $key => $value) {
                //key#type
                $explodedID = explode('#', $key);

                if($explodedID[0] != $key) {
                    $key = $explodedID[0];
                    $type = $explodedID[1];

                    $data[$key][$type] = $value;
                }
            }
            print_r($data); //this line is not even printed in PRD
        }
    }
}

1 Answer 1

1

The issue with meta boxes and saving in general with Wordpress is that is is really annoying to debug. This is because of how Wordpress does it.

Basically, when you press Publish, Wordpress sends an AJAX request to save the post. This is what is calling your save handlers. And when this request returns, it has a Location header in it that tells Wordpress what page to redirect to (usually one that displays a message like 'Post Updated').

The issue with this is that it just throws any debug statement into oblivion. It makes it absolutely horrendous to debug meta boxes because you never actually see the output during the save routine.

The easiest way to get around this is do put an exit; statement right after your debug message. Yes, the entire page won't run, but you will be able to see your debug output.


The issue right now I see with your code is you never actually call update_post_meta or add_post_meta. Without using one of those functions to save the data (and then automatically filling in textboxes later with get_post_meta), you will never save your data.

6
  • Hey! +1 for the exit; when debugging meta boxes!
    – brasofilo
    Commented Jul 5, 2012 at 1:06
  • 2
    Even better than exit is wp_die().
    – Chris_O
    Commented Jul 5, 2012 at 1:37
  • @Chris_O I wouldn't use wp_die as it clutters up the output, and for a developer it doesn't really matter if the returned page is prettiful. Commented Jul 5, 2012 at 18:37
  • It clutters up the output?? What does that mean?
    – Chris_O
    Commented Jul 5, 2012 at 20:40
  • @Chris_O It adds in a bunch of HTML to make the resulting page look nice, which isn't needed if you are trying to debug. Commented Jul 5, 2012 at 21:19

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.