• Hello everyone, I’m developing a plugin that calls wp_insert_post(), but the plugin execution is locked in the call to wp_insert_post().

    This is the snippet of code I’m running:

    $new_page = array(
    'post_type' => 'post',
    'post_title' => 'Test Page Title',
    'post_content' => 'Test Page Content',
    'post_status' => 'draft',
    'post_author' => get_current_user_id(),
    );
    echo "PUBLISHING 2.....<br>";

    try {
    $new_page_id = wp_insert_post($new_page, true, false);
    echo "PUBLISHING 3: id=$new_page_id.....<br>";
    } catch (Exception $ex) {
    echo "EXCEPTION..... $ex<br>";
    }

    This is the output:

    PUBLISHING 1.....
    PUBLISHING 2.....

    The post is published, as I see it in post list, but the execution is stopped, without giving any feedback from the function and without throwing any exception.

    Anyone knows how to fix it?

    I’m available for providing futher info. I’m running it on a local WordPress installation.

    Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    What do you mean by “the plugin execution is locked”? How is this snippet called to start with? Do you mean it’s hooked to an action fired by wp_insert_post()? If so, you might be getting stuck in an infinite loop where your callback is indirectly recursively calling itself. To prevent this your callback needs to remove itself from the action call stack so it cannot re-execute when it calls wp_insert_post() for itself.

    You should call the removal function first thing straight away because it takes time for removal to be processed. If you wait until the end for removal your callback will be called again before removal can be completed.

    Thread Starter Riccardo79

    (@riccardo79)

    Hello @bcworkz, thanks for your reply.

    This snippet of code is called by a click on a button in a custom page of WP dashboard, that fires its execution (and should reload the same page). but when it’s executed, the web page shows only:

    PUBLISHING 1.....
    PUBLISHING 2.....

    and nothing else.

    It seems that the execution stops there (as I don’t see the “PUBLISHING 3” string), but the post is correctly created.

    The code is not in a callback, it’s in the plugin code and runs when the dashboard page is loading.

    I’ll try to call the removal function at the beginning of my code an try to see if this helps.

    Thanks in advance

    • This reply was modified 5 months, 2 weeks ago by Riccardo79.
    • This reply was modified 5 months, 2 weeks ago by Riccardo79.
    Moderator bcworkz

    (@bcworkz)

    Plugin on-load code should not generate any output. I suggest instead you error_log() any debug messaging.

    Also, calling wp_inset_post() that early is likely a problem. You should wait until the “init” action to do something like that. Plugin on-load code generally consists of only add_action() and add_filter() calls to set up things to execute later

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.