WordPress Create plugin Select Update Delete Loops serialize deserialize

In this post I am going to try and give a simple guide and several options in WordPress Create plugin Select Update Delete Loops serialize deserialize.

The list of things I am going to cover are

  • Create a simple plugin for the tutorial
  • Connecting to a MySQL database in WordPress/PHP
  • Querying the database for row(s) of data
  • Looping through the data
  • Deserilazing a column of data
  • Modifying, adding and deleting serilaized data
  • Updating the database with the serialized data again

As you can see I am going to cover many areas I will also use simple examples to demonstrate without going into too much technical detail.

I am going to go through the various areas in the format of a tutorial, if you are just starting out in PHP/WordPress development then it wouldn’t hurt for you to follow all the steps in this tutorial and then you can apply the skills learned to your own piece of work.

Create a simple plugin for the tutorial

I am not going to go into depth about how to create plug-ins, best practices and many details just a simple overview so we can use this tutorial.

Create a directory in your wp-content/plugins directory

Create a plugin php file the same name as the plugin

This is your main plugin file where we are going to do all the coding, now edit the file and enter the below information so WordPress knows you have a new plugin.

Activate the plugin

Select Activate (Don’t worry about Network Activate here, I am using multi-site but it is the same on single site)

You have now created a WordPress plugin – cool eh?!

Create a plugin test page

Now I am also going to create a new blank page and menu option so we can test this plugin.

Go to Pages -> Add New and create the above page, you only need to enter a title then ‘Publish’

Go to Appearance -> Menus, type the menu name ‘Main Menu’, and ‘Add Menu’, then from the left hand side select your new page, and ‘Add to Menu.

Make sure the ‘Primary Menu’ box is checked so the menu is added to your theme.

Select ‘Save’ and then visit your site.

You should now see your new menu option for your new page.

Create Some Sample Data

Now we should have a plugin, a page and a menu option, to start developing we need to create some sample data, so open up phpMyAdmin.

If you haven’t used phpMyAdmin before then there is a description in one of my posts on how to get phpMyAdmin working if you are running on a cloud server or there are IP address security restrictions on logging in to it, you need to set up a port tunnel, the blog is below.

Create and clone a WordPress site in 15 minutes

In phpMyAdmin select your database from the left hand side list, then the SQL tab in the right hand side pane, and input the following SQL.

Select ‘Go’ and you should now have a simple table with three products in it.  Note the details column is going to be our serialized data so we will populate this in PHP later in the tutorial.

Connect to the MySQL database and query the data

Edit your wp-blog-tutorials.php that we created earlier.

And add the following code so your file looks like below.  I will explain each step after that.

Now this isn’t as scary as it looks let me explain what is going on.

WordPress works on things called filters and actions this is how developers write plugins to WordPress and customise it.  We are adding a filter (hook) on ‘the_content’  which means every time your site displays a page it runs the function ‘gth_tutorial_1’ so we can customise the content returned in our function.

This sets up the plugin global variable that allows us to use WordPress database functions.  This needs to be in the body of your PHP file and also in the function using it too.

We only want to customise the content returned on our tutorial page for the moment

This creates our query and executes it to a variable called products which is the full result of our query.

We need to be able to query individual rows so we need to cycle through them and put each row into a $product array variable, we can then reference these using $product->${field name}

We then return all content that the page would normally return ($content) but add our own $output variable to the end of the page.

If we are not on our tutorial page then just return the $content and there is no change to the page.

The output from your page should look something like below

You have now written a plugin, created a WordPress ‘hook’, connected to a database, queried and returned the data to a page – excited yet?!

Deserialize your data add some serialized data, reserialize again and update the database

Change your foreach section to look like below

So again let me explain what is happening

For each product it now unserializes the details column, even if there is no data there so we create and array variable ($unserial)

If we are currently on the Plug product then we want to add some values.  $unserial[] is similar to a PHP array_push function it just adds an extra value to that array.

This then updates the database at the current product we are cycling through with the new serialized value.

If we now view our page we see something like this.

And if we use phpMyAdmin to view the database we can see the serialized data has been added.

Your full wp-blog-tutorials.php script should now look like this

Delete and update serialized data

We now modify the script and add a section to update and remove serialized data, I have included the whole script below.  I have made a couple of other changes too in outputting the data and clearing down the data for the purpose of this tutorial – I have also put some comments in the script.

The full script should look like below

The key things to note from the update/delete section are

  1. unset($unserial[$key]) is the command that removes the value while cycling through the array
  2. The update is simply done by setting the $unserial[$key] to whatever value you wish

Your output screen should now look like below

You can view the output of the tutorial on my site at https://blogtutorial.gothai.land/blog-tutorial-1-plugin-database-development/

The script is functional but there are some things I haven’t gone into detail that you would probably want to implement with any code you write such as error handling – I may blog on this at a future date.  Please subscribe to my newsletter and blog for any future updates.

Hopefully you were able to follow everything in the tutorial, if not please feel free to ask me any questions.

Have fun!

 

 

 

 

Paul H

IT consultant with 20+ years experience specialising in Oracle Database, Oracle Business Intelligence, Web/Mobile development, Application Express development, cloud technology and more

Leave a Reply

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