waughj/wp-theme-image

Class for autogenerating HTML for WordPress theme image.

v0.4.0 2019-08-28 21:16 UTC

This package is auto-updated.

Last update: 2024-04-29 03:30:07 UTC


README

Class for autogenerating HTML for WordPress theme image.

Works just like HTMLImage class, but autogenerates the FileLoader to fit WordPress’s theme directory.

Local directory within theme directory can be set on an image-by-image basis by setting the “directory” key in the $attributes hash map to the desired local directory string.

For example, in a theme named “blue” setup on localhost:

use WaughJ\WPThemeImage\WPThemeImage;

$image = new WPThemeImage( 'demo.png', [ 'directory' => 'img' ] );
$image->print(); // Will print <img src="http://localhost.com/wp-content/themes/blue/img/demo.png?m=4389109" alt=""> ( #s after m= will vary ).

Or you can globally set the default local directory with the static method setDefaultSharedDirectory():

use WaughJ\WPThemeImage\WPThemeImage;

$image = new WPThemeImage( 'demo.png' );
$image->print(); // Will print <img src="http://localhost.com/wp-content/themes/blue/demo.png?m=4389109" alt=""> ( #s after m= will vary ).

WPThemeImage::setDefaultSharedDirectory( 'pictures' );

$image = new WPThemeImage( 'demo.png' );
$image->print(); // Will now print <img src="http://localhost.com/wp-content/themes/blue/pictures/demo.png?m=4389109" alt=""> ( #s after m= will vary ).

Error Handling

As a child o’ HTMLImage, WPThemeImage will throw an exception if it’s set to show versioning ( default ) & can’t find the file on the server. Read the HTMLImage documentation to learn mo’ ’bout this. WPThemeImage works the same way:

try
{
	$image = new WPThemeImage( 'missing.jpg' );
}
catch ( MissingFileException $e )
{
	$image = $e->getFallbackContent();
}
$image->print() // Will print <img src="http://localhost.com/wp-content/themes/blue/missing.jpg" alt=""> without throwing an error.

Changelog

0.4.0

  • Add function for getting URL directory.

0.3.0

  • Integrate HTMLImage Autogeneration o’ Sizes Attribute & Shorthand Srcset Format

0.2.0

  • Apply HTMLImage HTML Cache Optimization

0.1.0

  • Initial Release