Skip to content

Instantly share code, notes, and snippets.

@quietcactus
Last active February 5, 2026 16:22
Show Gist options
  • Select an option

  • Save quietcactus/e117b13d6bea7f4d2fca5e490ea0af7e to your computer and use it in GitHub Desktop.

Select an option

Save quietcactus/e117b13d6bea7f4d2fca5e490ea0af7e to your computer and use it in GitHub Desktop.
Function that creates a custom URL for a placeholder image. Utilizing placehold.co
<?php
/**
* Generates a dynamic URL for a placeholder image.
*
* @param int $width The width of the image. Default: 600.
* @param int $height The height of the image. Default: 400.
* @param string $backgroundColor The background color of the image. Default: 'black'.
* @param string $textColor The text color of the image. Default: 'white'.
* @param string $text The text to display on the image. Default: 'Placeholder'.
* @param author Ian Garcia
*
*/
function the_placeholder_image(int $width = 600, int $height = 400, string $backgroundColor = 'black', string $textColor = 'white', string $text = '') {
$placeholder = sprintf('https://placehold.co/%dx%d/%s/%s?text=%s', $width, $height, $backgroundColor, $textColor, urlencode($text));
echo $placeholder;
}
function get_the_placeholder_image(int $width = 600, int $height = 400, string $backgroundColor = 'black', string $textColor = 'white', string $text = '') {
$placeholder = sprintf('https://placehold.co/%dx%d/%s/%s?text=%s', $width, $height, $backgroundColor, $textColor, urlencode($text));
return $placeholder;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment