How to create basic wordpress plugin

There are many things that you can do with your wordpress plugin to enhance your website that can also be used by other website owners. In this post we will go through a very basic wordpress plugin and then in other posts i will show some advanced options like admin panel and all.

Create a folder with typically your plugin name. Inside that folder create a php file (i would give it my plugin name again). WordPress actually doesn’t care about the file names. You need to specify in your php file what file should be considered as the main file for wordpress to consider.

So in my case i created a folder test and inside test i created a file test.php. In my test.php i added this code:

<?php
	/*
	Plugin Name: Test Plugin
	Plugin URI: http://www.testplugin.com
	Description: Plugin to test how plugins work
	Author: My Name
	Version: 1.0
	Author URI: http://mysite.com
	*/

	require_once('action.php');
	require_once('admin.php');

	if(is_admin()){
                //this will add a menu item for this plugin in the admin panel
		add_action('admin_menu', 'admin_menu');
	}
?>

Add these information according to you: Plugin Name, Plugin URI, Description, Author, Version, Author URI

I am calling two php files from test.php. admin.php for admin options for website owners and action.php to do what you want your plugin to do.

admin.php

<?php
	/*
	The main admin page for this plugin.
	*/

	function admin_register_head() {
		$siteurl = get_option('siteurl');
		$url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/style.css';

		//specify css file to use for plugin's admin section
		echo "<link rel='stylesheet' type='text/css' href='$url' />\n";
	}

	//function to call citrix_twitter_feed_admin_page()
	function admin_menu(){
		add_options_page('Test Plugin', 'Test Plugin', 'manage_options', 'test-feed', 'admin_page');
		//check here to see how to use add_options_page

		add_action('admin_head', 'admin_register_head');
	}

	function admin_page(){
		echo "put test() in your post to use plugin.";
	}
?>

In admin.php, add_options_page is called to display what to do in the admin panel by calling admin_page()

Also in admin_menu() add_action hook is called to specify css used for the admin section. In my next post i will show some advanced options for admin section.

Because this is very basic plugin, we are not letting users customize this plugin and only displaying that they should put test() in their post to use this plugin.

action.php

<?php
	/*
	This is where all the code is written that plugin actually does
	*/

	function test() {
		echo "This is my first basic plugin";
	}
?>

So, now if anyone activate this plugin and put test() in their post, it will display “This is my first basic plugin”.

Don’t forget to leave comments if you have any questions.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*