{"id":341442,"date":"2021-06-02T06:35:53","date_gmt":"2021-06-02T13:35:53","guid":{"rendered":"https:\/\/css-tricks.com\/?p=341442"},"modified":"2022-11-03T12:43:28","modified_gmt":"2022-11-03T19:43:28","slug":"a-crash-course-in-wordpress-block-filters","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/a-crash-course-in-wordpress-block-filters\/","title":{"rendered":"A Crash Course in WordPress Block Filters"},"content":{"rendered":"\n
Blocks in WordPress<\/a> are great. Drop some into the page, arrange them how you like, and you\u2019ve got a pretty sweet landing page with little effort. But what if the default blocks in WordPress need a little tweaking? Like, what if we could remove the alignment options in the Cover block settings? Or how about control sizing for the Button block?<\/p>\n\n\n\n There are plenty of options when it comes to extending the functionality of core blocks in WordPress. We can add a custom CSS class to a block in the editor, add a custom style, or create a block variation<\/a>. But even those might not be enough to get what you need, and you find yourself needing to filter the core block to add or remove features, or building an entirely new block from scratch.<\/p>\n\n\n\n I\u2019ll show you how to extend core blocks with filters and also touch on when it\u2019s best to build a custom block instead of extending a core one.<\/p>\n\n\n\n\n\n\n Before we dive in, I\u2019d like to note that code snippets in this article are taken out of context on purpose to focus on filters rather than build tools and file structure. If I included the full code for the filters, the article would be hard to follow. With that said, I understand that it\u2019s not obvious for someone who is just starting out where to put the snippets or how to run build scripts and make the whole thing work.<\/p>\n\n\n\n To make things easier for you, I made a WordPress plugin with examples from this article available on my GitHub. Feel free to download it<\/a> and explore the file structure, dependencies and build scripts. There is a README<\/a> that will help you get started.<\/p>\n\n\n The concept of filters is not new to WordPress. Most of us are familiar with the A simple example of a PHP filter could look something like this:<\/p>\n\n\n\n In this snippet, we create a function that receives a string representing a post title, then wrap it in a JavaScript filters work in a similar way. There is a JavaScript function called Looks pretty similar, right? One notable difference is What makes JavaScript filters challenging to understand and use is the different nature of what they can filter. Some filter strings, some filter JavaScript objects, and others filter React components and require understanding the concept of Higher Order Components<\/a>.<\/p>\n\n\n\n On top of that, you\u2019ll most likely need to use JSX which means you can\u2019t just drop the code into your theme or plugin and expect it to work. You need to transpile it to vanilla JavaScript that browsers understand. All that can be intimidating at the beginning, especially if you are coming from a PHP background and have limited knowledge of ES6, JSX, and React.<\/p>\n\n\n\n But fear not! We have two examples that cover the basics of block filters to help you grasp the idea and feel comfortable working with JavaScript filters in WordPress. As a reminder, if writing this code for the Block Editor is new to you, explore the plugin<\/a> with examples from this article.<\/p>\n\n\n\n Without any further ado, let\u2019s take a look at the first example.<\/p>\n\n\n We\u2019re going to filter the core Cover block and remove the Left<\/em>, Center<\/em>, Right<\/em>, and Wide<\/em> alignment options from its block settings. This may be useful on projects where the Cover block is only used as a page hero, or a banner of some sort and does not need to be left- or right-aligned.<\/p>\n\n\n\nA quick note on these examples<\/h3>\n\n\n
Block filters in an nutshell<\/h3>\n\n\n
add_filter()<\/a><\/code> function in PHP. It allows developers to modify various types of data using hooks<\/a>.<\/p>\n\n\n\n
function filter_post_title( $title ){\n return '<strong>' . $title . '<\/strong>';\n};\n\nadd_filter( 'the_title', 'filter_post_title' );<\/code><\/pre>\n\n\n\n
<strong><\/code> tag and return a modified title. We then use
add_filter()<\/code> to tell WordPress to use that function on a post title.<\/p>\n\n\n\n
addFilter()<\/code> that lives in the
wp.hooks<\/code> package and works almost like its PHP sibling. In its simplest form, a JavaScript filter looks something like this:<\/p>\n\n\n\n
function filterSomething(something) {\n \/\/ Code for modifying something goes here.\n return something;\n}\n\nwp.hooks.addFilter( 'hookName', 'namespace', filterSomething );<\/code><\/pre>\n\n\n\n
addFilter()<\/code> has a
namespace<\/code> as a second argument. As per the WordPress Handbook<\/a>, \u201cNamespace uniquely identifies a callback in the the form
vendor\/plugin\/function<\/code>.\u201d However, examples in the handbook follow different patterns:
plugin\/what-filter-does<\/code> or
plugin\/component-name\/what-filter-does<\/code>. I usually follow the latter because it keeps the handles unique throughout the project.<\/p>\n\n\n\n
Removing the Cover block\u2019s alignment options<\/h3>\n\n\n