What Is A WordPress Plugin ?
A WordPress plugin is a standalone set of code that enhances and extends the functionality of WordPress. By using any combination of PHP, HTML, CSS, JavaScript/jQuery, or any other web programming language, a plugin can add new features to any part of your website, including the Admin Control Panel. You can modify the default behavior of WordPress, or remove unwanted behavior completely. Plugins allow you to easily customize and personalize WordPress to fit your needs.
Plugins are never automatically overwritten or deleted when installing WordPress updates. If your plugin has coding errors, you can usually just deactivate it in the Admin Control Panel while you fix it. If your plugin has a serious error, WordPress will sometimes automatically deactivate it for you, allowing your site to continue functioning.
How do WordPress plugins work?
An important concept to understand in WordPress plugins are WordPress hooks, e.g. actions and filters. Hooks allows our plugins to run specific functionality at specific times within WordPress functions.
What are Hooks?
WordPress Plugins interact with core code using hooks. There are two different types of hooks.
- Action hooks (To add/remove functions)
- Filter hooks (To modify data that is produced by functions)
Actions and Action Hooks
When you visit any page of a WordPress website, a series of PHP functions (named actions) are called at various points, and they are attached to action hooks. Using the action hooks provided by WordPress, you can add your own functions to the list of actions that run when any action hook is called, and you can also remove pre-existing functions from any action hook. Action hooks dictate when the actions are called. Before the closing </head> tag of any page, the wp_head() action hook is called, and the actions that are hooked to wp_head() are run.
Action hooks are contextual – some are called on every page of your website, others are only called when viewing the Admin Control Panel, and so on. A full list of actions hooks and the context in which they are called can be found on the WordPress Plugin API/Action Reference page.
Actions
Actions are hooks triggered when WordPress performs events like publishing a post, initializing header and footer areas, creating a new user or deleting pages, etc. For example, the following is an action which runs specific functionality when admin menus are loaded in the administration panel:
add_action( 'admin_menu', ‘add_admin_menu' ); function add_admin_menu() { // add a group of custom menus }
admin_menu
is an action hook provided by WordPress which allows us to run a function, add_admin_menu
, for our needs right when WordPress is going to load administration menus.
Filters
In general, a filter function takes the input as unmodified data and returns the modified data. Filters sit between the database and browsers and perform certain operations on data, like when WordPress is generating pages or adding new posts in the database. For example, take a look at the following filter:
add_filter( 'comment_text', 'filter_words' ); function filter_words( $data ) { // filter comment words and return the modified comment }
In the above filter, comment_text
is the filter name and filter_words
is a callback function which will take the comments as input and filter the words, returning the modified comments to the output.
That’s it for today
In Next Part we will build our first WP Plugin