Click

Let's write a plugin for WordPress. Part 2. The general theory.

wordpress-plugin-2 In the first part of a series of publications on creating your own wordpress plugin I tried to answer the question "Why and how to write a plugin for WordPress?". Now it's time to get on the plan to create a plug-in. As an example, I rassmoryu plugin that will create the header and body to modify entries on the stage of its publication (ie, at a time when the user clicks the "Post" or "Reload"). In my opinion this approach is primarily interested in what will in the future on the basis of an example plug-in to write a more serious plugin for WordPress, for example - automatically changing content.

Let us proceed.

Contents of the course create a plugin for WordPress

Show Table of Contents »

  1. Introduction, general information (write a plugin for WordPress. Part 1.)
  2. The General Theory (write a plugin for WordPress. Part 2.)
  3. Preparing to Create the page settings (write a plugin for WordPress. Part 3.)
  4. To create a page plugin settings (write a plugin for WordPress. Part 4.)
  5. Functional component (write a plugin for WordPress. Chapter 5.)
  6. Internationalization and Russification (write a plugin for WordPress. Part 6.)
  7. Adding to the repository WordPress.org (write a plugin for WordPress. Chapter 7.)
  8. Source code file plugin for WordPress

The mechanism of the plug-in WordPress.

Consider to begin with an overview of the mechanism of any wordpress plugin to my plugin to write intelligently.


Catching events (hooks)

The mechanism of the plug-ins based on the interception and the further processing of user-defined function's engine - the so-called hukah (hook). Thus, to write a plugin that changes the entry in the publication, you must intercept sootvetstvubschee event, process it and return the result of the engine.

To trap events, you can use two methods:

  • add_action (overrides the basic function of a custom wordpress)
  • add_filter (works when you call the base function, calling for user-defined function)

Accordingly, the possible reverse action - remove_action and remove_filter.

In the plug-example I'll use the add_filter, because I only need to change the data postupyuschie for publication, the publication itself is the mechanism I do not want to touch (and do not need it).

Event Processing (function)

For treatment of captured events used functions (they are the same - the procedure). In the process of writing a plugin for WordPress we have not time to confront them. In php functions used to determine the official word function.

Start working on plug-in

Description of the plugin

The minimum non-functional part of the plugin - this is a summary of it, added to the top of the plugin file. As follows (in fairness I should note that really need to specify only the name of the plugin, but it is better to fill in more information, the more it will be useful in the future):

/ *
Plugin Name: My New Plagin
Plugin URI: http://www.dimio.org/my_plugin
Description: My plugin for WordPress.
Version: 0.1
Author: dimio
Author URI: http://www.dimio.org
* /

This section should be inserted in the beginning of the file plug-in, after coming off the tag php (<? Php).

Installation and removal wordpress plugin

When writing a plugin for WordPress to remember and for good reasons - namely, to use the tools to install and remove the plug. Some features (such as creating tables with data for the plugin to work, adding new features that are used by the plugin to the appropriate database table wordpress) should only be done once - when you install the plugin. Well, it is removed to clean up for you - remove the installed options and created the table. For these purposes, the interceptors are wordpress

register_activation_hook (__FILE__, 'myplug_set_options');
register_deactivation_hook (__FILE__, 'myplug_unset_options');

Let me explain: when you install the plug-in will be called myplug_set_options, which we will describe the process of creating the necessary settings, but what if you delete all the settings plug-in will call unichtozheyn myplug_unset_options. Of course, the names of called functions can be arbitrary. The above two lines should be placed after the description of the plugin, then there is the whole code is as follows:

<? Php
/ *
Plugin Name: My New Plugin
Plugin URI: http://www.dimio.org/myplugin
Description: My plugin
Version: 0.1
Author: dimio
Author URI: http://www.dimio.org
* /
, 'myplugin_set_options' ) ; register_activation_hook (__FILE__, 'myplugin_set_options');
, 'myplugin_unset_options' ) ; register_deactivation_hook (__FILE__, 'myplugin_unset_options');
?>

Write a plugin for WordPress is really easy!

On this happy note, I would like to finish some general theoretical and subsequently go on to consider the establishment of a plug-in settings page. In conclusion, I recommend see the list of events wordpress, which you can install the interceptor.

More on similar topics:

Category Filed under: SEO , Blogging , Coding | Tag Tags: , , , , , | Comments 4 comments

Comments

4 comments to "write a plugin for WordPress. Part 2. The General Theory. "

  1. karser writes:

    typo
    settings plugin will call unichtozheYN myplug_unset_options

    • dimio writes:

      Some bloggers make errors especially in order to increase "uniqueness" of the text in the eyes of search engines, so I will not deny the right of existence of the clerical errors, once she slipped through the spell checker :)

  2. Hilarion writes:

    Well this is something dear author :-) Yet very little information and a lot of unnecessary words. Try to write already, without much talk, but it was interesting to read :-)

Leave a Reply