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 :
1 2 3 4 5 6 7 8 9 10 |
<?php /* Plugin Name: Название плагина Plugin URI: https://страница_с_описанием_плагина_и_его_обновлений Description: Краткое описание плагина. Version: Номер версии плагина, например: 1.0 Author: Имя автора плагина Author URI: https://страница_автора_плагина */ ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /* Copyright ГОД ИМЯ_АВТОРА_ПЛАГИНА (email: E-MAIL_АВТОРА) 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
1 2 3 4 5 6 7 |
<?php function get_the_title( $id = 0 ) { … … return apply_filters( 'the_title', $title, $post->ID ); //Пример зацепки для функции get_the_title(); } ?> |
An example of using such a hook in a WordPress plugin
1 2 3 4 5 |
<?php … add_filter( 'the_title', 'my_own_function_for_title' ); //Так выглядит регистрация в файле плагина новой функции my_own_function_for_title(); с дополнительным инструкциями для зацепки 'the_title'. … ?> |
An example of how the my_own_function_for_title function works()
1 2 3 4 5 6 7 8 9 10 |
<?php … /* Так в плагине может выглядеть функция, изменяющая заголовки Wordpress. В данном случае она заставляет выводить каждое слово заголовка с большой буквы. */ my_own_function_for_title( $title ){ $title = ucwords($title); return $title; } … ?> |
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.