In our latest tutorial we started creating the MyFirstPlugin project.
In this tutorial we are going to have our plugin to make changes to the post title name.
The concepts that are covered in this guide.
- Move our plugin from under the settings admin area
- Creating a search button
- Creating an update page
- Interacting with the post title to update the post title name
NOTE: Our project will be displaying posts that are in draft mode, you will want to make sure you have a few draft mode posts already created in WordPress.
We are going to start by moving our plugin from under Settings and have it placed in the admin menu on the left.
Make the changes to the function myfirstplugin_admin_action().
The add_menu_page adds a top-level menu page. MyFirstPlugin is the title of the page as shown in the <title> tags.
My First Plugin is the name of your menu that is displayed on the Dashboard.
The manage_options checks for the administrator role.
The myfirstplugin is the slug name to refer to the menu; should be a unique name.
Next myfirstplugin_admin is the function to be called to display the page content for the item.
The dashicons-shield is a custom image to use as the menu icon and can be changed.
Lastly, we are using the position location in the menu in our project, 99 will place our plugin at the bottom.
Next, we are going to add some HTML headings to the top of our plugin to explain to the users what our plugin does.
Change the HTML <h4>My First Plugin</h4> with the changes below.
We are now going to create a search button that will load the option table with any post that are set to draft mode.
We have added a HTML form which will represent the button. We have also left the action parameter blank because we are not sending data to another page.
Our method parameter is set to post which will allow us to check if someone has clicked on the button.
The input tag has a type equal to submit, which defines our button as a submit button. The button will execute the query to the database.
The button name parameter we have entered the following string search_all_draft_post. For the name parameter you can make up whichever name you want.
We will use the string search_all_draft_post later for checking the post global variable $wpdb to check if our button was clicked.
The value parameter has the value of Load New Draft Post, which is displayed on the button as it appears to the user on the page.
The class parameter value has been set to button-primary, which has its own CSS class within WordPress.
Before the button will work, we need to add the code to our project.
We have added an if statement which is encasing the original code, which is preforming the database query, along with the foreach loop which is displaying the results.
Inside the if statement we have an isset statement.
The isset statement is checking the global variable search_all_draft_post, to see if our button has been clicked.
If the button has been clicked it will return true and the code inside the if statement will be executed.
To save the data into the options table inside the database, we will need to add the update_option function.
WordPress will check the database options table to see if ‘myfirstplugin_all_draft_posts’ already exists in the option_name column.
If ‘myfirstplugin_all_draft_posts’ does not exists in the database options table WordPress will create it.
The update_options function takes two arguments.
The first argument is our option name, and it is important that the name is unique.
The second argument is the values that will be stored in the option_value column in the database.
Next, we will add an else if statement, along with the get_option function.
The get_option function will retrieve our options from the options table.
However, it will return false if the option does not exist.
In our example if the option does exist, we will return an array of contents which is being stored in the $mytestdrafts variable.
Now we must initialize our $mytestdrafts variable, which we will place under the global variable.
$mytestdrafts = array();
We have assigned the $mytestdrafts variable to a blank array.
The reason for the blank array is it will ensure that we will not get an error in the for each loop if we do not meet the conditions in the if statements.
If you get an error, make sure you are closing the if statements at the right ending block.
Now we have all draft post being saved to the options table.
From here we want to be able to change the name of each Post Title that are in draft mode.
We could always click on the Posts menu find the correct Post Title that we want to change the name, but it would be great to be able make the change within our plugin.
We are going to recode the foreach loop.
Create two variables $id and $post_title with their correct values.
In the table we added a href with the admin_url function, the $id and $post_title values are being stored in the URL.
You will notice that the Post Title now have clickable links.
Also, if you click on the Post that are in draft, you will see the id and title change in the URL.
In the function myfirstplugin_admin, we will want to move the global $wpdb and $mytestdrafts variables in our project from where they currently are located.
We will create a new variable named $mytestdrafts_id which will get the id from the URL.
Next, create an if statement along with an else statement, make sure to close the else statement at the bottom of the project.
We are checking to see if the Post Title link has been clicked.
In the if statement we placed an echo message to visually see which id is being called.
After you see the test you will need to remark the echo message and unremarked the include_once(“updaterecords.php”);.
We will need to use the include_once(“updaterecords.php”); so that our project will call the updaterecords.php page once.
Next, we must add the functionality to our project so that we can make changes to the Post Title.
Creating the updaterecords.php page.
We will need to create a new php page and name it updaterecords.php, you can name the page which ever name you would like.
To add some security to the project, we are adding an if statement to check that role is an administrator with the manage_options.
If a hacker were to try to access the updaterecords.php within the URL path, they will not be allowed to view the page.
Next, we are going to get the id from the URL and place the value into the $myfirstplugin_id variable.
We are going to call the function myfirstplugin_update_form and passing the $myfirstplugin_id variable.
Inside the function myfirstplugin_update_form($myfirstplugin_id) we are calling another function called myfirstplugin_update and passing the $myfirstplugin_id variable.
But first we are going to continue in the function myfirstplugin_update_form($myfirstplugin_id).
Create an if statement to gather the data out of the options table and fill the $myfirstplugin_result variable with the array.
Create an if statement to check to see if the variable $myfirstplugin_id has data which will make the if statement true.
The foreach loop will iterate over $myfirstplugin_result while the value of the current element is assigned to $myfirstplugin_cols.
The $id is assigned the value from the array ID.
The if statement is checking for true, $id equals $myfirstplugin_id value.
Next, we are getting the title and placing that value into $myfirstplugin_title variable.
Creating the form – we must remember to end the php tag after the end of the if statement because we are going to add HTML inside the php page.
We create an HTML form to collect user input.
The method post is used to send the updated data to our database.
From here we create an input tag giving it a name inputposttitle, and echo out our $myfirstplugin_title variable, which will display the title.
The label is displaying the value from the $id variable.
The button tag is set to a type submit and given the name update.
From here we want to finish building out the if statement myfirstplugin_update that will allow updating the post table.
We have used a special WordPress class called $wpdb which is designed specifically for WordPress database interactions.
Load the $table_name variable with the posts table name.
The $post_title variable is getting the value from the input tag.
We add an if statement if(isset($_POST[‘update’])) to check if the update button has been clicked.
Next, we check to see if input tag is empty by creating an if statement if(empty($post_title)) and displaying a message.
In the else statement, we update the $table_name variable by using an array.
We show the users a message to let them know the records have been updated.
Lastly, we load the $mytestdrafts variable with all the records from the posts table and show the draft post.
Make a change to the Post Title and click on the Update button to see the plugin working.
Creating your own plugins can be extremely rewarding. Now you have stepped through the process of creating a CRUD plugin. You should now understand that you can create your own table and select data, update, and delete from the table.