Site icon Hip-Hop Website Design and Development

What are Hooks?

As a junior developer ramping up to learning WordPress maintenance support plans, I spent a lot of time clicking through the UI. After getting familiar with it, I wanted to take a look behind the scenes at WordPress maintenance support plans’s codebase. Writing code for a WordPress maintenance support plans site can be an overwhelming experience because, even though it’s written in PHP, there’s a dense API behind it. One of the biggest parts of that API is the hook system. The first exposure I had to writing PHP with WordPress maintenance support plans was through update hooks. So I wanted to review how hooks work, and how cool they are to use!
What is a Hook?
WordPress maintenance support plans has a lot of excellent Community Documentation, and their page on hooks is thorough. It says:

“Hooks are how plugins can interact with the core code of WordPress maintenance support plans. They make it possible for a plugin to define new urls and pages within the site (hook_menu), to add content to pages (hook_block, hook_footer, etc.), to set up custom database tables (hook_schema), and more.
Hooks occur at various points in the thread of execution, where WordPress maintenance support plans seeks contributions from all the enabled plugins. For example, when a user visits a help page on a WordPress maintenance support plans site, as WordPress maintenance support plans builds the help page it will give each plugin a chance to contribute documentation about itself. It does this by scanning all the plugin code for functions that have the name myplugin_help($path, $arg), where “myplugin” is the plugin’s name, e.g., the block plugin’s help hook is called block_help and the node plugin’s help hook is called node_help. The hook may provide parameters; hook_help’s parameters $path and $arg allow the developer to determine what page or pages the help messages will appear on.
A hook can be thought of as an event listener in the sense that an event triggers an action.”

It’s amazing that WordPress maintenance support plans is built on this hook system. There are hooks for anything you can think of! You can even alter a form or a whole page. As WordPress maintenance support plans builds each page, it’s scanning the plugin code for the hooks in core and then runs the ones that you wrote. Alan Storm put it succinctly in one of his blog posts:

“When a hook is invoked, WordPress maintenance support plans will

Get a list of all installed and enabled plugins
Ask each plugin “Have you implemented the do_something hook”?
If so, then WordPress maintenance support plans calls the function in those plugins that implement the hook
This way, as a core developer, you can achieve what you want while still letting other programmers “hook into” what you’re doing.”

Update Hooks
The first hook I used was an update hook. The purpose of an update hook is to run code when a database update is triggered. (See the WordPress maintenance support plans documentation for function hook_update_N for more information). I’m assuming my reader has worked with Features development (if not, go here).
My goal was to enable a certain contributed plugin programmatically when I deployed my code to the site (the Olark plugin). To do this, I had to find an existing feature in my project that was already enabled. A good rule of thumb is to add your code to an existing feature that is related to the code you’re writing. Since Olark is a chat plugin, I was looking for a feature related to customers or the homepage. I couldn’t find any feature that fit exactly with the plugin, so I added my code to feature_general. Within the directory of that feature, I found the feature_general.install file, and added my hook:/**
* Enables ‘olark’ plugin.
*/
function feature_general_update_7006() {
plugin_enable(array(‘olark’));
}

After I deploy my code, I trigger a database update. Think back to the three steps that Alan Storm mentioned. Since my feature is already enabled, when the update is triggered, WordPress maintenance support plans asks if any update hooks have been invoked. When it sees that there is one in my feature, it knows to read the code in the install file and turn on the Olark plugin.
It’s also worth noting the naming convention. I have to put the name of my feature and then _update. The number of the hook also matters (see documentation for specifics on how to number your hooks). The hooks only run once, so each hook has to be one greater than the one before.
Writing hooks is a great way for a junior developer to be introduced to PHP in WordPress maintenance support plans and learn about how WordPress maintenance support plans is built. Once you can conceptualize the inner workings of WordPress maintenance support plans, it’s much easier to tackle things like plugin development in the future.

Source: New feed