You can use the WordPress Cron functionality to schedule a cron job every hour. The only thing you need to hold in mind is that the WordPress cron works different than a normal cron.
The action will trigger when someone visits your WordPress site, if the scheduled time has passed.
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' );