how-wordpress-plugins-hooks-work

How WordPress plugins work

WordPress APIs

A plugin in WordPress is a PHP script that extends or alters the core functionality of WordPress. WordPress use many APIs for interacting with a plugin. Following is a list of the main available APIs:

  • Plugin — Provides a set of hooks that enable plugins access to specific parts of WordPress.There are 2 types of hooks: actions and filters. The action hook enables you to trigger custom plugin code at specific points during execution (for example: you can trigger a custom function to run after a user registers a user account in WordPress). The filter hook is used to modifies text before adding or after retrieving from the database.
  • Widgets — Create and manage widgets in your plugin. Widgets appear under the Appearance > Widgets screen and are available to add to any registered sidebar in your theme. The API enables multiple instances of the same widget to be used throughout your sidebars.
  • Shortcode — Adds shortcode support to your plugin. A shortcode is a simple hook that enables you to call a PHP function by adding something such as [shortcode] to a post or page
  • HTTP — Sends HTTP requests from your plugin (retrieves content from an external URL or for submitting content to a URL). Currently you have 5 different ways to send an HTTP request. This API standardizes that process and tests each method prior to executing. Based on your server configuration, the API will use the appropriate method and make the request.
  • Settings — Inserts settings or a settings section for your plugin. The primary advantage to using the Settings API is security. All settings data is scrubbed, so you do not need to worry about cross site request forgery (CSRF) and cross site scripting (XSS) attacks when saving plugin settings.
  • Options — Stores and retrieves options in your plugin.(capability to create new options, update existing options, delete options, and retrieve any option already defined.
  • Dashboard Widgets — Creates admin dashboard widgets.
  • Rewrite — Creates custom rewrite rules: add static end – points ( /custom – page/ ), structure tags ( %postname% ), additional feed links ( /feed/json/ ).
  • Transients — Creates temporary options (cached data) in your plugins. This API is similar to the Options API, but all options are saved with an expiration time.
  • Database — This includes creating, updating, deleting, and retrieving database records for use in your plugins.

WordPress features pluggable functions which enable you to override specific core functions in a plugin and are defined in the /wp-includes/pluggable.php core file. Plugins are loaded early in the process when a web page is called.
Bellow you can see a diagram with the standard loading process of a page:

wordpress-diagram-page-load

Advantages of plugins

  • Change the behavior of wp without modifying any core files
  • Easy updates
  • Easier to share and reuse
  • Plugin sandbox – the site won’t break when activate a broken plugin (fail-safe feature)
  • Huge community centered around plugin development

Types of plugins

  • Active — Plugin is active and running in WordPress
  • Inactive — Plugin is installed but not active. No code from the plugin is executed
  • Must-Use — All plugins installed in the wp-content/mu-plugins directory. These plugins are loaded automatically and it can be deactivate
    only by removing it completely from the directory.
  • Drop-ins — Can replace the functionality of WP. If WP detects one of these files (in wp-content directory), it will be auto-loaded and listed under the Drop-in filter on the Plugin screen.
    There are 10 Drop-in plugins:
    • advanced-cache.php — Advanced caching plugin
    • db.php — Custom database class
    • db-error.php — Custom database error message
    • install.php — Custom installation script
    • maintenance.php — Custom maintenance message
    • object-cache.php — External object cache
    • sunrise.php — Advanced domain mapping
    • blog-deleted.php — Custom blog deleted message
    • blog-inactive.php — Custom blog inactive message
    • blog-suspended.php — Custom blog suspended message

Testing plugins

<?php
// Load the WordPress Environment
// define( 'WP_DEBUG', true ); /* uncomment for debug mode */
require('./wp-load.php');
// require_once ('./wp-admin/admin.php'); /* uncomment for is_admin() */
?>
<pre>
<?php
/* test stuff here */
var_dump( is_admin() );
?>
</pre >

This is a quick way to load all of the required WP functions to test plugin functionality without actually creating a plugin.

email-newsletter

Dev'Letter

Professional opinion about Web Development Techniques, Tools & Productivity.
Only once a month!

Any suggestion on what to write next time ? Submit now

1
Opinions

avatar
550
1 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Leta Bcn Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Leta Bcn
Member
Leta Bcn

You should mention the execution params in hooks/filters

Related Posts