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 created a meta box I notice that save_post seems to be triggered when I land on the page, thus giving me errors like Undefined index: xxx in /data/www/ae/wp-content/themes/xx/functions.php on line 121

add_action('save_post', function($id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!isset($_POST) || !is_array($_POST)) {
        return;
    }
    update_post_meta($id, 'xxx', sprintf('%f', $_POST['xxx']));
    ...
});
share|improve this question

1 Answer 1

up vote 2 down vote accepted

See Why does save_post action fire when creating a new post?

You're getting the error as you need to check for the existence of $_POST['xxx'], rather than just checking if $_POST is set (it will always be set & an empty array by default).

add_action('save_post', function($id) {
    if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || !isset( $_POST['xxx'] ) )
        return;

    update_post_meta( $id, 'xxx', sprintf( '%f', $_POST['xxx'] ) );
    // run further code
});
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.