Despite 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.