Featured image not working when posting to WP with a cron job

by comdex   Last Updated May 14, 2019 21:08 PM

I have a really weird issue. Here is my code to post to WordPress and set a featured image from a remote image

if (!function_exists(add_featured_image)) {

    function add_featured_image($image_url,$post_id,$image_name) {
        // Add Featured Image to Post
        $upload_dir       = wp_upload_dir(); // Set upload folder
        $image_data       = file_get_contents($image_url); // Get image data
        $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
        $filename         = basename( $unique_file_name ); // Create image file name

        // Check folder permission and define file location
        if( wp_mkdir_p( $upload_dir['path'] ) ) {
            $file = $upload_dir['path'] . '/' . $filename;
        } else {
            $file = $upload_dir['basedir'] . '/' . $filename;
        }


        // Create the image  file on the server
        file_put_contents( $file, $image_data );

        // Check image file type
        $wp_filetype = wp_check_filetype( $filename, null );

        // Set attachment data
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title'     => sanitize_file_name( $filename ),
            'post_content'   => '',
            'post_status'    => 'inherit'
        );

        // Create the attachment
        $attach_id = wp_insert_attachment( $attachment, $file, $post_id );

        // Include image.php
        require_once('../wp-admin/includes/image.php');

        // Define attachment metadata
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

        // Assign metadata to attachment
        wp_update_attachment_metadata( $attach_id, $attach_data );

        // And finally assign featured image to post
        set_post_thumbnail( $post_id, $attach_id );
    }

}



    // * * *
    // Create WordPress post

        $title = "wp test";
        $post_content = "test";

        echo "<br><b>Posting to WP:  $title</b>";

        include_once("../wp-load.php");
        $new_post = array(
            'post_title' => $title,
            'post_content' => $post_content,  
            'post_status' => 'publish',
            //'post_date' => date('Y-m-d H:i:s'),
            'post_author' => $wp_user_id,
            'post_type' => 'post',
            'post_category' => array(),
            'tags_input' => array()
        );
            $post_id = wp_insert_post($new_post, true);


            echo "<br>Post ID = $post_id";




        $heading_image = "https://www.example.com/remote-image.jpg";
        $image_name = "test.jpg";
        add_featured_image("$heading_image","$post_id","$image_name");
        echo "<br>Heading IMG: $heading_image  (renamed to $image_name)";

The code works perfectly when I open the PHP file manually in Chrome BUT it doesn't work if I run the script using a cron job or using wget in ssh, like this:

wget -O /dev/null --no-check-certificate https://www.infolibertaire.net/rss_aggregator/test.php

The above will successfully upload the image to WordPress media library, and it even says that the image was uploaded to the post we just created, titled "wp test". http://prntscr.com/nol4g8 BUT when I open the post in wp-admin, it does NOT have a featured image

I am totally confused. Why is it working perfectly when I run the code in my web browser, but yet it is always failing when I run the code with wget?



Related Questions


How to find attachment by it's name?

Updated May 11, 2017 15:08 PM

Adding Post Image programatically

Updated September 18, 2018 15:08 PM


Get first image in a post

Updated April 28, 2018 15:08 PM