Forum Replies Created

Viewing 15 replies - 76 through 90 (of 106 total)
  • 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.

    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.

    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.

    Plugin Author stianlik

    (@stianlik)

    The error message you experience is not directly from this plugin so it is hard to pinpoint exactly what is failing. In the next version I will add some more error handling or logging to ease the debugging process.

    Thank you for posting the output of pdfinfo(), from what I can see imagick should be properly installed. The rest of this post assumes that you have experience with PHP programming, let me know if anything is unclear. You can test imagick directly using the following code (replace ‘somefile.pdf’ with a readable PDF file):

    <?php
    // read the first page of PDF and output as JPEG
    header("Content-type: image/jpeg");
    $im = new \Imagick('somefile.pdf[0]');
    $im->setImageFormat('jpg');
    echo $im->getImageBlob();

    The preceding code should display an image of the first page from ‘somefile.pdf’. Please let me know if this works, or if you get any error messages.

    It can also be related to execution time. If the PDF is large, it may take a long time to process and time out. You can modify maximum execution time in php.ini.

    Plugin Author stianlik

    (@stianlik)

    Great, I’ll do my best to help you further. You found the correct e-mail address.

    Plugin Author stianlik

    (@stianlik)

    You are probably right about the attachments being duplicated before thumbnails are generated, however, synchronize_attachment_metadata does not copy the relevant metadata for thumbnails so we would also need to modify that to keep the connection. Adding something like the following (below the update_post_meta call) should solve the second issue:

    $thumbId = get_post_meta( $attachment_id, '_thumbnail_id', true );
    if ($thumbId) {
        update_post_meta( $translation->element_id, '_thumbnail_id', $thumbId );
    }

    To make WMPL run the sync function after thumbnails are generated, you can add it with higher priority as follows*:

    remove_action('synchronize_attachment_metadata');
    add_action('wp_generate_attachment_metadata', 'synchronize_attachment_metadata', 11, 2);

    * Or get the WPML team to set priority 11 or higher to the action

    No need to modify this plugin ?? Besides, it would not be a good thing to add a dependency to an unrelated plugin, certainly not a commercial one. WPML is concerned with content duplication, this plugin is focused on generating thumbnails. Adding code to overcome lacks in WMPL would be breaking the separation of concerns principle. It should be a easy for the WPML team to fix the problem as described above.

    If I get access to the source code for “WPML Media”, I may be able to find a way that you can solve the issue without modifying any of the plugins, but you should check with them first.

    Plugin Author stianlik

    (@stianlik)

    Hi,

    It should be possible, I have some related notes in Other Notes. To make this work without a page refresh, we need to hook into the JavaScript API and update the media browser. I’m working on a solution, but at this point the API is poorly documented and I’m required to read through the core source code to get useful information. It will take me some time to figure out how this API can be utilized efficiently / correctly. You’re welcome to contribute if you find a good way to update the media browser ??

    The plugin does not automatically add PDF thumbnails to your posts, it generate an image of the PDF which is used as thumbnail for the actual PDF attachment. If you would like the thumbnail attached as a featured image to a normal post, you need to do that explicitly using the following steps:

    1. Upload file to post
    2. Refresh page
    3. Choose the generated thumbnail as featured image

    Alternatively, you can automatically display thumbnails for all PDFs uploaded with the method described in this support thread: where and how to put in that magic snippet?.

    Best Regards,
    Stian

    Plugin Author stianlik

    (@stianlik)

    It should be fairly easy to make the plugin work, but it can be used in many different ways. I can probably guide you through it if you explain exactly what you are trying to do and how the solution should be used afterwards. I.e. step-by-step explanation of how your wife would use the system and the expected outcome.

    Edit:

    I’m guessing that you are trying to link to PDF-files uploaded to the current page? In that case, you can replace your attachment-code with
    the following:

    <?php foreach (get_attached_media('application/pdf') as $pdf): ?>
        <a href="<?php echo wp_get_attachment_url($pdf->ID); ?>">
            <?php echo get_the_post_thumbnail($pdf->ID); ?>
        </a>
    <?php endforeach; ?>

    This will get all PDF attachments uploaded to the current page and display a thumbnail with download link to the PDF.

    Plugin Author stianlik

    (@stianlik)

    If I understand you correctly, you have set the PDF thumbnail as a featured image for a static page? The image is displaying as expected, but you need some way to get PDF attachment ID?

    In the current version it works the other way around, there is a connection from the PDF to the thumbnail. That is, if you have the ID of a PDF, you can show the thumbnail. However, it would make sense to have a bi-directional connection.

    I will publish a new version 0.0.6 shortly, with that you can get the PDF ID using post_parent:

    echo get_the_permalink(wp_get_post_parent_id($thumbnailId)));

    This will only work for PDFs that you uploaded after upgrading to version 0.0.6. If you need support for older thumbnails, you can add
    the connection manually by setting the post_parent-parameter for your existing thumbnails.

    Plugin Author stianlik

    (@stianlik)

    To view the generated thumbnail for a PDF, all you need to do is:

    echo get_the_post_thumbnail($id);

    Where $id is the ID of a PDF file you uploaded after installing the plugin. For your first try, you can copy the ID shown in your address bar when editing uploaded PDF in the media manager.

    To give you more specific advice, I need some more information:

    1. Which template file is the code from (i.e. filename)?
    2. Did you upload the PDF before or after you installed “PDF Thumbnails”?
    3. What do you mean by “it still shows the jpg-file”? Which jpg do you
      see?
    Plugin Author stianlik

    (@stianlik)

    You should be able to bypass the problem if you can find out how “WPML Media” connects the duplicates with the original PDF. In that case, you could simply get the original ID and use that to access the thumbnail as follows:

    // functions.php
    function wpml_media_get_original_attachment_id($id) {
        // Retrieve attachment ID somehow, probably stored as post meta.
        // The "WPML Media"-team should be able to help you here.
    }
    
    // Template
    // ...
    $id = wpml_media_get_original_attachment_id($PDF[1]['ID']);
    echo get_the_post_thumbnail($id);
    // ...

    Note that the above code will not work, I do not have access to the source code for “WPML Media” so I do not know where the connection is.

    Plugin Author stianlik

    (@stianlik)

    Great, thanks ?? Marking this as resolved for now. Please let me know how it goes with “WPML Media”.

    Plugin Author stianlik

    (@stianlik)

    I understand, thank you for keeping me informed. I checked out the documentation for “WPML Media”. I is likely that they only copy the attachment content (title, description and such), not metadata. I.e. the connection _thumbnail_id is not copied to duplicates.

    Since it is “WPMP Media” that duplicates the attachments, it is reasonable for the fix to be performed from that end. If I’m not missing something, all they need to do, is to include a copy of post meta _thumbnail_id.

    Plugin Author stianlik

    (@stianlik)

    I am able to view thumbnails for both of your PDFs with the code you provided. That is, I used the following:

    // Newsletter PDF
    $PDF = squazz_get_PDF('newsletter', 1, array('german', 'french'));
    echo get_the_post_thumbnail($PDF[1]['ID']);
    
    // Other PDF
    $PDF = squazz_get_PDF('appendix', 1, array('german', 'french'));
    echo get_the_post_thumbnail($PDF[1]['ID']);

    Is it possible that you are retrieving some other PDF when the thumbnail does not show up? Can you try to use the ID directly (copy from edit page in the media manager)?

    Tip to improve your code: Try to do most of the work using WP_Query options, and use custom queries for more advanced functionality. Using get_posts without a limit can be costly if you have many posts in the database.

    Plugin Author stianlik

    (@stianlik)

    Ok, that’s strange. If the thumbnails are generated, you should be able to retrieve them from the PDF.

    I use get_the_post_thumbnail($post_id), that would be get_the_post_thumbnail($PDF->ID); in your case. Is it possible that the variable $PDF does not contain the attachment? Can you try var_dump($PDF); and var_dump($PDF->ID); and post the results here?

    How do you define the $PDF variable?

Viewing 15 replies - 76 through 90 (of 106 total)