• This is the data I get from the PDF post itself.
    WP_Post Object ( [ID] => 2585 [post_author] => 5 [post_date] => 2014-12-09 12:15:12 [post_date_gmt] => 2014-12-09 09:15:12 [post_content] => [post_title] => Winter_2014_Newsletter_(V) [post_excerpt] => [post_status] => inherit [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => winter_2014_newsletter_v-8-4 [to_ping] => [pinged] => [post_modified] => 2014-12-09 12:15:12 [post_modified_gmt] => 2014-12-09 09:15:12 [post_content_filtered] => [post_parent] => 0 [guid] => https://beta.musalaha.org/wp-content/uploads/Winter_2014_Newsletter_V1.pdf [menu_order] => 0 [post_type] => attachment [post_mime_type] => application/pdf [comment_count] => 0 [filter] => raw )

    When I make this call
    echo get_the_post_thumbnail( $PDF[1]['ID'], 'thumbnail' )
    I don’t get anything

    Where can I check if the image is actually added to the PDF? And what can I do to help you help me? ??

    https://www.ads-software.com/plugins/pdf-thumbnails/

Viewing 15 replies - 1 through 15 (of 31 total)
  • Thread Starter Squazz

    (@squazz)

    Ok, more information:

    With some PDF’s it works, with some it doesn’t.

    This file: https://beta.musalaha.org/wp-content/uploads/Theology-of-Reconciliaion-Full-Book-TextwithAppendix_10.06.131.pdf gived me a image back when I ask for it with echo get_the_post_thumbnail( $PDF[1][‘ID’], ‘thumbnail’ )

    But this file: https://beta.musalaha.org/wp-content/uploads/Winter_2014_Newsletter_V1.pdf gives me nothing back with that same request.

    (Future readers: these links won’t work for you, sorry)

    Plugin Author stianlik

    (@stianlik)

    Hi,

    First, a control question: Did you upload both images after you activated the plugin? If the answer is no, you can regenerate the thumbnails, or simply delete the PDF and upload it a second time with the plugin activated.

    Can you check if the thumbnail image has been generated? It will be saved as a normal image and you can find it in the media manager with title “Winter_2014_Newsletter_V1-thumbnail”.

    PDF thumbnails are connected to the PDF in the same way as featured images are to posts, with the meta key _thumbnail_id on the PDF attachment. Additionally every PDF thumbnail is marked with post meta PdfThumbnailsPlugin set to true.

    I uploaded both of the files you linked to on a test installation and was able to retrieve thumbnails for both.

    Thread Starter Squazz

    (@squazz)

    Yup, both “images” (I think you refer to the PDF files, right) are uploaded after activation of the plugin. Actually I first uploaded the PDF that worked, then the one that didn’t and then again the one that did, just to see if it was the file, or a general problem.

    The thumbnail image HAVE been generated, and I can acces it by directly linking to it by “original-upload-PDF-name-thumbnail-size.jpg”

    That is weird. That you cat get a thumbnail, while I don’t. Especially when I can get the image from one PDF file, but not the other.

    Maybe I am retrieving the image in a wrong way? How do you fetch your image? I try with get_the_post_thumbnail($PDF-ID).

    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?

    Thread Starter Squazz

    (@squazz)

    I use the solution that I have also written about on my blog https://squazz.dk/teknologi/wordpress/find-alle-pdf-filer-indeholdende-et-keyword/ ?? There might be a problem here, in that case, I’d love to know it ??

    function squazz_get_PDF($targetword, $limit = '-1', $remove = array()) {
       // Setup what we are searching for
       $args = array(
          'post_type' => 'attachment',
          'post_mime_type' => 'application/pdf',
          'numberposts' => -1,
          'post_status' => null,
          'post_parent' => null,
          'orderby' => 'post_date',
          'order' => 'DESC'
       );
    
       // Get the initial results
       $attachments = get_posts($args);
       // If there are results, sort them
       if ($attachments) {
          $i = 1;
          foreach ($attachments as $post) {
             // Get the post title
             $title = $post->post_title;
             // Only show results matching our keyword
             if (stripos($title,$targetword) !== false) {
                if(!strposa($title,$remove)) {
                   $files[$i]['ID'] = $post->ID;
                   $files[$i]['title'] = $title;
                   ...
                   if($i == $limit) { break; }
                   ++$i;
                }
             }
          }
       } else {
          _e('Nothing to see here', 'SquazzFunctions')
       }
       return $files;
    }
    
    $PDF = squazz_get_PDF('newsletter', 1, array('german', 'french'));

    Then I try out this:
    echo get_the_post_thumbnail( $PDF[1]['ID'], 'thumbnail' )

    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.

    Thread Starter Squazz

    (@squazz)

    Apparently I am creating a extra copy of the PDF when it’s uploaded. Might be something with my own setup.
    I’m uploading the file Winter_2014_Newsletter_(V), the thumbnail is attached to Winter_2014_Newsletter_V1 (ID 2582) and the ID that I get is Winter_2014_Newsletter_V1 (ID 2585), but there are two ID’s (one which does not have a thumbnail).

    Thanks for the tip about WP_Query, I might have to do that, the computer science student in me have always been screaming at my decision to not limit the amount of files I’m fetching ??

    Thread Starter Squazz

    (@squazz)

    Further studies show that all of my files are added to the database twice :/ I gotta find out what the problem is. It’s not yours. So sorry for the inconvenience :/

    Edit: The problem lies in the plugin “WPML Media”, a official WPML exstension that adds “multilingual support for Media files”. I don’t know if it is you who has to make support for WPML, or if it’s WPML that doing something wrong. Now you know it, and I will send WPML a message about it too.

    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.

    Thread Starter Squazz

    (@squazz)

    Thanks stianlik, I’ll put that information forward if they’re not taking actions ??

    Plugin Author stianlik

    (@stianlik)

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

    Thread Starter Squazz

    (@squazz)

    I’m having some trouble. You can follow the thread here: https://wpml.org/forums/topic/upload-creates-duplicate-in-database/

    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.

    Thread Starter Squazz

    (@squazz)

    Thanks for your idea! ??

    I will check that out in about 8 hours (when I get to work). Will tell you if I find a solution ??

    Thread Starter Squazz

    (@squazz)

    Almost a week went by before i got the time to play around with it.

    I am confident I have found some way to get around the problem, I, just don’t know how to implement it, maybe you can help me out here (been working with WP for 4 months now ?? )

    Apparently, “PDF Thumbnail” is creating and applying the thumbnail after WPML Media is done duplicating the files. At least that what I think, I have to admit that I do not have data to support this theory.

    WPML media has a function called synchronize_attachment_metadata which is set up the following way:

    global $sitepress;
    //Update _wp_attachment_metadata to all translations (excluding the current one)
    $trid = $sitepress->get_element_trid( $attachment_id, 'post_attachment' );
    if ( $trid ) {
    	$translations = $sitepress->get_element_translations( $trid, 'post_attachment', true, true, true );
    	foreach ( $translations as $translation ) {
    		if ( $translation->element_id != $attachment_id ) {
    			update_post_meta( $translation->element_id, '_wp_attachment_metadata', $metadata );
    		}
    	}
    }

    I am then trying to do this in within your function insertThumbnailAttachment:

    if(function_exists('synchronize_attachment_metadata')) {
        synchronize_attachment_metadata($thumbnailMetadata, $attachmentId);
    }

    My thought here is, that we make your plugin compatible with WPML Media. we check if WPML Media is installed (with function_exists(‘synchronize_attachment_metadata’) ) and if it is, then call the function than WPML itself uses to “synchronize attachment metadata”

    Does this make any sense for you?

    If you need the WPML files to look at I am pretty sure that I am allowed to give you the files. They won’t work on an installed without the license key anyway. (We might have to find an another way to communicate then ?? )

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘Thumbnail is created, but not added to PDF file’ is closed to new replies.