WordPress; Hello World

WordPress: Hello World

By Jonathan on

wordpress logo

I needed to make a WordPress plugin for a website design that I was working on, having never played with the plugins or widgets within WordPress I decided to look at making a simple ‘Hello World’ plugin. This plugin would allow the user to enter two different variables into the the admin control panel which would then save the variables into a database.

To get WordPress to recognise our PHP file as a plugin we need to insert the following code.

{code type=php}
/*
Plugin Name: Hello World Example
Plugin URI: https://www.falkondigital.com/
Description: A simple plugin to upload sponsors information
Version: 1.0
Author: Jonathan Garrett
Author URI: https://www.falkondigital.com/
*/
{/code}

This needs to be in comment tags at the very start of the file. Try to keep the basic’s which are above. This file should be saved into the plugins folder inside a new folder.

Next the database would need to be created, this would be done using the add_option() function which carries four parameters; name, value, deprecated and autoload.

{code type=php}
function set_db_install() {
add_option(‘hello’, ‘Default’, ”);
add_option(‘world’, ‘Default’, ”);
}
{/code}

The above will add two new fields into the database one called ‘hello’ and the other ‘world’ both of these have their values as default and autoload is also true.

Next up is the delete_option() function which only has one parameter; name. The below code will delete both ‘hello’ and ‘world’ fields from the database that was created.

{code type=php}
function set_db_uninstall() {
delete_option(‘hello’);
delete_option(‘world’);
}
{/code}

Now that code has been written, it needs to be called to action as these are functions. The fields want to be created and deleted when the plugin is activated and deactivated. For this the register_activation_hook() and the register_deactivation_hook() needs to be used. These functions are built into WordPress and will automatically be called when the plugin is activated.

{code type=php}
/* Runs when plugin is activated */
register_activation_hook(__FILE__,’set_db_install’);

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__,’set_db_uninstall’);
{/code}

After this we can start on creating the functions to check if the form has been submitted, update the database and displaying the form itself. To check if the form has been submitted the following code is needed.

{code type=php}
function hello_world_options(){
?>

if statement is used to check if the form has been submitted by checking if the page has had REQUEST. If the form has been submitted then the function update_hello_world_page(); will be called, if not print_hello_world_table(); will be.

Next up the code to update the database fields, this again uses the REQUEST script. Once this check has been carried out the update_option() function is used. This is another in built function that carries two parameters, the first of which is the field name that is being targeted, the second is the content to go into that field. All previous content within the variable of this field will be overwritten.

{code type=php}
function update_hello_world_page(){
if($_REQUEST['hello']){
update_option(‘hello’,$_REQUEST['hello']);
}

if($_REQUEST['world']){
update_option(‘world’,$_REQUEST['world']);
}
};
{/code}

The following code will add the sub menu page information into the WordPress plugin, this function is again built into WordPress and carries five parameters which should be used. These are; page_title, mene_title, capability, menu_slug and function. After this we add the

{code type=php}
function modify_menu() {
add_options_page(‘Hello World’, ‘Hello World’, ‘administrator’,
‘hello-world’, ‘hello_world_options’);

};

add_action(‘admin_menu’,’modify_menu’);
{/code}

Now for the easy bits, creating what the user will see in the back-end of WordPress.
{code type=php}
function print_hello_world_table() {
?>

Hello World Options

About Jonathan

Jonathan likes playing with code, cars and ermm... digital things

Related Posts

JOIN THE CONVERSATION - LEAVE A COMMENT