Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set featureed image automaticly on save post/page
@bradthomas127

This comment has been minimized.

Copy link

commented Jul 6, 2012

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

add_action( 'save_post', 'fb_set_featured_image' );

Needs to be:

add_action( 'publish_post', 'fb_set_featured_image' );
@bueltge

This comment has been minimized.

Copy link
Owner Author

commented Jul 7, 2012

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.

@bradthomas127

This comment has been minimized.

Copy link

commented Jul 7, 2012

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

@bueltge

This comment has been minimized.

Copy link
Owner Author

commented Jul 9, 2012

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.

@thob

This comment has been minimized.

Copy link

commented Oct 4, 2012

Very nice helper! How would I add a default ID or URL if no image is present?

@rpgmem

This comment has been minimized.

Copy link

commented Jan 12, 2013

@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!

@bueltge

This comment has been minimized.

Copy link
Owner Author

commented Jan 24, 2013

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;
}
@rpgmem

This comment has been minimized.

Copy link

commented Feb 1, 2013

Sorry, but how implement this code?
Should I replace or include it in which lines of the original code?

@bueltge

This comment has been minimized.

Copy link
Owner Author

commented Feb 11, 2013

@rpgmen: add this lines from my comment to the plugin, below all source, the init source and ready.

@saiallexander

This comment has been minimized.

Copy link

commented Aug 29, 2013

How to modify to support custom post type? for example I have custom post type 'property'

@duenner

This comment has been minimized.

Copy link

commented Sep 4, 2013

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.

@casepress

This comment has been minimized.

Copy link

commented May 4, 2014

Maybe you public this plugin to WordPress repository?

@rpgmem

This comment has been minimized.

Copy link

commented May 5, 2014

@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?

@tuba82

This comment has been minimized.

Copy link

commented Dec 8, 2015

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

@dragipostolovski

This comment has been minimized.

Copy link

commented Jul 11, 2018

so unhelpful and waste of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.