0

I was facing an issue (a custom metabox not saving), and after 2 hours I finally realised that, if you try to add an action to the save_post hook inside an if statement checking whether $_GET['post'] is set, it won't work.

Basically, even if $_GET['post'] is set, if you put this code in your functions.php, it won't work:

function foo() {
    die('Saving post');
}
if( isset($_GET['post']) ) {
    add_action( 'save_post', 'foo' );
}

I tried to reproduce this with other hooks, but save_post seems to be the only one that behaves like this.

I wonder why this happens?

0

How is $_GET['post'] set ? And why are you checking it?

It could be that you're adding the save_post call too late.

Try:

function foo() {
    die('Saving post');
}

function foo2() {
    if( isset($_GET['post']) ) 
        foo();
}
add_action( 'save_post', 'foo2' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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