Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author stianlik

    (@stianlik)

    Hi,

    If I’m going to give you specific advice, I need more information. I.e. information how you are trying to add the thumbnails, what you expect should happen, and what actually happens. In the following, I’m referring to other support threads that may help you on the way.

    If you are trying to add PDF thumbnail to a post, you can choose generated thumbnail as featured image manually, or add a code snippet to your theme.

    Thread Starter Nw92

    (@nw92)

    Hi,

    Sorry about the vague description. I’m hoping to dynamically create a product preview of a pdf by uploading an attached PDF to the product page and scraping the first page with this plugin and assigning it as the featured image. Currently, nothing happens. I cannot find the link in the code or anywhere that allows me to dynamically grab the ID of the thumbnail image and assign it to the products page as a featured image. I hope thats enough information.

    As for the links you provided, I thought that the second link you gave me was pretty close to what I’m looking for, minus the linking to the pdf functionality.

    Plugin Author stianlik

    (@stianlik)

    As a quick solution, you can use the following code to set the last uploaded PDF for a given post as the featured image:

    add_filter('update_post_metadata', function ($check, $object_id, $meta_key) {
        if ($meta_key === 'PdfThumbnailsPlugin') {
            $pdf_id = get_post($object_id)->post_parent;
            $post_id = get_post($pdf_id)->post_parent;
            set_post_thumbnail($post_id, $object_id);
        }
        return $check;
    }, 10, 3);

    Note that this only will work when you actually upload the PDF to the post, not when you simply choose an existing PDF from the media library. Not the best solution, but should point you in the right direction.

    Let me know if you’d like me to explain the code.

    Thread Starter Nw92

    (@nw92)

    Thank you for this, I really appreciate it.

    I’ve tried adding the code snippet to my functions.php but nothing appears to happen in the way of attaching the image as the featured image. I am uploading the image rather than choosing one from the media library. Could you please explain the code?

    Plugin Author stianlik

    (@stianlik)

    I’m not sure how experienced you are with PHP and WordPress, in the following I assume you have some coding experience. Let me know if anything is unclear. Note that the code requires PHP 5.3 or newer because of the anonomyous function.

    That’s strange, I tested the code locally before posting. Can you verify that the thumbnails are generated? I.e. do you find an image of the PDF when you open the media uploader? Do you see any error messages in the log?

    When a PDF thumbnail is generated, the plugin calls update_post_meta with meta key PdfThumbnailsPlugin which triggers the ‘update_post_metadata’ filter. We listen to this trigger using:

    add_filter('update_post_metadata', ...)

    In the function, we verify that the meta key is equal to PdfThumbnailsPlugin to be sure that we only execute the code for generated thumbnails:

    if ($meta_key === 'PdfThumbnailsPlugin')

    The variable $object_id contains the ID of the generated thumbnail that we would like to attach as a featured image. In all cases, a link to the PDF attachment is stored in the post_parent parameter, and a link to the post for which the attachment was uploaded is stored in the post_parent parameter for the attachment:

    PDF Thumbnai -> PDF attachment -> Post

    When we have a reference to the post and thumbnail, we simply set the featured image as follows:

    set_post_thumbnail($post_id, $object_id);

    The function always returns $check to avoid any side-effects. In some other context we could have returned false to prevent the metadata update.

    Thread Starter Nw92

    (@nw92)

    Hey Stianlik, sorry for the rather late reply.

    I can verify that the pdfs generate thumbnails that appear in the media library. I managed to set up php error logging and the logs are full of repeating errors of:
    PHP Notice: gd_edit_image_support is deprecated since version 3.5! Use wp_image_editor_supports() instead. in /home/…/functions.php on line 3378

    I’ve read around that this is probably to do with ImageMagick, which may explain why this wouldn’t work, but I’m doubtful because as far as I understand, ImageMagick is only involved in the pdf image generation stage and not the featured image allocation stage.

    Other than the above error, I can see no telling signs of why this would not work. Just to check, can you verify where you put the code snippet you created for this? I put mine in the child theme’s function.php file, but as I’m entirely new to WP I’m not entirely sure if this is the right area.

    Thanks again and Happy New Year!

    Plugin Author stianlik

    (@stianlik)

    Hi,

    No problem, and sorry for my late reply.

    I placed the code snippet at the first line of functions.php and tested as follows:

    1. Login to admin
    2. Edit post and upload PDF
    3. At this point, nothing has happened and I do not save the post
    4. Refresh page
    5. I can see that featured image is set

    If you can see the generated image of your PDF, ImageMagick is working (that’s how the image was generated) ??

    It looks like your functions.php file calls a deprecated function. I’m not sure if that is the root cause, but you should address the issue and follow the
    recommendation of using wp_image_editor_supports instead.

    Setting the featured image is simply saving metadata in the database so ImageMagick should not be an issue. To verify that the code has been executed, you can replace

    set_post_thumbnail($post_id, $object_id);

    with

    set_post_thumbnail($post_id, $object_id);
    error_log('Featured image attached');

    It’s a bit late, but Happy New Year!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Assign thumbnail as Feature Image’ is closed to new replies.