Jayne
Forum Replies Created
-
Forum: Reviews
In reply to: [Autoset Featured Image] Error Messages in WP4; please update!Please ignore the above!
Sorry – it’s got some errors in it – will work on fixing it and post backHere’s a link to an even slicker solution, and it means you don’t have to upload a default image to your themes folder – you can just use the ID of one of your already uploaded images
Here’s a link for how to add images to posts that don’t have any featured image set – you can add a link to the file via your functions.php file.
How to define a default post thumbnailHope this helps
Forum: Reviews
In reply to: [Autoset Featured Image] Error Messages in WP4; please update!I am not using this plugin but came across a similar error on my own custom autoset featured image, which I have modified from here.
I have included a few extra lines which means you don’t get the debug errors on posts that haven’t been created yet e.g. on new-post.php and new-page.php admin screens
The following may help you:
Create a new file called and name it something like featured-image.php and save somewhere in your themes folder
Then include a reference to if from your functions.php file e.g. (replace /inc/ with your path to the file)
require get_template_directory() . '/inc/featured-image.php';
Then in the featured-image.php file drop the following code
/** * Autoset featured image to first image in post if not specifically defined */ add_action('the_post', 'your_theme_autoset_featured_image'); add_action('save_post', 'your_theme_autoset_featured_image'); add_action('draft_to_publish', 'your_theme_autoset_featured_image'); add_action('new_to_publish', 'your_theme_autoset_featured_image'); add_action('pending_to_publish', 'your_theme_autoset_featured_image'); add_action('future_to_publish', 'your_theme_autoset_featured_image'); function your_theme_autoset_featured_image() { //checks for thumbnail - it does not use the global post ID because we haven't created a post yet! if ( !has_post_thumbnail() ){ // if post does not have a thumbnail then do nothing!! } else { // set globals for the function global $post; $already_has_thumb = has_post_thumbnail($post->ID); $screen = get_current_screen(); /* * checks that you are not on add a new post or add new page screen, so it is not looking for empty objects and thus returning no debug errors! Yay! */ // check for thumb and what admin page you are on if (!$already_has_thumb && $screen->base !== '-new.php') { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } }
The crucial bits in the code above are
if ( !has_post_thumbnail() ){ // if post does not have a thumbnail then do nothing!! }
and
if (!$already_has_thumb && $screen->base !== '-new.php') { //original code }
You might be able to modify the plugin php files with the extra bits Hope this helps