How to create a simple WordPress plugin

A plugin is a piece of code, that you can fire at your backend to extend the functionality of the default WordPress installation. Plugins made WordPress most popular CMS in the world.

In today’s tutorial, we are going to create a simple WordPress plugin, that allows us to display the username and the avatar of logged in users wherever we want. Let’s get started.

Plugin requirements

Plugin folder

WordPress plugins can be as simple or as complicated as you need them to be, but all of them start with plugin folder. Keep in mind that name of your plugin’s folder would be your plugin’s slug. I am going to name my folder display-user-shortcode and place it right inside WordPress plugin directorywp-content/plugins/.

Plugin file

Inside plugin’s folder, we need to create a display-user-shortcode.php file that would handle all of our custom functionality. Its name should be the same as your plugin’s slug.

Plugin header

There are a bunch of .php  files inside WordPress, but not every of them is a plugin.  There is a special PHP comment, that tells WordPress: “Hi, I’m a plugin. My name is…“, called by WordPress nerds plugin header comment. At the very least, the plugin header comment must contain the name of our plugin. It also can tell about its location, parents ( plugin’s authors ), version, short description and license information.

Now your plugin becomes visible from the WordPress dashboard. Navigate to Plugins/Installed Plugins and you would find our plugin:

Plugin WordPress

Bingo! We`ve created an empty WordPress plugin. Let’s add some functionality.

Functions in plugin

Every successful plugin has 3 types of functions:

  1. Custom functions. There we write all the weird stuff we think would add some value to WordPress core functionality.
  2. Hooks – special functions, that defines a place for our custom code in WordPress. When we use a particular hook, we literally tell WordPress: “This is where, when and how you should fire my code, Mr. WordPress“. To choose the right hook for your plugin, we should first answer these 3 questions: “When? Where? How?”.
  3. Security functions. “You shall not pass!”

Small piece of security code

Most hosts allow direct access to files on your server, including those belonging to your plugin. Direct file access is when someone directly queries your file. This can be done by simply entering the complete path to the file in the URL bar of the browser.

To prevent direct access to your files, we can check whether the constant ABSPATH is set or not. ABSPATH is a constant, defined into the wp-config.php file of the WordPress installation. It doesn’t exist if the wp-config.php file is not loaded.

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

Place this code right below the header comment.

Display user data

At this point we are ready to create a function, that would display user name and avatar. Let’s give it a unique and self-explanatory name. I added alex_vitorials to avoid possible conflicts with dozens of other installed plugins.

// display name and avatar
function alex_vitorials_get_user_unique_name(){ }
view raw Plugin function hosted with ❤ by GitHub

Next step, we need to check whether our user is logged in or not with a simple if-else statement

// display name and avatar
function alex_vitorials_get_user_unique_name(){
// check whether our user is logged in or not
if ( ! is_user_logged_in() ) { return; } // if not, return nothing
else { //our code would go here } // return something
}
view raw If else statement hosted with ❤ by GitHub

Then, we should show a user’s nickname. Let’s do it with wp_get_current_user(). This function retrieves the information pertaining to the currently logged in user.

function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) { // check whether our user is logged in or not return; }
// if not, return nothing
else { $user = wp_get_current_user();
$user_nicename = get_userdata( $user->ID )->user_nicename;
return $user_nicename; } // return users nickname
view raw Return user's name hosted with ❤ by GitHub

Create a shortcode

Shortcodes are little bits of code that allows users to execute a code right inside posts and pages via macros. In order to create a shortcode in WordPress we should use add_shortcode function

add_shortcode( 'user-ID', 'alex_vitorials_get_user_unique_name' );
view raw Create shortcode hosted with ❤ by GitHub

Paste the code from above outside your function. Now activate the plugin and try to insert the shortcode [user-ID] in one of your posts or pages. This would echo your nickname. What about an avatar? Let’s make this feature available!

Display user’s avatar

get_avatar() is what we need to display gravatar. It accepts an ID of the user and size of the photo.

$avatar = get_avatar( $user->ID, 40 );
view raw get avatar hosted with ❤ by GitHub

This code would store a my picture with 40px width and height in to the $avatar variable. Let’s wrap all around.

function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) : ?>
<?php return; ?>
<?php else: ?>
<?php $user = wp_get_current_user();
$avatar = get_avatar( $user->ID, 40 );
$user_nicename = '<span>';
$user_nicename .= get_userdata( $user->ID )->user_nicename;
$user_nicename .= '</span>';
?>
<?php return $avatar . $user_nicename; ?>
<?php endif;
}
add_shortcode( 'user-ID', 'alex_vitorials_get_user_unique_name' );

Notice that I wrap a span around the nickname to enable styling .

Few improvements

In WordPress, we can use functions in several places in the same installation. So it’s wise to check whether our function is already exist or not

// check for function with the same name
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! function_exists( 'alex_vitorials_get_user_unique_name' ) ) {
function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) {
return;
}
else {
$user = wp_get_current_user();
$avatar = get_avatar( $user->ID, 40 );
$user_nicename = '<span>';
$user_nicename .= get_userdata( $user->ID )->user_nicename;
$user_nicename .= '</span>';
return $avatar . $user_nicename;
}
}
add_shortcode( 'user-ID', 'alex_vitorials_get_user_unique_name' );
}

We’ ve almost done. For now, our plugin provides a feedback to the small part of our audience – logged in users. What about others? They would see nothing! Most of our visitors would achieve zero value from plugin’s functionality. Let’s give them a login link using wp_login_url() function

<?php
/*
Plugin Name: Display user shortcode
Plugin URI: https://vitorials.net/
Description: Display name and avatar of the logged in users
Version: 1.0 Author: Alex Kovalenko
Author URI: https://vitorials.net/
Text Domain: Display user shortcode
Domain Path: /languages
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! function_exists( 'alex_vitorials_get_user_unique_name' ) ) {
function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) {
$login_link = '<a href="';
$login_link .= wp_login_url();
$login_link .='"> Login </a>';
return $login_link;
}
else {
$user = wp_get_current_user();
$avatar = get_avatar( $user->ID, 40 );
$user_nicename = '<span>';
$user_nicename .= get_userdata( $user->ID )->user_nicename;
$user_nicename .= '</span>';
return $avatar . $user_nicename;
}
}
add_shortcode( 'user-ID', 'alex_vitorials_get_user_unique_name' );
}

We finally reached the end! Yes, we have our simple plugin! If you feel like it’s too simple for you, try to code an object-oriented plugin. Do not forget to share your thoughts, doubts and suggestions in to the comments form below. Cheers:)

Comments

comments

join our newsletter