Support » Plugin: PhotoSwipe » How to make this work for older posts?

  • Resolved codegrau

    (@codegrau)


    Hi there,

    thank you for this awesome plugin. Looking forward to the release of the “description & caption” feature!

    I have a blog with lots of posts (1000+) so I wonder if it’s possible to add photoswipe without saving all of them manually one by one?

    Regards,
    Thomas

    https://wordpress.org/plugins/photo-swipe/

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

    (@louyx)

    Hi @codegrau.

    Thank you! Well, I don’t think it would be hard to create a script that would loop through posts and trigger a “save_post” action.
    I’m a little busy these days so if you could help by creating one I can add it to the plugin.
    If not, I’ll add it to my to-do list 🙂

    Regards,

    pirro70

    (@pirro70)

    Hi @codegrau
    you can use bulk Action for update all post
    You can go to post page and in “screen option” select the quantity of posts you want to view (max=999), then select all by the first checkbox near the title -> “edit” -> apply -> update.
    Hope I was clear

    JonRWilson

    (@jonrwilson)

    Hi,
    Let me start by saying that I’m a novice and am happy that you’ve started turning PhotoSwipe into a full fledged plug in.

    That said, the idea of modifying every post rather than inserting data-size="XXXxYYY" in all the <a> tags with php, seems like a real backwards method of solving this problem. It dirties every post with this excess of information, that will remain, even if the plugin is uninstalled. IMHO plug-in’s should be totally “non-destructive” in their installation, leaving no trace of their presence if uninstalled.

    Additionally, this approach doesn’t work with featured images. I thought I’d be able to modify my singles.php file with the standard size of my images 1440×960, but then realized that a few of my header images are cropped down, so they’re now being stretched vertically if clicked on…

    Modifying this seems promising

    <?php
    list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
    echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
    ?>

    If I figure it out I’ll report back, since this plug-in is perfect otherwise and I would like to do what I can to help.

    Thanks,
    Jon

    JonRWilson

    (@jonrwilson)

    OK, I’ve made some progress and have modified my child theme single.php, which already included the addition of the featured image, but now also automatically inserts the data-size specific to the image being linked to:

    <!-- ADD FEATURED IMAGE & PhotoSwipe data-size="XXXxYYY" -->
    		<?php
    		if ( has_post_thumbnail() ) {
    			$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
    			$content = '<a data-size="' . $full_image_url[1] . 'x' . $full_image_url[2] . '" href="' . $full_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">' . get_the_post_thumbnail( $post_id, 'large' ) . '</a>';
    			echo $content;
    		}
    		?>

    Now I just need to figure out how to insert the data-size for all the linked images in a post and remove the automatic post modification.

    Looks like a modification of this will do the trick:

    /**
     * Attach a class to linked images' parent anchors
     * Works for existing content
     */
    function give_linked_images_class($content) {
    
      $classes = 'img'; // separate classes by spaces - 'img image-link'
    
      // check if there are already a class property assigned to the anchor
      if ( preg_match('/<a.*? class=".*?"><img/', $content) ) {
        // If there is, simply add the class
        $content = preg_replace('/(<a.*? class=".*?)(".*?><img)/', '$1 ' . $classes . '$2', $content);
      } else {
        // If there is not an existing class, create a class property
        $content = preg_replace('/(<a.*?)><img/', '$1 class="' . $classes . '" ><img', $content);
      }
      return $content;
    }
    
    add_filter('the_content','give_linked_images_class');

    Plugin Author Louy Alakkad

    (@louyx)

    Hi @jonrwilson,

    I chose this way because it’s the only way that has no effect on performance. It’s not efficient to query about image sizes every time you want to display a post.

    You can create a script that will clean up the database after the plugin uninstalls. It will be very simple.

    Also the plugin inserts the size attribute automatically when you use wp_get_attachment_link. So just use for your featured images.

    Regards,

    JonRWilson

    (@jonrwilson)

    Hi Louy,

    Thanks for your quick reply!

    I was unaware of the inefficiencies and detrimental performance aspects involved in querying image sizes. I also don’t have a ton of posts, so last night I just went ahead with the process of “Quick Edit” + “Update”ing all my posts.

    I don’t understand what you mean by

    Also the plugin inserts the size attribute automatically when you use wp_get_attachment_link. So just use for your featured images.

    Do you mean that if I use wp_get_attachment_link instead of wp_get_attachment_image_src, it’ll automatically add the data-size, because the plug-in ties into wp_get_attachment_link? If so, thank you for the informational link, I’ll post the updated code when I figure it out (looks like it’ll be pretty similar to what I have), since I expect it to be useful for others using featured images.

    Thanks,
    Jon

    Plugin Author Louy Alakkad

    (@louyx)

    Hi @jonrwilson,

    It’s alright 🙂 Yes that is what I meant. It should be very simple.

    Regards,

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to make this work for older posts?’ is closed to new replies.