Creating a WordPress plugin
October 23, 2009
/ Filed under: WordPress
The WordPress codex explains plugins from a reference perspective, but the best way to learn is to see real-world code examples of basic plugins that demonstrate how to set things up for beginners. This tutorial is an overview for a basic plugin that uses PHP to save and retrieve data from the WordPress database to display on the site. Here are the steps:
Create a PHP fileFirst, create a PHP file in a folder with a unique name - this folder will reside in the same folder as other plugins, so it can't be named the same as anything else. This will be the file where all of your scripting for the plugin will reside. Set up two functions - one for public, one for adminIn the PHP file, start by creating two functions:
function mywidget_public($args = false) {
}
function mywidget_admin() {
}
Our widget, for example purposes, is called "My Widget," which we'll reference as
Feel free to put any PHP code in both of those functions - whatever you wish to run. A few notes on each function, and specific code constructs: In the admin function, you're basically setting up an HTML form for users to control settings related to your widget:
In the example screenshot above, we're asking users for information pertaining to making an API call. So, for starters, the admin function could look like this:
function mywidget_admin() {
$options_site = get_option("widget_mywidget_site");
?>
<p>
Your Software URL:
<input type="text" name="p_link" id="p_link" value="<?php echo $options_site["p_link"]; ?>" style="width:99%;" />
</p>
<p>
Your Software Username:
<input type="text" name="username" id="username" value="<?php echo $options_site["username"]; ?>" style="width:99%;" />
</p>
<p>
Your Software Password:
<input type="password" name="password" id="password" value="<?php echo $options_site["password"]; ?>" style="width:99%;" />
</p>
<?php
}
Notice, specifically, the If you're wondering how the values get to the database in the first place, they do so by calling another WordPress function: Let's take a look at the syntax for saving values to the database:
The function Your values are saved to the
You can create as many distinct "options" as you'd like, and all can be saved to the database. Continuing with the admin function, when someone fills in the details, and hits the "Save" button (this button is generated by WordPress automatically - you don't need to create it in your HTML form), you should call the In your public function, you can retrieve the database values to display on the site:
function mywidget_public($args = false) {
$options_site = get_option("widget_mywidget_site");
echo $options_site["html"];
}
It's as simple as that. This will display the "html" contents into the sidebar on the public side of your WordPress site. Call necessary code to register the plugin with WordPressThe example I provided was bare-bones and simple. There's a few more things we should do before the widget is recognized, and works with the WordPress system. At the bottom of your PHP file, include this code:
function widget_mywidget_init() {
register_sidebar_widget("My Widget", "widget_mywidget_public");
register_widget_control("My Widget", 'widget_mywidget_admin');
}
add_action("plugins_loaded", "widget_mywidget_init");
This will load the plugin on the admin page, so users can actually activate it. Wrap-upThat's the basic steps! There's plenty more involved for those that care, but this tutorial was just a quick demonstration of creating a WordPress plugin with code samples. Read the official WordPress plugin document to get more details.
Comments/Mentions
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|


how do i save data to a table that is not wp_options?