I have a genuine mystery. With this code (in an ajax call):

file_put_contents(ABSPATH.'wp-content/debug.log', 'Inserting new note'  . PHP_EOL, FILE_APPEND);
        $post_id = wp_insert_post( array(
            'post_title'    => sanitize_text_field( $title ),
            'post_status'   =>  'publish',
            'post_type'     => 'coursenote',
            'post_content'  => $_POST['data']['body']
        ), true );
        file_put_contents(ABSPATH.'wp-content/debug.log', 'WTF'  . PHP_EOL, FILE_APPEND);

I am seeing the new post in the database, but the wp_insert_post function is somehow not returning (evidenced by fact that second file_put_contents is not doing anything.

I don't know if it has to do with other ajax calls or something. Just really odd that everything is fine til wp_insert_post's return.

Can anyone think of a reason why this could happen?

thanks, Brian


So I found the problem. Some really awful programmer had an exit; call in a save_post hook in a plugin. That resulted in the script not returning to the calling script (wp_insert_post).

That awful programmer is the author of this question :-) And the offensive exit is now a return

  • 1
    Have you checked your PHP error logs? I notice your code example is incomplete, not indented, and lacks context. Just because the post is in the database doesn't mean wp_insert_post finished, there's a lot of hooks etc that could have gone wrong. Also, when you say an AJAX call, are you talking about the modern REST API? Or the ancient Admin AJAX endpoint under wp-admin? – Tom J Nowell Nov 28 '19 at 18:23
  • It is admin ajax. I just noticed that wp_insert_post is not resuming after the save_post do action. SO some plugin is in an infinite loop. Need to see how to return a list of all hook actions running on save_post... – Brian Nov 28 '19 at 18:41
  • query monitor can do that, but you can also disable all plugins and enable them one by one. I'd also recommend using the REST API as it brings a lot of benefits and advantages. You may even be able to use the built in endpoints and avoid having to create an endpoint at all by adding 'show_in_rest' => true when registering the post type – Tom J Nowell Nov 28 '19 at 20:37
  • Yeah, for my site mods I am doing that and in fact I want to implement this approach: deliciousbrains.com/… – Brian Nov 29 '19 at 8:30
  • And one more testimony to why using exit() should be using sparingly or not at all if it can be avoided. – Jesús Franco Nov 29 '19 at 11:13

Your Answer

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

Browse other questions tagged or ask your own question.