2

I am using following code to auto publish posts from images uploaded, it works but does not schedule posts publishing, all post are published right after images were uploaded by media uploader in wordpress dashboard.

$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;

    }
}

Please help me to change this code so it will schedule the posts publishing, one post at each 60 minutes.

2

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' );
  • Thanks Robert, but this is not working. I am uploading bulk images by media uploader and I want to schedule each post at 60 minutes. Using your code now nothing is happening, the uploaded images just remain in media library, there are not any posts scheduled, I'll wait for 2-3 hours to see if anything will happen, but in scheduled posts there is not any post now. – Marcel Cantea Apr 19 '16 at 4:59
  • I think the schedules are set properly, but I don't know if the functions they activate are setup right. Because we now schedule the event to run hourly_image_upload() which triggers a hooked function according to the Wordpress documentation: "This hook is called when an attachment created by wp_insert_attachment()" when a option is set. So I think you need to run the wp_insert_attachment() in the scheduled function. – Robbert May 3 '16 at 7:51
  • Check my edit. It is not complete yet, but I think if you complete the hourly_image_upload() function it will work. – Robbert May 3 '16 at 8:01

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.