Easy Wordpress Plugin
Webprogramo / Blog / Wordpress / WordPress Plugin (Incredible easy and painless)

WordPress Plugin (Incredible easy and painless)

The magic trick of an easy WordPress Plugin!

Alejandro Lopez

11 julio, 2017

Webprogramo / Blog / Wordpress / WordPress Plugin (Incredible easy and painless)
SHARE! and the author will get a Yummy Cookie :
Share on FacebookTweet about this on Twitter

If you are scared about creating a WordPress plugin, I just want to tell you, “Do not be afraid anymore!”. Because it is as easy as write,

echo "This is my first plugin";

Huh?, Really?, Not, I am just kidding, it is easy, but not that simple =). The primary purpose of a WordPress plugin is to “Modify some default functionality without changing core code.”, and to accomplish that goal WordPress has a huge list of “Hooks” (Actions and Filters, ref. https://codex.wordpress.org/Plugin_API/Hooks), so, stop the “blah blah blah” and let’s write some code!.

To create a plugin, you just need to create a folder and a PHP file named “abracadabra”* inside “/wp-content/plugins/” folder of WordPress.

And copy and paste this code inside that “abracadabra.php” file.

<?php
/*
Plugin Name: Abracadabra
Plugin URI: https:/webprogramo.com
Description: I am trying to do some magic and modify WordPress default functionality.
Version: 1.0
Author: Alejandro Lopez
Author URI: http://webprogramo.com
License: GPL2 ** Or you can attach any other license you want to support on your project **
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

With this code, you are telling WordPress, “Hey, you, this is a plugin named ‘Abracadabra,’ and I need you to read it,” and because of that, you will see your new fantastic plugin in the WordPress Plugins List menu (Plugins/Installed Plugins). Now, you can activate it, but, it does nothing, and because it is supposed to that every plugin must “Modify some default functionality.” We are going to add some more code.

E.g., we want to extend the WordPress page functionality to support “excerpt” text, and we need to change a custom post type created by another plugin (let’s say it is calendar post type from Simple Calendar plugin) to be modified only by administrators.
So, we will need an action (init) and a filter (register_post_type_args) to do that, and we are going to create a “Configuration class” with a couple of functions that will do the magic trick!.

Here is the fully commented code for a better understanding.

class Cadabra_Configuration {
public function __construct(){
// Here is the init action that executes the function 'global_on_init_changes' to modify the page behavior.
add_action( 'init', array( $this, 'global_on_init_changes' ) );
// This is the filter where we are going to modify the "calendar" post type attributes.
add_filter( 'register_post_type_args', array( $this, 'modify_wordpress_post_types' ), 20, 2 );
}
/**
* This is a filter function where we modify the calendar post type to be managed only by people with Administrator Role.
*
* @param $args post type arguments
* @param $post_type post type name
* @return Array 
*/
public function modify_wordpress_post_types( $args = array(), $post_type ){
// First we have to check if this is the correct post type that we are going to modify.
if( "calendar" === $post_type ) {
// Then, we just take the arguments and change the "capabilities" of that post type to "manage_options" (Admin only)
$args['capabilities'] = array(
'edit_post'          =&amp;gt; 'manage_options',
'read_post'          =&amp;gt; 'manage_options',
'delete_post'        =&amp;gt; 'manage_options',
'edit_posts'         =&amp;gt; 'manage_options',
'edit_others_posts'  =&amp;gt; 'manage_options',
'delete_posts'       =&amp;gt; 'manage_options',
'publish_posts'      =&amp;gt; 'manage_options',
'read_private_posts' =&amp;gt; 'manage_options'
);
}
// Return the arguments, and Whala!
return $args;
}
function global_on_init_changes() {
// With this line we add "excerpt" support on pages
add_post_type_support( 'page', 'excerpt' );
}
}
// Finally, we just initialize the class, and the magic trick is done!
new Cadabra_Configuration();

Ok, This is it, a WordPress plugin is easy to craft, I encourage you to start working on it, and do not worry, with time and practice you will discover many more of the WordPress Hooks to create the most “Abracadabra” of your life.

Just a plus about a WordPress plugin;

WordPress plugins have a couple of hooks that runs only one time when activating and deactivating it, they are:

register_activation_hook( __FILE__, ‘our_activation_function_to_run’ );
register_deactivation_hook( __FILE__, ‘our_deactivation_function_to_run’ );

We are not going talk about these now because this is just a basic WordPress Plugin creation tutorial (I talk about these hooks here), but, I will just tell you that they are very convenient. e.g., If you need to create some database tables for your plugin to work correctly; you do it on the activation hook. And, If you want to remove the “garbage” information that is left when the user “deactivates” the plugin; of course, you do it on the deactivation hook.

* Abracadabra is just the name of our tutorial plugin, please change it to a meaningful name of yours, e.g., “my-first-plugin.”