Uploaded attachment not set as featured image
I needed to do the following: set_post_thumbnail( $new_reseller_id, $attach_id );
I needed to do the following: set_post_thumbnail( $new_reseller_id, $attach_id );
You can’t redirect after content has already been sent to the browser, hook an action before the template is loaded and do your redirect there. function wpa_attachment_redirect(){ if( is_attachment() ){ // your code and redirect here } } add_action( ‘template_redirect’, ‘wpa_attachment_redirect’ );
Milo got me on the right track. If anyone else happens to have this issue the code I used is: /** * Redirect broken attachment links to 404 */ function ww_404_broken_links(){ global $wp_query; if(is_attachment()){ $attachment_path = get_attached_file( get_the_ID() ); //If the file doesn’t exist on the server show 404 if(!file_exists($attachment_path)){ $wp_query->set_404(); status_header( 404 ); get_template_part( … Read more
Since you’ve used the true argument for get_post_meta, $blogimages will be a single array, not a multidimensional one. So instead of $blogimages[0][‘blog-image-inside’], just use $blogimages[‘blog-image-inside’]. For absolute sanity, you might also want to check $blogimages before you use it: ‘exclude’ => [ $thumb_ID, isset( $blogimages[‘blog-image-inside’] ) ? $blogimages[‘blog-image-inside’] : 0, isset( $blogimages[‘blog-image-front’] ) ? $blogimages[‘blog-image-front’] … Read more
You could create a small plugin for this, here is an small example that will try to download and attach a file based on the url in the excerpt field for the post: add_action(‘publish_post’, ‘auto_featured_image_publish_post’); function auto_featured_image_publish_post($post, $post_id) { // check if this post is saved for the first time if($post->post_date == $post->post_modified) { // … Read more
Try this in the foreach loop: $thumb_url = wp_get_attachment_image_src( $attachment->ID, ‘full’ ); And this for the img src: echo $thumb_url[0];
If the attachment is attached to a post, the post is determined as “post_parent”. So, if you are in the attachement template (attachment.php, image.php, etc): while ( have_posts() ) { the_post(); // Get parent id of current post (get_the_ID() gets id of current post, // whicdh is the attachement, as we are in the attachment … Read more
You can use the wp_handle_upload_prefilter filter hook and it will be called when user will upload the file from admin side. Or You can use the wp_handle_upload filter hook for the uploaded file.
You’re missing an underscore: add_action(‘add attachment’, ‘bZive_generate_AlphanumericID’, 10, 3); Needs to be: add_action(‘add_attachment’, ‘bZive_generate_AlphanumericID’, 10, 1); I also changed the number of accepted arguments to 1, because add_attachment only has 1 and your callback function is only using 1 anyway.
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 … Read more