In content-attachment.php I have:
/* preview for PDFs*/
if ('' === $ai) {
wp_generate_attachment_metadata( $the_ID, wp_upload_dir() );
}
And it throws this error:
Fatal error: Call to undefined function wp_generate_attachment_metadata() in /home/content/xx/html/example/wp-content/themes/twentyseventeen-child/template-parts/post/content-attachment.php on line 110
Per another question here, I tried adding the following to the top of this file:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
And that generates a slew of errors like this:
Warning: preg_match() expects parameter 2 to be string, array given in /home/content/55/8933555/html/seatacnoise/wp-includes/functions.php on line 2797
Now what?
In content-attachment.php I have:
/* preview for PDFs*/
if ('' === $ai) {
wp_generate_attachment_metadata( $the_ID, wp_upload_dir() );
}
And it throws this error:
Fatal error: Call to undefined function wp_generate_attachment_metadata() in /home/content/xx/html/example/wp-content/themes/twentyseventeen-child/template-parts/post/content-attachment.php on line 110
Per another question here, I tried adding the following to the top of this file:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
And that generates a slew of errors like this:
Warning: preg_match() expects parameter 2 to be string, array given in /home/content/55/8933555/html/seatacnoise/wp-includes/functions.php on line 2797
Now what?
Share Improve this question edited Sep 17, 2020 at 8:24 Cyclonecode 1,1841 gold badge9 silver badges32 bronze badges asked Sep 16, 2020 at 20:51 jchwebdevjchwebdev 7752 gold badges14 silver badges33 bronze badges1 Answer
Reset to default 6Despite you already solved it (the "undefined" issue), it should be noted that wp_generate_attachment_metadata()
is defined in wp-admin/includes/image.php
, so you need to manually load that file if you're using the function on the front-end, i.e. public side of the site or non admin/wp-admin
pages (e.g. single attachment pages).
And in fact, the function reference says that:
If this function is undefined in the environment where it is to be used, such as within a Shortcode, use the include function:
if ( ! function_exists( 'wp_crop_image' ) ) { include( ABSPATH . 'wp-admin/includes/image.php' ); }
(You may use wp_generate_attachment_metadata
instead of that wp_crop_image
)
Secondly, you're not properly using the function:
wp_generate_attachment_metadata( $the_ID, wp_upload_dir() );
Because the second parameter for wp_generate_attachment_metadata()
should be the attachment file path, and not an array (wp_upload_dir()
returns an array) or anything else. So for example:
$file = '/full/path/to/the/attachment/file.pdf';
wp_generate_attachment_metadata( $the_ID, $file );
And make sure the file path is valid and that the $the_ID
has the correct attachment post ID.