如何在每隔60分钟安排自动售票发布?

时间:2016-04-18 作者:TheMoonAlliance

我正在使用以下代码从上传的图像中自动发布帖子,它可以工作,但不会安排帖子发布,所有帖子都是在wordpress仪表板中的媒体上传器上传图像后立即发布的。

$autopost_controler = get_theme_mod( \'auto_onoff\' );
if( $autopost_controler != \'\' ) {
    switch ( $autopost_controler ) {
        case \'on\': 

            add_action( \'add_attachment\', \'auto_post_on_image_upload\' );
            function auto_post_on_image_upload( $attachId ) {

                $attachment         = get_post($attachId);
                $image              = wp_get_attachment_image_src( $attachId, \'large\');
                $image_tag          = \'<p><img src="\'.$image[0].\'" /></p>\';
                $theoriginaltitle   = $attachment->post_title;
                $onetitle           = str_replace("-"," ",$theoriginaltitle);
                $thetitlef          = str_replace("_"," ",$onetitle);
                $thetitle           = ucwords(strtolower($thetitlef));
                $cidargs            = array( \'category_name\' => get_theme_mod( \'cat_1\' ) );
                $cid                = array( get_category_by_slug( $cidargs[\'category_name\'])->term_id );
                $category_id        = get_cat_ID( $cidargs[\'category_name\'] );
                $category_link      = get_category_link( $category_id );

                $content = \'<strong>\'. $thetitle .\'</strong> is an <strong>HD wallpaper</strong> posted in <strong><a href="\'. esc_url( $category_link ).\'" target="_blank">\'.$cidargs[\'category_name\'].\'</a></strong> category. You can download <strong>\'. $thetitle .\'</strong> HD wallpaper for your desktop, notebook, tablet or phone or you can edit the image, resize, crop, frame it so that will fit on your device.<br />\';

                $alltags =  $thetitle;
                $createtags = explode(" ", $alltags); 

                 $postData = array(
                    \'post_title\'    => $thetitle,
                    \'post_type\'         => \'post\',
                    \'post_content\'  => $content,
                    \'post_category\' => $cid,
                    \'tags_input\'    => $createtags,
                    \'post_status\'   => \'future\'
                );

                $post_id = wp_insert_post($postData);

                // attach media to post
                wp_update_post( array(
                    \'ID\' => $attachId,
                    \'post_parent\' => $post_id
                ));

                set_post_thumbnail( $post_id, $attachId );

                return $attachId;
            } 
            break;

        case \'off\':
            break;

    }
}
请帮助我更改此代码,以便安排帖子发布,每60分钟发布一篇帖子。

1 个回复
SO网友:Robbert

您可以使用Wordpress Cron功能每小时安排一次Cron作业。您需要记住的唯一一件事是Wordpress cron的工作方式与普通cron不同。

如果超过预定时间,当有人访问您的WordPress站点时,将触发该操作。

https://codex.wordpress.org/Function_Reference/wp_schedule_event

<?php
// Schedules the hourly cron job
function create_hourly_schedule() {
    $timestamp = wp_next_scheduled( \'hourly_image_upload\' );
    if( $timestamp == false ) {
        wp_schedule_event( time(), \'hourly\', \'hourly_image_upload\' );   
    }
}
add_action( \'wp\', \'create_hourly_schedule\' );

// The image upload trigger
function hourly_image_upload() {
    $autopost_controler = get_theme_mod( \'auto_onoff\' );
    if( $autopost_controler != \'\' ) {
        switch ( $autopost_controler ) {
            case \'on\': 
                add_action( \'add_attachment\', \'auto_post_on_image_upload\' );
                break;

            case \'off\':
                break;

        }
    }
}

// The image upload function
function auto_post_on_image_upload( $attachId ) {

    $attachment             = get_post($attachId);
    $image              = wp_get_attachment_image_src( $attachId, \'large\');
    $image_tag          = \'<p><img src="\'.$image[0].\'" /></p>\';
    $theoriginaltitle   = $attachment->post_title;
    $onetitle           = str_replace("-"," ",$theoriginaltitle);
    $thetitlef          = str_replace("_"," ",$onetitle);
    $thetitle           = ucwords(strtolower($thetitlef));
    $cidargs            = array( \'category_name\' => get_theme_mod( \'cat_1\' ) );
    $cid                = array( get_category_by_slug( $cidargs[\'category_name\'])->term_id );
    $category_id        = get_cat_ID( $cidargs[\'category_name\'] );
    $category_link      = get_category_link( $category_id );

    $content = \'<strong>\'. $thetitle .\'</strong> is an <strong>HD wallpaper</strong> posted in <strong><a href="\'. esc_url( $category_link ).\'" target="_blank">\'.$cidargs[\'category_name\'].\'</a></strong> category. You can download <strong>\'. $thetitle .\'</strong> HD wallpaper for your desktop, notebook, tablet or phone or you can edit the image, resize, crop, frame it so that will fit on your device.<br />\';

    $alltags =  $thetitle;
    $createtags = explode(" ", $alltags); 

     $postData = array(
        \'post_title\'    => $thetitle,
        \'post_type\'         => \'post\',
        \'post_content\'  => $content,
        \'post_category\' => $cid,
        \'tags_input\'    => $createtags,
        \'post_status\'   => \'future\'
    );

    $post_id = wp_insert_post($postData);

    // attach media to post
    wp_update_post( array(
        \'ID\' => $attachId,
        \'post_parent\' => $post_id
    ));

    set_post_thumbnail( $post_id, $attachId );

    return $attachId;
} 

?>

EDIT:

<?php
// Schedules the hourly cron job
function create_hourly_schedule() {
    $timestamp = wp_next_scheduled( \'hourly_image_upload\' );
    if( $timestamp == false ) {
        wp_schedule_event( time(), \'hourly\', \'hourly_image_upload\' );   
    }
}
add_action( \'wp\', \'create_hourly_schedule\' );

// The image upload trigger
function hourly_image_upload() {

    // Create your attachment here <---

    wp_insert_attachment( $attachment, $filename, $parent_post_id );
}

// The image upload hook function
function auto_post_on_image_upload( $attachId ) {

    $autopost_controler = get_theme_mod( \'auto_onoff\' );

    if( $autopost_controler != \'\' ) {
        switch ( $autopost_controler ) {
            case \'on\': 

                $attachment             = get_post( $attachId );
                $image              = wp_get_attachment_image_src( $attachId, \'large\');
                $image_tag          = \'<p><img src="\'.$image[0].\'" /></p>\';
                $theoriginaltitle   = $attachment->post_title;
                $onetitle           = str_replace("-"," ",$theoriginaltitle);
                $thetitlef          = str_replace("_"," ",$onetitle);
                $thetitle           = ucwords(strtolower($thetitlef));
                $cidargs            = array( \'category_name\' => get_theme_mod( \'cat_1\' ) );
                $cid                = array( get_category_by_slug( $cidargs[\'category_name\'])->term_id );
                $category_id        = get_cat_ID( $cidargs[\'category_name\'] );
                $category_link      = get_category_link( $category_id );

                $content = \'<strong>\'. $thetitle .\'</strong> is an <strong>HD wallpaper</strong> posted in <strong><a href="\'. esc_url( $category_link ).\'" target="_blank">\'.$cidargs[\'category_name\'].\'</a></strong> category. You can download <strong>\'. $thetitle .\'</strong> HD wallpaper for your desktop, notebook, tablet or phone or you can edit the image, resize, crop, frame it so that will fit on your device.<br />\';

                $alltags =  $thetitle;
                $createtags = explode(" ", $alltags); 

                 $postData = array(
                    \'post_title\'    => $thetitle,
                    \'post_type\'         => \'post\',
                    \'post_content\'  => $content,
                    \'post_category\' => $cid,
                    \'tags_input\'    => $createtags,
                    \'post_status\'   => \'future\'
                );

                $post_id = wp_insert_post($postData);

                // attach media to post
                wp_update_post( array(
                    \'ID\' => $attachId,
                    \'post_parent\' => $post_id
                ));

                set_post_thumbnail( $post_id, $attachId );

                break;
        }

    }

    return $attachId;
} 
add_action( \'add_attachment\', \'auto_post_on_image_upload\' );

相关推荐