Introduction To Writing WordPress Plugins

The WordPress Codex is the place to go when you want to learn more about the WordPress software.

To learn about WordPress Plugins start at:

Writing a Plugin

[This is the link to the page. The page is an excellent summary of the process of writing a plugin. You need to review this whole page – below is just the introduction.]

[After you’ve reviewed the above page, go to The Plugin Handbook. It has details and links to everything you need to know about plugins.]

Introduction

WordPress Plugins allow you to easily modify, customize, and enhance a WordPress site. Instead of changing the core program code of WordPress, you can add functionality with WordPress Plugins. Here is a basic definition.

A WordPress Plugin is a program or a set of one or more functions written in the PHP scripting language, that adds a specific set of features or services to the WordPress site. You can seamlessly integrate a plugin with the site using access points and methods provided by the WordPress Plugin Application Program Interface (API).

Wish that WordPress had some new or modified functionality? The first thing to do is to search various WordPress Plugin repositories and sources to see if someone has already created a WordPress Plugin that suits your needs. If not, this article will guide you through the process of creating your own WordPress Plugin.

This article assumes you are already familiar with the basic functionality of WordPress and with PHP programming*.

*For a helpful introduction to PHP, see the excellent set of YouTube tutorials beginning with 1: Introduction to PHP programming | PHP tutorial | Learn PHP programming 5:29 YouTube video. By mmtuts Published on Oct 19, 2015. 

*And be sure to visit Getting Started from The PHP Manual.

Resources

  • To understand how WordPress Plugins work and how to install them on your WordPress blog, see Plugins.
  • There is a comprehensive list of articles and resources for Plugin developers in Plugin Resources, including external articles on writing WordPress Plugins and articles on special topics.
  • To learn the basics about how WordPress Plugins are written, view the source code for a well-written Plugin like Hello Dolly, which is distributed with WordPress.
  • Once you have written your WordPress Plugin, read Plugin Submission and Promotion to learn how to distribute it and share it with others.

 

The following is from WordPress All-In-one For Dummies, 2017, 3rd ed; Book 7 Using and Developing Plugins, Chapter 5: Creating Simple Plugins From Scratch, pp.637 – 690:

p. 637

You can extend WordPress functionality through plugins and themes without modifying any WordPress core files.

By using the WordPress software’s built-in action hooks (placeholder functions that allow plugin  developers to execute hooked into them) and filter hooks (other placeholder functions that you can use to apply to parameters to filter results), you can create just about any functionality you can imagine.

The following is from What is: Hooks from wpbeginner:

Functions used to modify Actions/Filters in WordPress can be hooked into WordPress. However, it is important to note that actions and filters are not the same thing. Actions are functions performed when a certain event occurs in WordPress. Filters allow you to modify certain functions. Arguments used to hook both filters and actions look the same. But they are different in functionality and how they behave.

Example of a hook used with a filter in WordPress:

1
2
3
4
5
6
7
functionwpb_custom_excerpt( $output) {
  if( has_excerpt() && ! is_attachment() ) {
    $output.= wpb_continue_reading_link();
  }
  return$output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt');

The sample code above creates a function wpb_custom_excerpt which is hooked into get_the_excerpt filter.

Example of a hook applied to an action:

1
2
3
4
functionmytheme_enqueue_script() {
    wp_enqueue_script( 'my-custom-js', 'custom.js', false );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_script');

The sample code above creates a function mytheme_enqueue_script which is hooked into wp_enqueue_scripts action.

Filtering Content from WP All-In-One:

The filter you use in this section is the_content, which replaces all the content in your site’s posts and pages with a simple  message. [See p. 649 for explanation and file]

For more information on filters see https://codex.wordpress.org/Plugin_API/Filter_Reference:.

Plugin API/Filter Reference

This article contains an extensive (but not 100% comprehensive) list of the filter hooks available for use in plugin development in Version 2.1 and above of WordPress. For more information:

This entry was posted in Coding, Open Source Software, Wordpress. Bookmark the permalink.