I’m running the code above as WP cron
The file_is_valid_image
function is defined in wp-admin/includes/image.php
, and this file is loaded automatically only on WordPress admin pages (in wp-admin
).
So if you want to use that function on other/non-admin pages, e.g. a page in the site’s front-end, a REST API endpoint, or wp-cron.php
which runs during a WP-Cron request, you need to manually load wp-admin/includes/image.php
, like so:
// load the file which defines the file_is_valid_image function
require_once ABSPATH . 'wp-admin/includes/image.php';
// now you can call the function, e.g. in your case:
if ( file_is_valid_image( $upload_basedir . "https://wordpress.stackexchange.com/" . $image_file ) ) ...
But actually, you don’t have to use file_is_valid_image()
, because you can easily do what it does where it uses wp_getimagesize()
which is loaded everywhere in WordPress:
// all you need to do is, replace this:
if ( file_is_valid_image( $upload_basedir . "https://wordpress.stackexchange.com/" . $image_file ) )
// with this:
$size = wp_getimagesize( $upload_basedir . "https://wordpress.stackexchange.com/" . $image_file );
if ( ! empty( $size ) )