Customization of WordPress - how it works?

What is Wordpress and how it works?

WordPress is a typical example of CMS (Content Management System). CMS is written in PHP and uses a MySQL database. Its installation and configuration is very simple and requires no special programming knowledge.
It’s engine is constantly being developed by a large group of developers around the world. It is a completely free tool and because of that people working on its development are not gaining any profits from it.

The main purpose of WordPress is the creation and management of blogs. However, due to its very high functionality, ease of extending and wide range of ready to use public plugins, it is also used as a CMS to create both simple websites and large, complex portals with payment systems.

At this moment WordPress is the most popular content management system in the world - it is the most commonly chosen because of its speed and ease of use.

What are the characteristics of WordPress?

CMS stands out thanks to its very intuitive and easy to use interface of admin panel - it can be managed by almost everyone. The administrator can easily install new plugins and then use them without any problems. In addition to that a large number of plugins created by the community is complimentary, making it completely free for you to create your own blog or website with additional features. However, when creating something more unusual, the installation and configuration of additional plugins sometimes might not be enough. In that case we will need both programming knowledge of PHP and the WordPress engine, but the whole process will not be gruesome thanks to the help of actions, filters and WordPress API.

Actions and filters

WordPress has a complex system of creating extensions in form of actions and filters (also known as hooks). They allow developers of plugins and themes (skins) to change almost all functionalities of WordPress without interfering in its core (the most important rule: do not mess with the core!). It is a powerful and necessary mechanism which allows, among other things, to write simple additions without having to learn a complex API.

But let’s start at the beginning...

The first we have to do during rendering of each page is to run the core, which is the heart of WordPress. After that we have to run plugins and then, at the very end the template, which is defining how to display the website. During this process hundreds of operations are carried out, such as reading from the database, rendering menu, translation of texts, etc. In all of these operations additional actions and filters are performed. As the result during a specific operations we can take over the context of application for a moment and go through some additional steps, for instance changing the rendering of the menu.

The function performing the actions added in the module is

do_action('action_name', $parametr1, $parametr2, … );

On the other hand, the function performing all added filters (such as module) is

apply_filters('filter_name', $parametr1, $parametr2, … );

These functions are triggering all actions/filters connected to 'action_name' hook and 'filter_name'. They are used, for example, for writing official modules (created for the WordPress community). Users of such modules may also attach their hooks to them without the interference in their source code.

While executing do_action and apply_filters functions, all actions and filters added using chosen name are triggered.

Creating actions and filters

While creating motive or module we can easily attach custom functions to a particular hook. To do this we have to use functions:

  • add_action - attaching actions;
  • add_filter - attaching filters.

Example of usage of these functions

add_action('action_name', 'funtion_name', $priority, $accepted_args);add_filter('filter_name', 'function_name', $priority, $number_arguments);

Where:

  • 'action_name', 'filter_name' - name of action or filter, to which the function has to be attached with the name defined in the second parameter, which is 'function_name';
  • $priority - defines the weight of the hook in comparison with other hooks that are attached to the same event;
  • $accepted_args - this parameter defines the amount of arguments that 'function_name' takes.

Check out how it looks in this example

In the <head> section of WordPress theme we can find <title> tag that defines the name of website.

<title><?php wp_title('Website’s title'); ?></title>

To show how precise the hook system works in WordPress, to the wp_title(); function will be attached the filter that can change the value returned by this function:

function wp_title_home( $title )
{
if ( ( is_home() || is_front_page() ) ) {
// if we are on the homepage, then we have to change the site’s title
$title = __( Homepage, 'my_theme' );
}
return $title;
}

And then we have to attach the filter

add_filter( 'wp_title', 'wp_title_home' );

That’s all. Now during the viewing of the website’s homepage we receive it’s "Homepage" title, while other sub-pages will be called "Title of the page". This is a very trivial example, but it illustrates well how to use filters in WordPress.

function add_og_head() {
if (is_single())
{
print '<meta property="og:title" content="'.get_the_title().'">';
print '<meta property="og:url" content="'.get_permalink().'">';
}
}
/* [...] */
add_action( 'wp_head', 'add_og_head' );

This example illustrates action’s implementation and its attachment to the WordPress wp_head function, which prints tags from the head section of HTML site. The add_og_head function is triggered in the moment of rendering the suitable HTML for head section and adds the OpenGraph elements for Facebook.

Differences between actions and filters

From the description and examples mentioned above we can conclude that actions and filters operate in a similar manner. They are created and attached to the event in the same way, but can’t be seen as identical - the difference lies in the way they work. Filters, as the name suggests it, are used to filter data. The function attached as the filter receives data in the parameter, on which it performs certain operations and returns them afterwards. Input and output data must have the same structure and construction, what ensures the consistency. The role of the filter is only to transformate/filtrate the data and not anything else. That is their purpose and programmers should stick to it. Actions on the other hand are not returning any data and act inversely to the filter. The role of action is operating on the database, printing additional elements on the output and realization of many other tasks.

To sum up: if we want to change the processed data or filter it we should use filters. On the other hand, if we want to implement additional tasks, then we attach them to actions. Despite a similar structure of implementation of these hooks, there is a fairly large difference between them.

Why WordPress?

WordPress is the most popular CMS. It’s acclaim, ease of use, constant development and a huge variety of plugins or themes helped in gaining a good reputation.

There are many other Content Management Systems that are free of charge, such as Drupal or Joomla. WordPress, in comparison to Drupal, is more popular among users. What does it have that Drupal doesn’t? Check out this short comparison:

Discipline TechnologyEase of learning WordPressSafetyDrupalStabilityDrupalPerformanceDrupal (build-in cache)Speed of developmentWordPressSEOWordPressAmount of official plugins/modulesWordPress

This comparison shows that WordPress owes its victory to the fact that it is very easy to learn and quick to expand. Sites based on the WP engine are formed much faster compared to the Drupal-based ones. However, Drupal definitely excels in terms of performance and safety. Why users choose the speed and ease, not safety and efficiency? The answer to this question is left for you.

Navigate the changing IT landscape

Some highlighted content that we want to draw attention to to link to our other resources. It usually contains a link .