Creating a WordPress pluginPosted by Matt Thommes on October 23, 2009 | Post type: Gain 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. 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 (you can put anything here - this is just an example). 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 So, to modify the admin function a little bit:
function mywidget_admin() {
$options_site = get_option("widget_mywidget_site");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$options_site = array(
"p_link" => $_POST["p_link"],
"username" => $_POST["username"],
"password" => $_POST["password"],
);
update_option("widget_mywidget_site", $options_site);
}
else {
?>
<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
}
}
Here we check to see if the "Save" button was hit ( 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. Provided there is an "html" array item in 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 documentation to get more details. About the author(s)Matt Thommes is an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us. Comments
|
Quick Link to this postTTIP.me/2230 |