0

I built a plugin for my company, which basically goes to tube sites, scrapes them for content, and posts them automatically in whatever state is defined (publish, draft, future, etc).

Now, everything goes well when I manually add a post through the plugin: it opens a cURL to the sites, grabs the videos and embeds, takes thumbnails, posts titles, flawless. however, when I try to run it from the cron job using

add_action('timelyTube', 'timelyPost');

and on the activation hook:

wp_schedule_event(time(), 'hourly', 'timelyTube');

it works in the sense that it fires the function timelyPost(), but when it goes to grab the video, it's not able to do so, it grabs the thumbnails & titles, but not the embeds, meaning that the cURL is working, but something gets messed up in the process.

if anyone has any idea about what to do, I'll be grateful, Thanks, Itai.

1

I just encountered this very same problem. I was trying to do wp_insert_post() from within a cron action, and the script would apparently just die during some save_post action handler. I tracked down my problem, which turned out to be a save_post handler which is used to save the POST data from a custom metabox. Within this handler, it calls check_admin_referer( 'myplugin_save', 'myplugin_nonce') as a security check. This function very annoyingly calls wp_die() if the security check fails, and as a result the execution just stops. So by adding a check to short-circuit the function if DOING_CRON then everything started working as expected. See:

<?php
function myplugin_metabox_save(){
    // If doing wp_insert_post() during a cron, the check_admin_referer() below
    // will always fail and we know the metabox wasn't shown anyway, so abort.
    if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ){
        return;
    }

    // This is to get around the problem of what happens when clicking the "Add Post"
    // link on the admin menu when browsing the main site.
    if( $_SERVER['REQUEST_METHOD'] != 'POST' ){
        return false;
    }

    // Verify this came from the our screen and with proper authorization
    if ( !check_admin_referer( 'myplugin_save', 'myplugin_nonce') ) {
        return false;
    }

    // ...
    // Now handle $_POST fields and save them as postmeta for example
}
add_action( 'save_post', 'myplugin_metabox_save' );
0

Well, I've found my own solution, thanks weston, but I believe mine is a bit easier :)

from the function which calls to insert post, you do the following for simplicity's sake:

kses_remove_filters();
wp_insert_post( $post );
kses_init_filters();

And voila! there's a new post.

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.