WordPress Plugins
- Plugins are packages of codes which would help us extend the functionality of our wordpress site.
- They do not mess with the core files, instead they get included in the wordpress so that their functionality can be used.
- In plugin our main file requires certain headers :
/**
* Plugin Name: My Basics Plugin
* Plugin URI: https://example.com/plugins/the-basics/
* Description: Handle the basics with this plugin.
* Version: 1.10.3
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: John Smith
* Author URI: https://author.example.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://example.com/my-plugin/
* Text Domain: my-basics-plugin
* Domain Path: /languages
*/Not all of these are required, only the Plugin Name is required to get our plugin functional.
__ PLUGIN LOADING ORDER IN WORDPRESS __
- In
wp-settings.php, WordPress first checks for any must-use plugins (plugins in the optionalmu-pluginsfolder) and loads those. - Then, if you’re running a multisite installation, it checks for plugins that are network-activated and loads those.
- Then it checks for all other active plugins by looking at the
active_pluginsentry of thewp_optionsdatabase table, and loops through those. The plugins will be listed alphabetically.
- Our active plugin details are stored in the wp_options table under the option
active_plugins
As [ Array ( [0] => query-monitor/query-monitor.php [1] => movie-library/movie-library.php ) ]
In the format ofplugin_folder_name/plugin_file_name.php
Actions and Filter :
Actions: Actions allows us to add or change data in the database or modify the functionality of WordPress
Filters: Filters gives us the ability to change the data before it is outputted or inserted into the database.
Hooks? -> Actions and Filters both run on the concept of hooks, hooks are a kind of identifiers for actions and filters.
What would happen if we register both action and filter for the same hook?
A: On doing so, calling either do_action or apply_filters the hook is executing for a both the attached callbacks, whether it be an action or a hook.
The apply_filters() function returns a value, while the do_action() function returns nothingSome global variables that can be helpful :
- $wp_actions -> It stores the number of times a particular action has been run.
- $wp_filter -> It stores all the action and filters with their callbacks
Q: How to check the number of times an action was called?
A: We can use the function did_action() to check how many times the action was trigerred.
Reference : WORDPRESS HOOKS LIST
- has_action( $hook, $callback ) -> This function is used to check whether a callback is hooked to an action.
It you send the second parameter to this function as the callback it will check that if that particular callback is registered to the hook. - has_filter( $hook, $callback ) -> This function is used to check whether a callback is hooked to an filter.
It you send the second parameter to this function as the callback it will check that if that particular callback is registered to the hook. [ Same as has_filter() ] - add_action( $hook, $callback, $priority, $accepted_args) -> Always returns true [ hardcoded ]
- add_filter( $hook, $callback, $priority, $accepted_args ) -> Always returns true [ hardcoded ]
Q: How to remove an action?
We can use the function remove_action() to remove an action, we need to pass both the hook name and the callback in this function to delete.
Q: How to remove all actions?
We can remove all actions for a particular hook by calling the remove_all_actions( $hook, $priority ) function, if the priority is not given, this would delete all the actions or filters attached to that hook,
If the priority is give, it will only delete the actions or filters under the specified priority only.
Q: How to know the current filter or action being executed inside a callback function?
A: We can use the doing_action( $action_name ) and we can use the current_filter() function.
The functions internally check $wp_current_filter variable which is set by wordpress during the execution of a hook.
This case can be helpful when a single callback function is attached to many actions or filters, we can work with conditional statements inside it to solve this issue.
The all hook
There’s is one special hook in WordPress which is fired when any action or filter is run, this is known as the all hook, any callback you add to the all hook will be executed when any hook is triggered, you can internally
The all hook can be used for debugging and we can know about the current hook in the all hook by calling the current_filter() function.
Q: What will happen if we apply a filter on a string when the filter does not even exsits?
A: If we apply an un-existing filter to a string, it would simply return the string itself as inside the apply_filters function there is a conditional statement which checks if the hook is registered and the filter is only applied if the hook is registered, else if will just simply return the orignal value.
WP Options
Additional option data in WordPress is stored under the wp_options table, we can use the functions provided by the WordPress to alter/access this data.
Some of those functions are listed below.
- add_option( $option_name, $option_value, $deprecated, $autoload )
By default the option added to the database, the autoload value for it is yes and it is autoloaded.
We can also create an option without any value and it would be created empty in the database.
Returns true or false for whether the option is created or not. - delete_option( $option_name )
This would delete the option with the name.
Returns tru or false for whether the option was deleted or not. - update_option( $option_name, $value, $autoload )
This function is used to update the value of the existing option in the table,
The value is only update if the current value !== previous_value =>
if current === previous the function simply returns and stops executing , therefore we cannot even change the autoload value only, to change just the autoload value we will also have to change the content if we are working through the upadte_option function
This also creates the option if it does not exists already in the database.
We can also set empty value.
Just the condition should be our newly set data should not be equal to the previous data already in the option.
This also returns true if the data was updated, else false. - get_option( $option_name, $default ) :
This function is used to retrieve the value of a particular option from the options table,
The parameter default is there to specify the default value if the option is not found in the database, in that case the $default would be returned,
This would return the value of the option
If the option doesn’t exists this would return the $default
If $default is not specified it would return false.
Custom Post Types
In WordPress, everything works around the posts, the default post types WordPress offers ( page and post most common ) may not be enough to deal with the data sometimes, in those cases the Custom Post types comes handy, we can create custom post types and assign different configuration to the same.
Default WordPress post types:
- Post
- Page
- Attachments
- Revisions
- Navigation Menus
- Custom CSS
- Changesets
How to create a custom post type?
We can use the basic WP Cli command to generate a template for out custom post type
wp scaffold post-type --prompt
This would prompt us the details for the post-type, and then it would create a post type file which registers our custom post type.
We use the function register_post_type to register our post type and hook this function to the init hook of the WordPress.
The function:
register_post_type($post_type, $args = array())
The first parameter is sent as the slug for our post type, and the second is the configuration of it.
Some of them are: [ labels ] -> it holds different kind of labels for our custom post type
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'comments' ),
'has_archive' => true,
'rewrite' => true,
'query_var' => true,
'menu_position' => null,
'menu_icon' => 'dashicons-video-alt2',