A plugin can add new features to any part of your website, including the WP-Admin-Panel.
You can change the default behavior of WordPress, or remove the functionality/functions completely.
Plugins allow you to customize the WordPress site without touch your actual WordPress code.
WordPress plugins are standalone, they do not physically alter any of the WordPress core code.
A plugin can be made up of a single PHP file or multiple files like images, JavaScript, language, CSS files.
It’s recommended that WordPress developers name their main plugin file after their plugin by convention with a unique name.
In my example, I named the plugin WP_mythcat plugin would be wp-mythcat.php .
After you develop all the code for a plugin you then compress your main folder into a zip file to be uploaded and installed on a WordPress site.
Then you can install from your WordPress website.
The main PHP plugin file comes with a header with the standard plugin information header at the top of your main PHP file.
Example:
1 2 3 4 5 6 7 8 9 10 | /* Plugin Name: Name of your plugin Plugin URI: link to your plugin homepage Description: Describe what your plugin is all about in a few short sentences Version: 1.0 Author: Your name Author URI: link to your website License: your license fro the plugin License URI: link to your plugin license */ |
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
Let’s see some examples with hooks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Hook to the 'init' action, which is called after WordPress is finished loading the core code. add_action( 'init', 'add_Cookie' ); // Set a cookie with the current time of day setcookie("last_visit_time", date("r"), time()+60*60*24*30, "/"); } ... // Hook the 'init' action, which is called after WordPress is finished loading the core code // and add the function 'remove_tags' add_action( 'init', 'remove_tags' ); // Remove the 'add_My_Meta_Tags' function from the wp_head action hook function remove_tags() { remove_action( 'wp_head', 'add_tags'); } |
The next source code will add the mythcat settings plugin into admin main menu with all HTML5 inputs.
You need to change this new_option_name for each input tag used.
Let’s see the source code, is very simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php /* Plugin Name: mythcat Description: This is my first plugin! It makes a new admin menu link! Author: Cătălin George Feștilă */ // create custom plugin settings menu add_action('admin_menu', 'mythcat_plugin_create_menu'); function mythcat_plugin_create_menu() { //create new top-level menu add_menu_page('mythcat - plugin settings', 'mythcat settings', 'administrator', __FILE__, 'mythcat_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) ); //call register settings function add_action( 'admin_init', 'register_mythcat_plugin_settings' ); } function register_mythcat_plugin_settings() { //register our settings register_setting( 'mythcat-plugin-settings-group', 'new_option_name' ); register_setting( 'mythcat-plugin-settings-group', 'some_other_option' ); register_setting( 'mythcat-plugin-settings-group', 'option_etc' ); } function mythcat_plugin_settings_page() { ?> <div class="wrap"> <h1>Your Plugin Name</h1> <form method="post" action="options.php"> <?php settings_fields( 'mythcat-plugin-settings-group' ); ?> <?php do_settings_sections( 'mythcat-plugin-settings-group' ); ?> <table class="form-table"> <tr valign="top"><th scope="row"> </th><td><input type="button" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="checkbox" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="color" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="date" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="datetime-local" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="email" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="file" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="hidden" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="image" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="month" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="number" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="password" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="radio" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="range" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="reset" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="search" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="submit" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="tel" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="time" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="url" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> <tr valign="top"><th scope="row"> </th><td><input type="week" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td></tr> </table> <?php submit_button(); ?> </form> </div> <?php } ?> |
This is the result of the source code, see the screenshot: