Wordpress Plugin Development Guide

Source: Internet
Author: User
Tags coding standards function examples install wordpress wordpress database wordpress blog
& Nbsp; 1. introduction Wordpress plug-in allows you to modify, define, and enhance blog functions in a simple way. You can add features directly to WordPress through plug-ins without modifying the core code of WordPress. The following is a basic definition of the WordPress plug-in: can a WordPress plug-in be a program? I. INTRODUCTION

The WordPress plugin allows you to modify, define, and enhance the functions of a blog in a simple way. You can add features directly to WordPress through plug-ins without modifying the core code of WordPress. The following is a basic definition of the WordPress plug-in:

WordPress Plugin:The WordPress plug-in can be a program or a group of functions written in PHP. It can use a series of methods and interfaces provided by the plug-in API to add some specific functions or services to the WordPress blog and make them look like the original functions of WordPress.

Want to add some new or different features to your blog? You can search for the WordPress plug-in library to see if someone has developed a WordPress plug-in that meets your requirements. If unfortunately-no, this article will guide you to develop one by yourself.

Resources
  • If you want to know how to install WordPress plug-ins and how they work, go to the plug-in resource collection and check out the articles and resources of many plug-ins developers, including how to compile WordPress plug-ins, and other articles on specific topics.
  • If you want to know how the WordPress plug-in is compiled, you can view the source code of some plug-ins, such as the Hello Dolly plug-in WordPress.
  • If your plug-in has been written and feels good, you can read plug-in submission and promotion to learn how to publish it and share your labor results with others.
2. create a plug-in

This part shows you how to turn the development plug-in into reality.

1. name, file, and location

Name

First, you need to think about what this plug-in is used for, and then you can give it a unique name. If you are not sure whether the name has been used, you can search by Google or other means. Most plug-in developers can intuitively describe the functions of the plug-in. for example, the name of a plug-in related to weather should contain the word "weather. The plug-in name can contain multiple words.

File

The next step is to create a main PHP file based on your plug-in name. For example, if the plug-in name is "Fabulous Functionality", what is the name of the main PHP file?"Functionality. php"Of course, pay attention to the problem of duplicate names. Because when you install your plug-in, your plug-in will be installed toWp-content/plugins/If the file names of the two plug-ins conflict with each other in the directory.

Your plug-in should contain at least one main PHP file (you can also split it into multiple files), as well as Javascript files, CSS files, image files, and language files. If your plug-in contains multiple files, you also need to create a folder and put all the files contained in the plug-in into this folder, so that you only need to let others put the entire folderWp-content/plugins/Directory. The name of the plug-in folder is usually the same as the name of the plug-in php file. for example, if the name of the php file is functionality. php, the name of the folder can be called functionality.

Note that you can configureWp-content/plugins/Directory, so you must use the plugin_dir_path () and plugins_url () functions to obtain the plug-in path.

View http://codex.wordpress.org/Determining_Plugin_and_Content_Directories? For more information

  • Later in this article, if we mention the "PHP main file" concept, it refers to the most important PHP file in the plug-in. This file is usually located inWp-content/plugins/Directory or its subdirectories.

README file

If you want to publish your plug-in to the http://wordpress.org/extend/plugins/, you must create a standard format in the plug-in packageReadme.txtFile. file format see http://wordpress.org/extend/plugins/about/readme.txt.

You can access http://wordpress.org/extend/plugins/about/readme.txt? View how to format the README file or access the http://generatewp.com/plugin-readme? To use the automatic document generator

It should be noted that WordPress judges whether the plug-in is in the "necessary" or "test" state through the self-report file.

Home Page

It is very useful to create a home page for your plug-in. on the plug-in home page, you can introduce the plug-in functions, installation methods, instructions for use, applicable WordPress versions, and plug-in update information.

2. file header

Now, let's add some information to the main PHP file.

Standard plug-in information

The top of the main file of the plug-in must contain a standard plug-in information header. WordPress identifies the existence of the plug-in through the standard information header and adds it to the plug-in management page of the control panel so that the plug-in can be activated, loaded into the plug-in, and run the functions in it; if no information header is available, the plug-in cannot be activated or used. The format of the standard information plug-in header is:

 

The standard information header must contain at least the plug-in name so that WordPress can identify your plug-in. Other information is displayed on the control panel plug-in management page. Standard plug-in information has no requirements on the order of each line.

Copyright information

We usually need to add the license information of the plug-in to the standard information header. Most plug-ins use the GPL or GPLCompatibleLicenses license. If you use the GPL license, the plug-in must contain the following information:

 
3. start writing plug-ins

Now it's time for your plug-in to do something. This part includes the general idea of plug-in development and the work to be done.

WordPress plugin hook

In WordPress, "plug-in hook" is a very important concept. Many WordPress plug-ins are associated with plug-in hooks to complete their functions. How plug-in hooks work: during WordPress running, there are many specific time points, WordPress will detect the corresponding plug-in hooks at these time points, if a function is detected to be associated with the current plug-in Hook, these functions will be run. These functions change the default features of WordPress.

For example, before WordPress publishes an article, it first detects a plug-in Hook named "the_title" (a filter hook ), if there are any plug-in functions associated with this hook, the title of the article will be first processed by these functions in turn, and then the function processing result will be displayed on the screen. Therefore, if your plug-in wants to process the title of the article, you need to register the corresponding processing function to the filter hook named "the_title.

For another example, WordPress is about to generateBefore the tag, a plug-in Hook named "wp_footer" (Action type hook) will be detected. if a plug-in function is associated with this hook, then WordPress will execute these functions in sequence before generatingLabel.

If you want to know the differences between action type hooks and filter hooks, how to register functions with hooks, and when WordPress will call plug-in hooks, you can refer to the Plugin API. If you find a time point for processing it, but WordPress does not provide plug-in hooks for this time point, you can also provide us with suggestions through the Reporting Bugs, your suggestions are generally accepted by us.

Template tag

Another way to add plug-ins to WordPress is to create a custom Template tag Template Tags. In this way, if someone wants to use your plug-in, they can add these labels to their topic, sidebar, article content, and any suitable place. For example, you can define a plug-in namedGeotag_list_states ()Template tag function, which can add a geographical location tag for the article in the sidebar. when you click this tag, you can also open all the corresponding articles under this geographic location tag.

To define a custom template tag, you only need to write a PHP function, and then tell the plug-in user through the document, plug-in's home page, or declaration in the PHP main file. When writing a document for this function, it would be better if you could provide an example for using this function to tell users how to call this function in the topic.

Save plug-in data to the database

Most WordPress plug-ins need to obtain some information entered by the administrator or user and save it in the session for filtering, action, and template functions). To continue using this information in the next session, you must save it to the WordPress database. The following are four ways to save the plug-in data to the database:

1) use the WordPress "option" mechanism (which will be introduced later ). This method is suitable for storing a small amount of static data with a specific name-this type of data is usually some initialization parameters set by the website owner when creating the plug-in, and will rarely be changed later.

2) use article metadata (also known as user-defined domain ). This method is suitable for storing data related to personal articles, pages, or attachments. For more information, see post_meta function examples and add_post_meta () related functions.

3) use custom classification. This method is suitable for storing data that needs to be stored in different categories, such as user information, comment content, and editable data, it is especially suitable when you want to view related articles and data based on a certain type. For more information, see M Taxonomies.

4) Create a new custom database table. This method is suitable for storing data that is irrelevant to personal articles, pages, or attachments and will grow over time without a specific name. For more information, see Creating Tables with Plugins.

WordPress option mechanism

See Creating Options Pages to learn how to create a page that automatically saves option data.

WordPress has an "option" mechanism for saving, updating, and retrieving data with specific names that are independent. The options can be strings, arrays, or even PHP Objects (of course, PHP objects must be serialized or converted to strings during storage, it must also be deserialized during retrieval ). The options must be strings and must be unique so that they do not conflict with WoredPress or other plug-ins.

Generally, you 'd better streamline the number of plug-in options. For example, if you have 10 options with different names to save to the database, you can consider using these 10 data as an array, and save it to the same option of the database.

The following are the main functions that allow your plug-in to use the option mechanism:

add_option($name, $value, $deprecated, $autoload);
Create a new option and save it to the database. if the option already exists, no operation is performed.
$ Name
Required parameter (string ). The name of the option to be created.
$ Value
Optional parameter (string ). The value of the option to be created. The default value is an empty string.
$ Deprecated
Optional parameter (string ). Whether this option has expired. To enable the $ autoload parameter to take effect, you must input an empty string or null.
$ Autoload
Optional parameter (enum: 'yes' or 'no '). Whether to automatically load this option. If it is set to 'yes', this option is Get_alloptions? The function is automatically retrieved. The default value is yes '.
get_option($option);
Obtain the value of the specified option from the database.
$ Option
Required parameter (string ). Option name. You can find the default Option list installed with WordPress in Option Reference.
update_option($option_name, $newvalue);
Update the option value in the database. if the option does not exist, this option is created.
(Note: If you cannot use $ DeprecatedAnd $ AutoloadThe parameter is not required. Add_optionFunction ).
$ Option_name
Required parameter (string ). The name of the option to be updated/created.
$ Newvalue
Required parameter (string | array | object ). The value of the option to be updated/created.

Management Panel

Assume that your plug-in has some options (options) stored in the WordPress database (see the previous section ), you may want a main control panel to allow your plug-in users to view and edit option values. The method to achieve this goal is described in Adding Administration Menus.

4. internationalization of plug-ins

After you complete the compilation of your plug-ins, another question to consider (assuming you are going to share your plug-ins with you) is to internationalize them. Internationalization is the process of setting your software to be localized. localization is the process of translating the language displayed in the software into other languages. Wordpress is being used by people around the world, so globalization and localization are his internal features, which include the localization of plug-ins.

We hope that you can internationalize your plug-in so that users in other countries can use it locally. We have a comprehensive description about internationalization in? I18n for WordPress Developers, which includes a section describing the internationalization of the plug-in.

3. update your plug-in

This section describes the necessary steps to update you in the http://wordpress.org/extend/plugins? The plug-in on also includes the use of Subversion in wordpress.org? (SVN) details.

Suppose you have submitted your plug-in to the WordPress Plug-In repository. Over time, you may find that you need and want to add new features to your plug-in or correct errors. You often want to complete these changes and submit them to the backbone of your plug-in. The changes will be publicly visible, but only professionals can view your plug-ins through SVN. other users will not download the changes through the website or their WordPress plug-ins.

When you are about to release a new version of the plug-in:

  • Make sure everything is submitted and the new version works properly. Note: test all Wordpress versions supported by your plug-in. It is not just a test of new features, so make sure that you do not accidentally destroy some old functional plug-ins.
  • Update the version number of the header comment in the main php file (in the main folder ).
  • Update the version number of the 'stables tag' in the readme.txt file (in the main folder ).
  • Add a new part in the readme.txt file-"changelog", which briefly describes the changes compared to the last version. It will be listed in the plug-in page.
  • Submit these changes.
  • Create a new SVN tag as the master copy. For more information, see here.

Work for the system for a few minutes, and then check the wordpress.org plug-in page and the page for installing your plug-in on Wordpress, check whether everything is correctly updated and whether plug-in updates are displayed in your installed Wordpress (the update check may be cached, which may take some time, try to access the "available updates" page in your installed WordPress ).

Troubleshooting:

  • The plug-in page on wordpress.org still lists the old versions. Do you want to update the 'stables tag' in the main folder '? Creating a tag and updating readme.txt in the tag folder is not enough!
  • The plug-in page provides a new version of the zip file, but the button still lists the old version number and there is no update notification in your WordPress installation. Do you remember to update the "Version" comment in the main php file?
  • For other questions, see Otto's FAQ page: The Plugins directory and readme.txt files
IV. plug-in development suggestions

The last part is some suggestions for developing plug-ins.

  • Should the WordPress plug-in code be followed? WordPress Coding Standards. For more information, see Inline Documentation.
  • The names of all functions in your plug-in should be different from those of the existing Wordpress Core function and other plug-ins or themes. For this reason, we recommend that you add a prefix of your choice before the names of all functions in your plug-in, or write all the functions of your plug-in into a class (of course, the class name must also be unique ).
  • Do not write the Wordpress database table prefix (usually "wp _") directly in your plug-in. please use$ Wpdb-> prefix?.
  • Although the database reading is relatively cheap, its writing is quite expensive. Databases are very good at getting information and presenting it to users, and these operations (usually) are very fast. However, modifying the database is a very complicated process and requires a longer computing time. Therefore, try to reduce the number of times you write data to the database. Make all preparations when you write the program, so that you can write the program only when necessary.
  • Only SELECT what you need in the database. Despite the ease of reading the database, we recommend that you find the data you really need to minimize the database load. For example, if you only want to obtain the number of rows in the table, do not use?SELECT * FROMIn this way, all data in each row will be read, resulting in a waste of memory. Similarly, if you only want to get post_id and post_author in the plug-in, please only?SELECT? These two items are used to reduce the load on the database. Remember: In a single operation, hundreds of other processes may need to use the database, and both the database and server must meet the needs of all these processes at the same time. Learning how to minimize the use of your plug-ins for databases can avoid abuse of these resources.
  • Do not make your PHP error. AddDefine ('WP _ debug', true );To test all your functions to see if there are any errors or warnings. You can fix the problem until it is no longer displayed.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.