1

How do I display a different group of photos with each blog post without uploading the photos to the Wordpress Media Library?

I’m using Wordpress for my blog. The blog is sort of a diary. One blog per day. The post title is each day’s date, in the format yyyymmdd. For each blog post, I take 10-50 photos. All the photos are in a single folder on my server. Each photo is numbered sequentially (1 thru whatever) & the file name of each image is the date (yyyymmdd) concatenated with the numeric counter.

I rather not upload all these photos into the Wordpress Media Library. (Or maybe I would upload just one photo per post.) When a visitor clicks on a link to view a post (of a particular day), I would like each photo to be displayed at the end of the post content. Either in a carousel or a grid.

I know that this is pretty straightforward if I would upload the photos into the WP Media Library, but that is what I’m trying to avoid.

I have at least 3 theories:

  1. figure out how to enter (programmatically) the image info to the media library. (Not sure if this can be done without triggering the photos being upload to the media library, which I don't want)
  2. do an add_filter on the_content (to insert the html needed),
  3. use a hook to capture output buffer & insert the html.
3
  • #2 looks like your most straightforward way to proceed. The filter function would provide whatever necessary processing to the folder of images, though to get the best results you may end up mimicking what the built-in media functions already do.
    – CK MacLeod
    Feb 22 '18 at 23:40
  • You can attach a photo in the media library to a post without it appearing in a post. Keep in mind that you cannot rely on the built in galleries etc if you don't create attachments, nor will plugins be able to access them. Can you provide some context as to why you don't want these items in the media library?
    – Tom J Nowell
    Feb 23 '18 at 2:12
  • Tom, thanks for your response. I don't want them in the media library for at least 2 reasons. First of all, there are so many of them. Secondly, I will have multiple sites accessing the same photos. So, why store them in multiple places if I can store them in one place. I could go with one of the plugins like DropBox File Chooser (similar plugins available for Google Drive, S3, etc.) but I don't want to go with one of them, because I'd like more flexibility re where I store the image files.
    – jjp
    Feb 23 '18 at 4:59
1

You can either manually insert the image tags - try adding one through the media library first to see the code it generates, then go into the Text Editor rather than the Visual Editor and change the URL to one of your images - or you can code a theme template that would match up the day and automatically show the images.

You'd create a child theme, copy your parent theme's post.php file, and then create some logic - get the current post's published or last updated date, then check the folder you manually upload to to see if there are any images whose filenames start with that date (making sure to format it in the same way your filenames are formatted). If so, do a foreach loop, and set up image tags. The biggest problem to doing it this way is you won't have any meaningful alt text for either search engines or visitors who need it, such as those using screen readers. That's another benefit of using the Media Library - makes it much simpler to include meaningful alt text.

2
  • WebElaine & Mohit, thanks for the suggestions. Here's another approach, does this make sense? Another option would be a blend of your two answers. Take the logic that WebElaine suggests putting in the child theme & instead use this code to populate one custom meta field containing an array of all the image urls for each post (alternatively multiple post meta fields, each containing a single url). Then, in the child theme put the code that would retrieve this custom meta data. (I've got an idea for inserting meaningful alt text, when available. But that's another project.)
    – jjp
    Feb 25 '18 at 5:29
  • I'm not sure what benefit there is to doing it this way rather than just uploading to the media library. If you set the postmeta once then accidentally delete some of the images as you're uploading, you'll end up with broken images on some posts and may not even realize it until much later. Yes, it's possible to set it up this way, I just don't see what the benefit would be. Your mention of using a variety of domains makes it sound as though you're trying to display images that aren't on your own servers, which is known as hotlinking and is frowned upon.
    – WebElaine
    Feb 26 '18 at 14:54
1

I strongly recomend you to install CBX Dropbox File Chooser plugin as you will be able to add images straight from your dropbox account and images files won't load any files into your hosting. You´d set up an API key that you can find in your dropbox account.A bottom will show up in your editor, this could be helpful: https://codeboxr.com/product/cbx-dropbox-file-chooser-for-wordpress/

1
  • Albertini, thanks for the info re CBX Dropbox. I can see how that would be useful, except that in my case, I need a plugin that includes the flexibility to use a variety of domains/paths for the url’s.
    – jjp
    Feb 23 '18 at 0:12
1

You can create a function for custom meta data and add one text field where you can enter external image url this will save only url of image. this will not upload image on your server and you can easily fetch on frontside.

Follow this link.

https://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.