Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter pepperbiffen

    (@pepperbiffen)

    When a user uploads a file, The pdf image grnerator plugin generates a thumbnail of uploaded files. i want the generated thumbnail set as the featured image.

    Plugin Author Mizuho Ogino

    (@fishpie)

    Hi.

    I hope this function helps you.

    //Automatically set PDF thumbnail as featured image.
    function save_pdf_thumb_as_featuredimage ( $post_id ) {
        if ( wp_is_post_revision( $post_id ) ) return;
        if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type
        if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image
        $attaches = get_posts ( 'post_parent='.$post_id.'&numberposts=-1&post_type=attachment&post_mime_type=application/pdf&orderby=menu_order&order=ASC' );
        if ( $attaches ): foreach( $attaches as $attach ):
            if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail
                update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
                break;
            }
        endforeach; endif;
    }
    add_action( 'save_post', 'save_pdf_thumb_as_featuredimage' );

    If you want always automatically to set first attachment as featured image even if it is PDF, try this function.

    //Automatically set first image/PDF as featured image.
    function save_thumbnail_as_featuredimage ( $post_id ) {
        if ( wp_is_post_revision( $post_id ) ) return;
        if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type
        if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image
        $args = array(
            'post_parent' => $post_id,
            'post_type' => 'attachment',
            'numberposts' => -1,
            'post_status' => null,
            'orderby' => 'menu_order date',
            'order' => 'ASC ASC'
        );
        $attaches = get_posts($args);
        if ( $attaches ): foreach( $attaches as $attach ):
            if ( $attach->post_mime_type == 'application/pdf' ){
                if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail
                    update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
                    break;
                }
            } elseif ( preg_match("/^image\//", $attach->post_mime_type ) ) {
                update_post_meta( $post_id, '_thumbnail_id', $attach->ID );
                break;
            }
        endforeach; endif;
    }
    add_action( 'save_post', 'save_thumbnail_as_featuredimage' );

    Thank you.

    Thread Starter pepperbiffen

    (@pepperbiffen)

    Thank you for answering me Mizuho. Where do i insert this function?

    Plugin Author Mizuho Ogino

    (@fishpie)

    the functions.php in the theme which you are using.

    Thread Starter pepperbiffen

    (@pepperbiffen)

    Thank you for helping me Mizuho, I really appreciate it. I am using Easy digital Downloads bundled with Marketify as theme. So the custom post type is the “download” So when i am adding a download i insert the pdf from the media uploader. But the Featured Image wont be uploaded automatically? Why is that? I can provide you the webpage and more if you want. Please contact me on anonym97@hotmail.com

    Thank you very much.

    Thread Starter pepperbiffen

    (@pepperbiffen)

    Plugin Author Mizuho Ogino

    (@fishpie)

    Ok.

    I’m not sure to be able to hook into EDD post type.
    But it sounds like useful.
    I’ll try in the near future.

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Automate PDF image into custom post type.’ is closed to new replies.