1

I have a website which the post's contents are dynamically generated AFTER a post is published.

I'm using this code to generate the content i want:

add_action( 'publish_post', 'generate_content'); function generate_content($post){ //some code here }

This process can sometimes take up to 5 minutes, while the post is instantly published (I already set the php timeout to 600 seconds).

I want to schedule the post for when the function has finished it's task, or to save the post as draft and automatically publish it when it's ready.

Is there a way to achieve this? Any help is appreciated.

  • How do you create the post? There's a lot of missing code here, as well as context as to why this is how it's set up, please update your question using the edit link – Tom J Nowell Nov 19 '16 at 14:12
1

There might be two ways:

add_action( 'draft_post', 'wpse_246730_my_function' );
function wpse_246730_my_function( $post_id, $post )
{
    // Do your things

    // Just to stay safe
    remove_action( 'draft_post', 'wpse_246730_my_function' );
    wp_publish_post( $post_id );
    add_action( 'draft_post', 'wpse_246730_my_function' );
}

Or make the post future status, and set a time after 10 or 20 mins to publish. Then use the following code:

add_action( 'future_post', 'wpse_246730_my_function' );
function wpse_246730_my_function( $post_id, $post )
{
    // Do your things
}
0

Use wp_publish_post( $post_id) to change the post's status. And consider another hook like 'save_post' instead of 'publish_post' to kick off the process.

  • I can save the post as draft, and i can do so as you said to prevent the publish. but how can i have the post being automatically published when the process is finished? (I mean change it's status from Draft to Published automatically) – Jack Johansson Nov 19 '16 at 17:43
  • Depends on what your process is. But at the end, if you have the post_id just run wp_publish_post( $post_id) . Without seeing how you finish a process it's impossible to tell you how to invoke that function. – jgraup Nov 19 '16 at 17:46
  • The process launches a loop which i do some modification on the post's thumbnail. Adding watermark, resizing, making exclusive thumbnails and then updating the post's meta at the ending line. so i can wp_publish_post( $post_id) to publish the post, but the problem is how to automatically run this after a post is actually Saved as draft. – Jack Johansson Nov 19 '16 at 17:50
  • There is a save_post hook which is fired after a post is created, but in the codex it says whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. i'm not sure if this hook is fired after drafting though. – Jack Johansson Nov 19 '16 at 18:01
  • Could you hook draft to pending before you publish. codex.wordpress.org/Post_Status_Transitions – jgraup Nov 19 '16 at 18:13
0

This will let you run code once a post has been created and should run instantly. So if the new status of the post is publish and was any other status before like draft or no status this will run.

function some_function( $new, $old, $post ) {
    if ( ( $new == 'publish' ) && ( $old != 'publish' ) && ( $post->post_type == 'post' ) ) {
        //Run code here

    } else {
        return;
    }
}
add_action( 'transition_post_status', 'some_function', 10, 3 );

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.