Table of Contents
Introduction of Wordpress Plugin Developmen
Wordpress Plugin Development can be complicated task for any wordpress developer. There lots of Developer searching a good tutorial from where they can learn creating wordpress plugin. Following i will demonstrate to create a plugin that would be ads management plugin .In that we will be using lots of section of advance wordpress plugin development like Creating custom database tables. insert, update , delete query, admin menu, widget development etc. Let's start. For a plugin ,wordpress required minimum one file inside your wp-content/plugins. But for our requirement we will have 3 files .We will be going step by step creating this plugin . I have created a folder "ads" inside wp-content/ and a file ads.php inside that looks like belowStep 1 : Write plugin information
Now open ads.php in your proffered php editor. First thing to do in our plugin file is to write plugin information as below Above statement are self explanatory, they register some information related with the plugin that shows in admin panel under plugin page.Step 2 : create a database table
For our plugin we have to create a database table . here table name will be ‘ads’ . So to create a table when this plugin install and delete the table when this plugin uninstall , I will be using 2 wordpress hook i) register_activation_hook() ii) –this execute when the plugin installed/enabled iii) register-deactivation_hook() - this execute when the plugin uninstalled/disabled For table creation sql code, I recommend to manually create table inside phpmyadmin will the following attributes and export it to notepad. Then copy sql code to use in our plugin. Table structure should looks like belowStep 3 : register_activation_hook and register_deactivation_hook
Ads.php now looks like below
First hook register_activation_hook call function ‘w3_ads_create_table’ This function create database table ,
If you are quite familier with PHP , you can easily understand about the array $w3_ads_table
and $w3_ads_table I am using this keeping in mind if we have to create other table just
copy paste it and provide $num=1 This will create another table. you can check this by activating the plugin from admin panel.
Step 4 : create admin panel menu
Now time to create some admin panel menu pages for our plugin settings page. Here I will create 2 menu page i) ads listing page ii) Ads add page I will use wordpress a hook i.e admin_menu and 2 function add_menu_page() and add_submenu_page() function to create menu and submenu for our plugin .Check below
From add_action('admin_menu','register_ads_page'); this will call function register_ads_page when admin_menu will load from admin panel.
Following parameter are in sequence of page title, menu title, capability, slug, function name.
for details about this function check wordpress codex add_menu_page.
add_menu_page('Advertiese','Ads','manage_options','ads','ads',plugins_url('images/icon.webp',__FILE__ ));
This function will register a menu in left sidebar of admin dashboard .
It also call a function ads() to display page of this menu item.
So we have to create for this to list our all ads. we know currently we do not have any ads,
So better we consider working on add_new_ads() called from add_submenu_page() function .
Here above I have create a form to insert ads in database table ‘w3_ads’.
To interact with wordpress database I have used global $wpdb;.
Insert query looks like below $wpdb->insert( $wpdb->prefix.'w3_ads',
array( 'title'=$title, 'image'=$upload_image, 'body'=$body ), array( '%s', '%s', '%s' ) );
Let's have a look, here $wpdb->prefix provide our current wordpress table prefix to be added with ‘w3_ads’ table name
There mainly 3 element in $wpdb->insert(‘table name’,’fields and values in associative array’,’data type string ,decimal etc’) So above query looks in that order There one important section to use wordpress built in media uploader used with our ‘Image Upload’ field.
I will describe this in next step Step 5 : Using Wordpress buil in media uploader
we will be Using Wordpress buil in media uploader with our image upload field. In order to use that we have to call some css and js required for this uploader as shown below
Above in my_admin_scripts() ,it call some js using wordpress wp_enqueue_script() function . my_admin_styles()
will call css file that already declared in wordpress , we are just calling to use that .
So we have also use hooks ,they are admin_print_scripts and admin_print_styles to load them.
There a file under ads/my-script.js this will return back the uploaded
file url to the textfield of id "upload_image" .So this url will finally insert in our table column "image"