Hooks in WordPress
Continuing the series of articles, for writing plugins WordPress I want to show some more useful code snippets.
Begin, as in previous articles. Create a file in the plugins folder and name it mytestplugin.php. Add the code to the generated file :
<?php /* Plugin Name: Название плагина Plugin URI: https://страница_с_описанием_плагина_и_его_обновлений Description: Brief description of the plugin. Version: Plugin version number, for example: 1.0 Author: Имя автора плагина Author URI: https://plugin_author_page */ ?>
Now we have inserted the minimum, which is needed to install and activate the plugin, then often after development it is necessary to think about a license for it. Many developers use the license GPL or similar to it. To describe the license, add the following lines to the plugin file:
<?php
/* COPYRIGHT YEAR PLUGIN_AUTHOR_NAME (email: E-MAIL_AUTHORDER)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>
Moving on to plugin programming and basic important concepts.
Consider the basic concept of plugin development, which works on the basis of hooks or they are also called hooks (hook). How hooks work is very simple., each of the elementary functions WordPress before returning some result of your work or performing some important action (database query, data processing, output on display), tries to use additional lines of code intended for it in plugin files.
An example of using hooks in WordPress
<?php function get_the_title( $id = 0 ) { … … return apply_filters( 'the_title', $title, $post->ID ); //Example of a hook for the get_the_title function(); } ?>
Before as WordPress adds a title to a post, first he checks, does any plugin have registered functions for a hook called "the_title».
An example of using such a hook in a WordPress plugin
<?php
…
add_filter( 'the_title', 'my_own_function_for_title' ); //This is what it looks like to register a new function in the plugin file my_own_function_for_title(); with additional instructions for catching 'the_title'.
…
?>The title text before the output will be passed through a special function my_own_function_for_title() specified in the plugin file or in the file function.php, after which the result will be displayed.
An example of how the my_own_function_for_title function works()
<?php
…
/* This is how the plugin may look like a function, changing WordPress titles. in this case it forces you to display each word of the title with a capital letter.
*/
my_own_function_for_title( $title ){
$title = ucwords($title);
return $title;
}
…
?>If you need to add some information to the header or change it, the filter hook for "the_title” and it contains the function, which makes all the necessary changes to the headers.
All hooks in WordPress are divided into two categories - Filters and Actions. (filters and actions respectively).
First clues filters (filters) really meant forfiltering» (changes) any data, before they are rendered on the page or added to the database for storage. This is spam filtering., errors or just erroneous input in forms.
Second (actions, actions) designed to replace various kernel actions with your own actions (for example, database query string changes), in programming, such a change in the actions of the basic functionality is also called overload.
In WordPress have your own API for working with plugins and a number of ready-made hooks. You can read about all standard hooks in the official documentation WordPress Plugin API true in english. And if you find a place, where such a hook is simply necessary, then you can offer it and how to do it is written here.
The information is taken from the official documentation and slightly processed by me. ). Good luck developing your own WordPress plugins. Stay tuned for new articles.
/*

- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.




