Hot Random Image Shortcode

Below, you’ll find a detailed guide on how to add the Hot Random Image Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Hot Random Image Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Hot Random Image Plugin and the shortcodes it provides:

Plugin Icon
Hot Random Image

"Hot Random Image is a dynamic WordPress plugin that enhances your website's visual appeal by displaying a random image from your gallery each time your page is loaded."

★★★★☆ (7) Active Installs: 3000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [randomimage]

Hot Random Image [randomimage] Shortcode

The Hot Random Image shortcode displays a random image from a specified directory. It allows customization of image dimensions and alt text. It merges arrays of different image file types from the specified path. A random image is then selected and displayed with the defined width, height, and alt text. If a link is provided, the image becomes clickable, redirecting to the given URL.

Shortcode: [randomimage]

Parameters

Here is a list of all possible randomimage shortcode parameters and attributes:

  • path – Sets the directory from where the images will be fetched
  • width – Defines the width of the displayed image
  • height – Sets the height of the displayed image
  • alt – Provides a text alternative for the image
  • link – Specifies a URL where the image links to

Examples and Usage

Basic example – The shortcode is used to display a random image from the images/random directory with the default width, height, alt text and without a link.

[randomimage /]

Advanced examples

Using the shortcode to display a random image from a specific directory (images/special), with a defined width and height, and a specific alt text. The image will not be linked.

[randomimage path="images/special" width="300px" height="200px" alt="Special image" /]

Using the shortcode to display a random image from a specific directory (images/banner), with a defined width and height, a specific alt text, and a link that redirects to a specific page when the image is clicked.

[randomimage path="images/banner" width="500px" height="300px" alt="Banner image" link="https://example.com/banner-page" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [randomimage] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'randomimage', 'randomimage_func' );

Shortcode PHP function:

                    function randomimage_func( $atts ) {
	$a = shortcode_atts( array(
		'path' => 'images/random',
		'width' => '100%',
		'height' => 'auto',
		'alt' => '',
		'link' => ''
	), $atts );

	$images1 = glob($a['path'].'/*.jpg');
	$images2 = glob($a['path'].'/*.png');
	$images3 = glob($a['path'].'/*.gif');
	$images4 = glob($a['path'].'/*.jpeg');
	$images5 = glob($a['path'].'/*.svg');
	$images6 = glob($a['path'].'/*.JPG');
	$images7 = glob($a['path'].'/*.PNG');
	$images8 = glob($a['path'].'/*.GIF');
	$images9 = glob($a['path'].'/*.JPEG');
	$images10 = glob($a['path'].'/*.SVG');

	$images = array();
			
	if(!empty($images1))
		$images = array_merge($images,$images1);
	if(!empty($images2))
		$images = array_merge($images,$images2);
	if(!empty($images3))
		$images = array_merge($images,$images3);
	if(!empty($images4))
		$images = array_merge($images,$images4);
	if(!empty($images5))
		$images = array_merge($images,$images5);
	if(!empty($images6))
		$images = array_merge($images,$images6);
	if(!empty($images7))
		$images = array_merge($images,$images7);
	if(!empty($images8))
		$images = array_merge($images,$images8);
	if(!empty($images9))
		$images = array_merge($images,$images9);
	if(!empty($images10))
		$images = array_merge($images,$images10);
		
	$ind = rand(1,count($images));
	$image = $images[$ind - 1];

	$html = '';

	if($image){
		if($a['link']){
			$html .= '<a href="'.$a['link'].'">';
		}
			$html .= '<img width="'.$a['width'].'" height="'.$a['height'].'" class="hot-random-image" src="'.get_site_url().'/'.$image.'" alt="'.$a['alt'].'" />';

		if($a['link']){
			$html .= '</a>';
		}
	}

	return $html;
}
                    

Code file location:

hot-random-image/hot-random-image/hot_random_image.php

Conclusion

Now that you’ve learned how to embed the Hot Random Image Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *