This tutorial aims to guide beginners in creating their own WordPress plugins and themes. Though WordPress already offers a wide array of existing plugins and themes, knowing how to create your own will give you the power to customize your website to your liking.
By the end of this tutorial, you will be able to:
- Understand the basic structure of WordPress plugins and themes
- Create a simple WordPress plugin
- Create a basic WordPress theme
Before you start, you should have:
- Basic knowledge of PHP, HTML, and CSS
- A local or live WordPress installation to practice on
To create a WordPress plugin, you need to create a new folder in your /wp-content/plugins/
directory. Let's create a basic plugin that adds a custom message to your website's footer.
Step 1: Create a new folder called my-plugin
in your /wp-content/plugins/
directory.
Step 2: In the my-plugin
folder, create a new PHP file my-plugin.php
.
Step 3: Open my-plugin.php
and add the following boilerplate code:
<?php
/*
Plugin Name: My Plugin
Description: This is a simple WordPress plugin.
Version: 1.0
Author: Your Name
*/
This header comment is necessary for WordPress to recognize your plugin.
Step 4: To add a custom message to the footer, we will hook into the wp_footer
action.
Add this code after the header comment:
function my_plugin_footer_message() {
echo '<p>This is my custom footer message!</p>';
}
add_action('wp_footer', 'my_plugin_footer_message');
In the WordPress admin dashboard, activate 'My Plugin' and visit your website. You should see your custom message at the footer.
A WordPress theme modifies the way the site is displayed without modifying the underlying software. Let's create a basic theme.
Step 1: Create a new folder called my-theme
in your /wp-content/themes/
directory.
Step 2: Create two files in this folder: style.css
and index.php
.
Step 3: Open style.css
and add the following code:
/*
Theme Name: My Theme
Author: Your Name
Description: This is a simple WordPress theme.
Version: 1.0
*/
body {
background-color: lightblue;
}
Step 4: Open index.php
and add the following code:
<?php
get_header();
?>
<h1>Welcome to my site!</h1>
<?php
get_footer();
?>
In the WordPress admin dashboard, activate 'My Theme'. You should see your site with a light blue background and a custom welcome message.
Let's add a shortcode that displays the current date. Add this code to my-plugin.php
:
function my_plugin_date_shortcode() {
return date('Y-m-d');
}
add_shortcode('current_date', 'my_plugin_date_shortcode');
Now you can use [current_date]
in your posts to show the current date.
Let's create a custom header. Add a new file header.php
in your theme folder and add the following code:
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<title><?php bloginfo('name'); ?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
In index.php
, replace get_header();
with this code to include your custom header:
<?php
include('header.php');
?>
In this tutorial, you learned how to create a simple WordPress plugin and a basic WordPress theme. You also learned how to create a shortcode in your plugin and how to customize the header in your theme.
Exercise 1: Create a plugin that adds a copyright notice to the footer.
Exercise 2: Create a theme that changes the font size and color of all headings.
Exercise 3: Modify your plugin to add a shortcode that displays the author's name.
Remember that practice is key to mastering WordPress development. Keep creating plugins and themes to hone your skills.