<?php | |
/** | |
* Plugin Name: Set featured image | |
* Plugin URI: http://wpengineer.com/2460/set-wordpress-featured-image-automatically/ | |
* Description: Set featureed image automaticly on save post/page | |
* Version: 1.0.1 | |
* Author: Frank Bültge | |
* Author URI: http://bueltge.de | |
* License: GPLv3 | |
*/ | |
// This file is not called by WordPress. We don't like that. | |
! defined( 'ABSPATH' ) and exit; | |
if ( ! function_exists( 'fb_set_featured_image' ) ) { | |
add_action( 'save_post', 'fb_set_featured_image' ); | |
/** | |
* Set featured image on posts | |
* | |
*/ | |
function fb_set_featured_image() { | |
if ( ! isset( $GLOBALS['post']->ID ) ) | |
return NULL; | |
if ( has_post_thumbnail( get_the_ID() ) ) | |
return NULL; | |
$args = array( | |
'numberposts' => 1, | |
'order' => 'ASC', // DESC for the last image | |
'post_mime_type' => 'image', | |
'post_parent' => get_the_ID(), | |
'post_status' => NULL, | |
'post_type' => 'attachment' | |
); | |
$attached_image = get_children( $args ); | |
if ( $attached_image ) { | |
foreach ( $attached_image as $attachment_id => $attachment ) | |
set_post_thumbnail( get_the_ID(), $attachment_id ); | |
} | |
} | |
} | |
if ( ! function_exists( 'fb_add_thumb_column' ) ) { | |
// posts columns | |
add_filter( 'manage_posts_columns', 'fb_add_thumb_column' ); | |
add_action( 'manage_posts_custom_column', 'fb_add_thumb_value', 10, 2 ); | |
// pages columns | |
add_filter( 'manage_pages_columns', 'fb_add_thumb_column' ); | |
add_action( 'manage_pages_custom_column', 'fb_add_thumb_value', 10, 2 ); | |
/** | |
* Add description for table head | |
* | |
* @param $cols Array | |
* @return $cols Array | |
*/ | |
function fb_add_thumb_column( $cols ) { | |
$cols['thumbnail'] = __('Thumbnail'); | |
return $cols; | |
} | |
/** | |
* Add thumbnail, if exist | |
* | |
* @param $column_name String | |
* @param $post_id Integer | |
*/ | |
function fb_add_thumb_value( $column_name, $post_id ) { | |
if ( 'thumbnail' !== $column_name ) | |
return; | |
$width = (int) 35; | |
$height = (int) 35; | |
$args = array( | |
'numberposts' => 1, | |
'order' => 'ASC', // DESC for the last image | |
'post_mime_type' => 'image', | |
'post_parent' => get_the_ID(), | |
'post_status' => NULL, | |
'post_type' => 'attachment' | |
); | |
$attached_image = get_children( $args ); | |
if ( $attached_image ) { | |
foreach ( $attached_image as $attachment_id => $attachment ) | |
echo wp_get_attachment_image( $attachment_id, array( $width, $height ), TRUE ); | |
} else { | |
echo __( 'None' ); | |
} | |
} | |
} |
I have use this hook for also save for draft e.g. Do you have create an new post, save via autosave works fine on my tests, also save and also on publish. Please give me more information for the php error, that I can reproduce this. Thanks a lot.
This is happening when i first add a new post with WP_DEBUG on, If i add content to the post and publish it the Notices ( Notice: Trying to get property of non-object in C:\xampplite\htdocs\wordpress\wp-includes\post-template.php on line 30 ) go away so it only when i first create a new post. Here is a screenshot: http://awesomescreenshot.com/04f9wuide
Thanks, now on my dev client I have see this bug and now i fixed this on the Gist; Also usable with Hook save_post
for save an image on all save todos.
Very nice helper! How would I add a default ID or URL if no image is present?
@bueltge
Very good solution! Works great, however I would like to know more how to implement a function to this script:
I would like if there were no images attached to the post, a standard image set to be featured image.
Tks!
If you will add a default image, if you have no uploaded image on the post, use the source bellow.
add_filter( 'post_thumbnail_html', 'fb_post_thumbnail_html' );
function fb_post_thumbnail_html( $html ) {
if ( empty( $html ) )
$html = '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default-thumbnail.png' . '" alt="" />';
return $html;
}
Sorry, but how implement this code?
Should I replace or include it in which lines of the original code?
@rpgmen: add this lines from my comment to the plugin, below all source, the init source and ready.
How to modify to support custom post type? for example I have custom post type 'property'
Bin ich nur zu blöd, dass ich den Download-Button nicht sehe? Wie kriege ich das Plugin sonst runter? Vielen Dank für eure Hilfe.
Maybe you public this plugin to WordPress repository?
@bueltge Strangely complement the script stopped working in the last version of WP ... the standard image that no longer appear when the post is saved ... could check the reason?
hello @bueltge I editliy my themes the component files individually. The markings on the slider component files: How do I bring small areas?http://prntscr.com/9bxbx4
so unhelpful and waste of time.
Thanks Frank,
This is very useful but i was getting errors when creating a new post
Notice: Trying to get property of non-object.
I am thinking that
Needs to be: