0

I'm making a plugin that has a custom post type and I need to add predefined content.

I used filters default_title and default_content for the title and content but I can't find how I can add an predefined attachment (thumbnail) image.

In my plugin I have the settings option where I set the default image and there I have the attachment ID. Now all I have to do is set it up when I create a new post.

Is there any action or filter to I can use it?

2
  • Do you need the default thumbnail to be saved to the post, or do you just want to display a default image on the front end if the post doesn't have a thumbnail? – Jacob Peattie Mar 19 at 10:45
  • I need default thumbnail to be saved to the post – Ivijan Stefan Stipić Mar 19 at 10:51
0

I found an adequate solution.

Generally, I used the save_post_{$post->post_type} hook which gave me a $post_ID and a $post object.

I was just checking to see if there was a thumbnail and if is not there to add it. That is all.

add_action( 'save_post_book', function ($post_ID, $post){
    if ( get_post_meta($post_ID, '_thumbnail_id', true) ) :
        return;
    endif;
    
    if($thumbnail_id = get_option('my_default_thumbnail_id', false)) {
        add_post_meta($post_ID, '_thumbnail_id', $thumbnail_id);
    }
}, 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.