Remove WordPress Action / Filter Hooks by Name

The WordPress remove_action() and remove_filter() functions are useful, but they require the original hook priority and a function name, a class object and method name, or a class name and static method name.

Providing the class object in particular may be difficult or impossible, as plugin and theme classes may be extended and instantiated any number of ways. The following SucomUtilWP::remove_filter_hook_name() static method can be used to remove WordPress action and filter hooks using their class name instead of requiring a class object.

For example, if an action hook like this one has been added within a class object that was not saved, saved in a private/protected variable, or cannot be re-instantiated, then removing it might be impossible.

If you know the class (or extended class) name (‘Plugin_Namespace\Class_Name’ for this example), you can remove it using the following static method:

Continue reading

Choosing your PHP Image Extension for WordPress

WordPress supports two different PHP image processing extensions — ImageMagick and GD — with a preference for ImageMagick over GD (even when ImageMagick is not installed). The GD extension can still be used in cases where the preferred WordPress ‘WP_Image_Editor_Imagick’ class does not provide support for the requested mime-type or class method.

In some cases, the ImageMagick extension might not be installed, or might be unreliable (old versions of ImageMagick can be buggy). You can hook the WordPress ‘wp_image_editors’ filter to manage the preferred order of WordPress image classes.

For example, here’s a filter and function to remove the ImageMagick class altogether:

Continue reading

Fix “sslverify=false” for Pro Plugin / Theme Updates

This is a pet peeve of mine – some plugin and theme authors (to make their lives easier) set “sslverify = false” in their Pro / Premium version update checks and/or other HTTP requests.

Luckily a few security and error checking plugins like the Query Monitor plugin (for example), will show a warning if the WordPress wp_remote_get() function is executed with “sslverify = false”. To make sure this is never the case, you can use the following filters in your functions.php file.

Continue reading

Is your filter going to break the WordPress layout?

If you’re not clear about the difference between WordPress actions and filters, you may end up breaking the page layout of WordPress – maybe days, months, or even years after you’ve written and implemented a new filter hook. The difference can be difficult for new developers to grasp – after all, hooking an action or filter runs your code, either way, right? Well, yes, it does, but filters can be executed several times, in different locations as the webpage is being built (header, body, footer, etc.), and even in the admin back-end. But more importantly, filters do not send anything to the webpage! Filter hooks receive their data / text as an argument, and then “return” the modified (or original) data / text at the end. They do not use “echo”, “print”, “printf”, etc. – they should not send anything to the webpage. If you need to output something directly to the webpage, use an action – that’s what they’re for. ;-)

Continue reading

PHP Code to Clear the WPSSO Cache

If you use a caching plugin, you may have an option to include custom PHP code when flushing the cache manually — for example, Comet Cache has an “Evaluate Custom PHP Code when Clearing the Cache?” option where you can enter additional PHP code to execute when clearing the cache manually. Adding the following code will also clear the WPSSO object cache when clearing the webpage cache:

Continue reading

PHP – Print a Prettier Array

I often want to output an array for debugging purposes, but using var_dump() or print_r() on an array that includes true / false values and class objects can be problematic — false values appear empty, and class objects can include too much information. I wrote the following recursive static method (presented here as a function) to pre-filter an array for readability when using print_r() or var_dump().

Continue reading

NextGEN Gallery v2 Resize Issue Persists

If you are concerned about the quality of your NextGEN Gallery v2.x resized images, this should be of particular interest to you.

Some months ago I contacted Photocrati to ask them how, in version 2.x, developers could retrieve the “actual” dimensions of a resized image. In the past, after resizing an image we could use PHP’s getimagesize() function on the resulting file, but in v2.x image resizing is dynamic and those resized images may not be available on disk. I had some concerns between the expected / calculated image dimensions, those returned by NGG v2.x’s methods / functions, and the actual image retrieved by the URL. All 3 dimensions were different! A resized uncropped image which should have been 300x200px, was reported as being 300x199px by the NGG methods / functions, and the image retrieved by URL was 298x199px!

This may not sound like much, but a few pixels here and there can lead to image distortion when rendered by browsers, alignment issues in page layouts, and failures when working with minimum image dimensions (for example, Facebook ignores images smaller than 200x200px).

Continue reading