How to develop a plug-in for WordPress

How to develop a plug-in for WordPress
How to develop a plug-in for WordPress

The success of WordPress is growing not only because it is an open source CMS, fast to install and easy to use, but also because of the countless possibilities of modification. Starting with version 1.2, released just four months after the release of the first stable variant of WordPress, you can develop plug-ins yourself, so that programmers can adapt the CMS to their needs and without having to alter the source code.  Since then, many plug-ins have been programmed and released, thanks to which it is possible to create and manage sites in a flexible way, transforming them into something more than a simple blog.


The advantages of creating your own plug-in

If you consider the many extensions, mostly available for free and downloadable in the plug-in directory on the official website, it would not seem necessary to try to create plug-ins. If you look for the most appropriate extensions for your project, you will quickly find that, although many WordPress plug-ins match your search criteria, they cannot:

  • be further developed
  • no longer work as you would like
  • or do not understand the required functions.
For this reason, you should devote yourself to programming WordPress and create a plug-in or modify an existing one according to your needs. In fact, the creation of a plug-in is a valid alternative to the classic expansion of the functions of WordPress, which takes place thanks to custom changes in the file "functions.php" of the theme used. The easiest and fastest way to expand WordPress with other functions (for example to integrate Google Analytics) is to copy strings of code already ready, but in the case of complex projects a similar solution can cause errors that affect the entire site. Moreover, if you change the theme, you also have to apply all the changes again, while this is not done by installing a plug-in. To develop them you don't need to have in-depth programming knowledge, but you just need to have the necessary programming knowledge to achieve your goal.

First step: create a plug-in for WordPress

WordPress is based on the PHP scripting language and for this reason, the plug-ins are nothing more than PHP scripts. A simple extension consists of a single PHP file with a few lines of code. In programming, the only limit you can run into is the license limit: WordPress is released under the GNU General Public License (GPL) and for this, you should choose a combinable license, such as the GPLv2. In addition, to standardize the code of the plug-ins, a guide on Coding Standards has been drawn up, which we recommend you follow for the release of the plug-ins, even if the information contained is not mandatory. If you want to develop your first WordPress plug-in, do the following:

1. First create a PHP file using any word processing program (once you start working with the code, it is recommended to switch to a specific program such as Notepad++). When saving, choose "all files" and add the .php extension at the end, renaming the file as "my-plug-in.php" for example. Now copy the file into the plugin directory of your WordPress installation, by default /wp-content/plugins/. To ensure a better structure, especially for scripts that are composed of multiple files, it is recommended to create a subfolder for the plug-ins: /wp-content/plugins/my-plug-in.

2. In the file you can already insert the first lines with general information about the plug-in:

  1. <?php
  2. /*
  3. Plugin Name: name of the plug-in
  4. URI Plugin: Link to Plugin
  5. Description: description of the plug-in
  6. Version: 1.0
  7. Author: your name
  8. Author URI: link to your site
  9. License: GPLv2
  10. */
  11. ?>
3. In doing so, you have already completed the first step and created your first WordPress plug-in. You will already find it in the list of plug-ins in the WordPress backend, including description and links to your site and the plug-in page. Please note that you can already activate and deactivate the plug-in, even if you have not yet defined the functions of your extension and the action would not lead to any result.

Second step: create your own plug-in

Once the header section of the PHP script has been created, you can start programming the functions of your WordPress plug-in. For example, you can use the code strings already mentioned, which you have so far included in the file "functions.php". In the example below you will find our PHP file, expanded by a shortcode that allows you to integrate a Google map:

  1. <?php
  2. /*
  3. Plugin Name: nome del plug-in
  4. Plugin URI: link al plug-in
  5. Description: descrizione del plug-in
  6. Version: 1.0
  7. Author: il vostro nome
  8. Author URI: link al vostro sito
  9. License: GPLv2
  10. */
  11. /* Shortcode – Integrazione di Google Maps*/
  12. function fn_googleMaps($atts, $content = null) {
  13.    extract(shortcode_atts(array(
  14.       "width" => 640,
  15.       "height" => 480,
  16.       "src" => ''
  17.    ), $atts));
  18.    return '<iframe width="' . $width . '" height="' . $height . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' . $src . '&amp;output=embed"></iframe>';
  19. }
  20. add_shortcode("googlemap", "fn_googleMaps");
  21. ?>
Enter the corresponding code (embed code) for the attribute "src", which you find on Google Maps ("Share or embed map"), and so you have developed your first plug-in. In the navigation menu under "Maps" you will find the plug-in ready to be activated and inserted in your project.

Programming WordPress: learning by mistake

As the example shows, you don't need to be an expert programmer to create your own WordPress extension. It is true, however, that this is a simple implementation, while more complex plug-ins, made up of multiple scripts, do not program in a few minutes. In the latter case, you have to proceed in order and work in a structured manner, so as to avoid possible errors or to be able to resolve them quickly. Be careful from the beginning to write clean code and insert explanatory comments to explain all the functions. The name of the plug-in should also be intuitive and never be the same as an existing one.

For more information on "Creating plug-ins for WordPress" we recommend the CMS developer forum in English. In the WordPress Plugin Developer guide, you will find detailed information for beginners and advanced users, such as programming tips or tips on useful development tools.

Post a Comment

5 Comments

  1. As a WordPress developer at web development company in India, Plugin development is the most challenging task for me...Thanks for your article it will be helpful for me

    ReplyDelete

  2. Hello,

    we provide affordable and result-oriented SEO services, please give a chance to serve you.


    Thanks
    Admin: E07.net

    ReplyDelete
  3. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Ozzun Website design and development company

    ReplyDelete
  4. Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. extension opencart

    ReplyDelete
  5. Good artcile, but it would be better if in future you can share more about this subject. Keep posting. wordpress theme

    ReplyDelete

Please do not enter any spam link in the comment box.