Users of the CyberSEO Pro and RSS Retriever plugins have repeatedly emphasized the need for a feature that allows the insertion of a random image from a specified directory on their server into generated posts. Previously, this requirement was met only by using Spintax in the URLs of inserted images or through custom PHP snippets (only available in CyberSEO Pro). Consequently, the need for this new feature, which is designed to select a random filename from a specified directory on the server, has become apparent.
It is important to acknowledge that while both plugins offer the ability to import images from Pixabay’s stock images, Google Image Search, and even generate completely unique images based on textual descriptions through AI technologies like Stable Diffusion and DALL-E, there are cases where these methods are insufficient. In such scenarios, it’s preferable to use random images from a personally curated collection of images, carefully selected from stock resources or created using services such as Midjourney. Note that each content source (feed) can have its own unique set of images.
Bearing these considerations in mind, new functionality has been introduced in CyberSEO Pro version 10.121 and RSS Retriever version 1.015. The new template shortcode %random_file[path]%
is now available. This shortcode should be used with the string value path, representing the absolute path to the server folder containing your image files. You can employ it in your post template as follows:
<img src="%random_file[/var/web/data/www/mysite.com/images]%">
Or, for instance, like this:
<img src="%random_file[/var/web/data/www/mysite.com/images]%" alt="%post_title%">
In the example above, it’s implied that /var/web/data/www/mysite.com/ is the path to the root directory of your WordPress site on the server, and /images is its internal directory containing your image files. Thus, the shortcode %random_file[path]%
will be replaced with the URL of a random image from this /images subdirectory.
Keep in mind that the path /var/web/data/www/mysite.com/ is used here purely for demonstration purposes. With your site, this path will be different. Most likely, you might not even know the exact subdirectory on the server where your site’s files are located. In this case, simply open the CyberSEO Pro General Settings page and find the “WordPress root directory” text field. This is the absolute path to your site’s subdirectory on the server.
You may have noticed that the image subdirectory in the %random_file[path]%
shortcode is set as an absolute path on the server, but in the generated posts, images are inserted as URLs. That’s correct; the plugin automatically converts the absolute path of the image file into its corresponding URL. In this context, there’s an important nuance to be aware of. The fact is that the directory with image files can be located either within your site’s file structure or outside of it. Let’s examine both scenarios in more detail.
1. Image files located within the site’s file structure, for example:
/var/web/data/www/mysite.com/images/house.jpg
/var/web/data/www/mysite.com/images/car.png
…
In this case, when inserted into a post, they will be replaced with the following URLs:
https://www.mysite.com/images/house.jpg
https://www.mysite.com/images/car.png
…
If you enable the “Store images locally” option in the feed settings, the image files will be copied to the /wp-content/uploads folder and renamed using the name of the current WordPress post being processed.
2. Image files located outside the site’s file structure, for example:
/home/alex/pictures/house.jpg
/home/alex/pictures/car.png
…
Since this directory is located outside the website’s file structure (the server directory /home/alex/pictures/ is not accessible from the Web), the files cannot be directly converted into URL addresses. Therefore, when inserting an image into a WordPress post, its file will always be automatically copied to the /wp-content/uploads folder. After this step, the plugin will then generate its URL address.
Furthermore, in the case of external files, their names will not be renamed using the name of the current WordPress post being processed. Their copies in the /wp-content/uploads folder will retain their original names – house.jpg and car.png.
Thus, by using both internal and external directories for storing image files, and by toggling the “Store images locally” option on or off, you can achieve the most suitable result for your needs. For example, you can use the same image files in different posts without creating additional copies in the /wp-content/uploads folder; you can either keep their original names or rename them to match the title of the containing post for better SEO.
Source: https://www.cyberseo.net/blog/how-do-i-insert-a-random-image-from-a-folder-into-a-wordpress-post/