During work on Cutify Media Marketing’s new WordPress competition sites I encountered a problem with WordPress image uploads.

The following code was called to attach an uploaded image to a WordPress post and set it as the WordPress featured image, or post thumbnail as its called in the code..

$attachment = media_sideload_image( $mediaURL , $postId );

if ( is_wp_error( $attachment ) )
{
    file_put_contents(dirname(__FILE__)."/../backend/form/add-entry.log", print_r($attachment, true)."\r\n", FILE_APPEND);
}

$media = get_attached_media( 'image', $postId );

if( count( $media ) > 0 ) 
{
    $keys = array_keys( $media );
    set_post_thumbnail( $postId, $media[ $keys[0] ]->ID ); // setting this as the featured image for editing
}

The problem is that the code kept failing. We checked the logging of is_wp_error and found the following error:

WP_Error Object
(
    [errors] => Array
    (
        [http_request_failed] => Array
        (
            [0] => SSL certificate problem: self signed certificate
        )
    
    )

    [error_data] => Array
    (
    )
)

Our site uses SSL (https://), however on the server itself we use a self signed certificate. The reason for this is because this site is for a very busy competition (in the order of black friday) and as such we’re running it on AWS ec2 containers, through an AWS load balancer and cloudflare in front of that.

Both the AWS load balancer and cloudflare have their own certificates. Over and above that, we need to be able to spin up servers from images in a minute or two during peak load times and we couldn’t possibly wait for SSL certificates on the server to be up and running, so built in self signed certificates just work for us on that side of the load balancers..

Locally, though, when wordpress does a cURL to get the image, it fails the certificate check because its doing a local lookup, its not going through cloudflare.

It turns out the solution is pretty simple. Simply tell wordpress to not verify certificates.

The following code can be put into your plugin file (as it was for us), or into the functions.php file for your theme:

add_filter('https_ssl_verify', '__return_false');

add_filter('https_local_ssl_verify', '__return_false');

As soon as we added the code above our images uploaded and were added as the post’s thumbnail as expected.

 

Comments

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*
*