Plugins are programs designed to modify or enhance WordPress’s core functionality.
We’re all familiar with the Dashboard. When you click on Plugins you’ll get a submenu:
One of the plugins, Akismet, needs a key to work. It is a very important plugin, probably the best one to install first. It takes care of the spam, because if you don’t have Akismet activated you will notice an awful lot of junk comments.
In the above image there are a couple of menu items that appear only because its plugin is activated. In the top menu is Add Event, placed there by a calendar plugin. In the left menu is another item placed there by the Nano Plugins.
Before installing a plugin, think about your website and how you want it to look and behave in addition to the core functionality provided. Then, search for the proper plugin in the WordPress Directory. Much is offered. You’ll find that these are mostly bulletproof programs written by programmers to do things in a specific way. If you want things to be done differently, you can modify their code, or create a plugin of your own from scratch. Either way, you will need to become familiar with the PHP programming language.
Most people have never created a plugin. How difficult do you think it is? Do you know where to begin?
I’ve created a simple plugin named ZapForm, for demonstration purposes. You’ll notice on the Installed Plugins page that it is ready to be activated:
Alongside its Name, there are a Description, Version number, Author with a hyperlink to the Author URI, and the label “Visit plugin site” which has a link to the Plugin URI.
In the next post you’ll see where this plugin and its information is located.
These are the files you received from wordpress.org.
In the wp-content directory are several files, one of which is the plugins folder which comes with akismet and hello.php included. (Two other plugins, named debug-bar and debug-bar-console, are extra plugins I’ve added just to play with. They are not required.)
The zapform folder is the plugin containing the zapform.php file. It is an ordinary text file with the file name extension telling the server to interpret it as a PHP program.
This is all the code in the plugin (at least for now):
That’s it?! If you understand PHP then you can see that it does nothing. All the text is within a comment.
To make this file into a plugin, the only thing required is the Plugin Name. WordPress reads the name from the text and also the other information found in the comment to get the Description, Version number, Author with Author URI, and the Plugin URI. This is the information we saw earlier on the Installed Plugins page.
Other than that, there is no executable php code here to do anything.
So, lets get this plugin to do something, such as create a page.
If we put this code into the file it will create a page:
The first line, register_activation_hook asks to run the createPage function when the plugin is first activated.
In the createPage function, the important command is wp_insert_post. The parameter it takes is an array with information about the page.
$page is an array with five elements: post_content, post_parent, post_status, post_title, and post_type. These are the names of the elements of the $page array.
The most interesting one of the five elements is post_content. The text string [zapform] will be the only content we put into this newly created page. The text string [zapform] does nothing until we make it into a shortcode (see Add Shortcode below).
BTW: In the array being passed to wp_insert_post above, realize that the post_type element doesn’t have to be a page, it could be a simple post or any of several other types. Same thing with the post_status element which could be draft, or pending, etc. Look in the documentation and you will see the many options for post_type and post_status. The following is pseudo code. It is formatted this way as documentation in the WordPress codex:
When the shortcode [zapform] is found in a post or page or even in the text field of a widget, the function evaluateForm is executed.
In the evaluateForm function, the value of one element of the $_POST array, submitForm, is tested to see if it’s been set with a value. $_POST(‘submitForm’) will be set to a value by the Submit button of the HTML form we are about to add. The $_POST array gets filled with values from the form because we are using the POST method. (The other method possible to use is the GET method.)
So far, the submitForm value has not been set, so the only thing that happens at first is the other four array values get initialized.
Next we’ll add a line of code to include the HTML file.
By including the html.txt file, now it will be possible to get all the values so that the the top half of this conditional will be executed. (That is where we will put the command to send email.)
What is in the html.txt file?
This is HTML code for a form with three text fields, one radio button field, and a submit button:
The above code executes PHP to get the name of the page. Easier to get that action value by using the hash character (Thanks Greg!):
We’ve pre-set the value for the Subject line to “WP Survey3”.
Notice that the value for the submit type of input is pre-set to “Send”. This is the label that will appear on the button of the form. Like the other values, it can be any text string.
html.txt <- code in text format
Activating the ZapForm plugin will create a page with a form object that looks like this:
The blanks can be filled in and the pre-set entry in the Subject text box can be changed by the user.
The radio button type allows only one item to be selected. (If we had made the type checkbox, then more than one item could be selected.)
After pressing the Send button, the user needs to be told that the email has been sent. Next.
Now, when the Send button is pressed, the submitForm variable will evaluate as true in the evaluateForm function, and we can work our magic by calling the sendMail command using the parameters set in the $_POST array by the HTML form:
But, before calling the sendMail function, let’s make sure that the email address is valid by using one of the built-in filters:
We’ll create the sendMail function in the next post. This is the evaluateForm function so far:
The echo command is a PHP language construct that outputs the text strings. The text strings will appear with the form, anyplace the active shortcode is being used.
BTW, it might also be important to filter the other text fields from passing malicious code.
This is our final piece of code. It uses the built-in PHP mail function:
The period is a concatenation operator. The .= is an append operator.
This demonstrates a very simple use of the mail command with three parameters. A fourth parameter could be added for the email header.
This is the entire zapform.php code. It works in conjunction with the html.txt file:
zapform.php <- code in text format
There is also a third file included in the zapform plugin directory: readme.txt
To make your entry in the plugin browser most useful, each plugin should have a readme file named readme.txt that adheres to the WordPress plugin readme file standard.