Как планировать публикацию автопоста каждые 60 минут?

Я использую следующий код для автоматического публикации сообщений с загруженных изображений, он работает, но не публикует публикации публикаций, все сообщения публикуются сразу после того, как изображения были загружены медиа-загрузчиком в панель 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 минут.

Вы можете использовать функциональность 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; } ?> 

РЕДАКТИРОВАТЬ:

 <?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' );