Is your filter going to break the 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 filterFilter Filters are one of the two types of Hooks They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. 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 (headerHeader The header of your site is typically the first thing people will experience. The masthead or header art located across the top of your page is part of the look and feel of your website. It can influence a visitor’s opinion about your content and you/ your organization’s brand. It may also look different on different screen sizes., body, footer, etc.), and even in the admin back-end. More importantly, filters do not send anything to the webpage! Filter hooksHooks In WordPress theme and development, hooks are functions that can be applied to an action or a Filter in WordPress. Actions are functions performed when a certain event occurs in WordPress. Filters allow you to modify certain functions. Arguments used to hook both filters and actions look the same. 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.

A good filter hook:

function my_filter_hook( $text ) {
    $text .= 'Adding some text.';
    return $text;

A bad filter hook:

function my_filter_hook( $text ) {
    echo 'Adding some text.';
    return $text;

How common is this problem?

Unfortunately, it’s much more common that you might think. The the_content filter, for example, is often a source of problems – developers may think their content filter hook is executed only once, when WordPress includes the post content, but post content may be required in header metaMeta Meta is a term that refers to the inside workings of a group. For us, this is the team that works on internal WordPress sites like WordCamp Central and Make WordPress. tags, widgets, in the footer, or even filtered in the admin back-end. The the_content filter may even be used on completely unrelated text, to expand shortcodes or format text. If you’re sending anything to the webpage from a filter hook, you’re doing it wrong – use an action instead.

How do you know if you have a badly coded filter?

If you activate a pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the Plugin Directory or can be cost-based plugin from a third-party that uses the the_content filter (as one example), and the webpage layout is affected, you may have an incorrectly coded filter hook. This problem is so common that plugin authors often avoid using the the_content filter for that very reason.

This problem can be frustrating for end-users to diagnose as well – often the only way is to start disabling plugins / change themes until the problem goes away, and reporting the problem to the plugin / theme author can also be challenging since it may, or may not, be caused by a filter hook – and if it is, determining which filter is affected can require some coding effort / knowledge.

This problem is so common, and so challenging for users to diagnose, that I even wrote a plugin specifically to fix and report which content filter hooks incorrectly send output to the webpage.

#action, #doingitwrong, #filter, #hook, #output