Skip to main content

How to load a custom WP_Image_Editor

In our previous post about the class WP_Image_Editor we only talked about some global information. Today this post is going to handle how to load a custom image editor. It isn’t that straight forward you would expected and the reason is that the the code in core is lazy loaded. So when you call wp_get_image_editor() the classes WP_Image_Editor, WP_Image_Editor_GD and WP_Image_Edtor_Imagick getting loaded.

This means that you need to use the filter ‘wp_image_editors’ to load your own files and use require_once since the filter will be called multiple times. For the plugin Gmagick there is a different approach because it’s going to be included in 3.6.

function image_editors_add_gmagick( $editors ) {
	if( ! class_exists('WP_Image_Editor_Gmagick') )
		include_once 'editors/gmagick.php';

	if( ! in_array( 'WP_Image_Editor_Gmagick', $editors ) )
		array_unshift( $editors, 'WP_Image_Editor_Gmagick' );

	return $editors;
}
add_filter( 'wp_image_editors', 'image_editors_add_gmagick' );

So you see that we check if the class exists when we including the gmagick editor. Include_once can be just an include since we already doing a class check. After that we check if the editor was already added to the $editors array. This will happen when Gmagick will be included in core.

The custom editor

The custom editor you are going to write can extend WP_Image_Editor or WP_Image_Editor_GD/WP_Image_Editor_Imagick. In case of a new editor like Gmagick you include WP_Image_Editor. In case of extra functionality you will include the editor you want to extend.

See Gmagick plugin for how you can write a custom editor.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.