This post was written as part of the How to Write a WordPress Plugin series.
There will be situations where you will have a main administrative panel, but would like individual users to set their own preferences. In the case of the Devlounge Plugin Series, we added an option for text to be added in at the end of each post. However, what if a logged-in user doesn’t want to see this text? Why not give them the option without affecting all of the other users?
This post will go over the steps to add in your own User’s Administration Panel.
Name Your User Options
[php]
class DevloungePluginSeries {
var $adminOptionsName = “DevloungePluginSeriesAdminOptions”;
var $adminUsersName = “DevloungePluginSeriesAdminUsersOptions”;
[/php]
Line 3 shows where I added in the member variable called adminUsersName . I gave this variable the long and unique name of DevloungePluginSeriesAdminUsersOptions.
Set Your the Default User Options
You’re going to need a place to initialize your user options, especially when a user first activates your plugin. However, these options should also work outside of the admin panel where users may or may not be logged in.
Here’s the function I inserted in the DevloungePluginSeries class:
[php]
//Returns an array of user options
function getUserOptions() {
global $user_email;
if (empty($user_email)) {
get_currentuserinfo();
}
if (empty($user_email)) { return ”; }
$devOptions = get_option($this->adminUsersName);
if (!isset($devOptions)) {
$devOptions = array();
}
if (empty($devOptions[$user_email])) {
$devOptions[$user_email] = ‘true,true’;
update_option($this->adminUsersName, $devOptions);
}
return $devOptions;
}
[/php]
What this function does is:
- Checks to see if a user is logged in (lines 3 – 7). This is easily determined by checking to see if the user_email variable is set.
- Attempts to find previous options that may have been stored in the database (line 8).
- If options aren’t found, defaults are assigned (lines 9-15)
- The options are returned for your use (line 16).
Initialize the Admin User Options
The getUserOptions can be called at anytime to retrieve the admin user options. However, what about when the plugin is first installed (er, activated)? There should be some kind of function that is called that also retrieves the user options. I added the following function into the init function:
[php]
function init() {
$this->getAdminOptions();
$this->getUserOptions();
}
[/php]
Line 3 calls the new function getUserOptions. Since there is already an action added that calls the init function, no extra steps are necessary.
How the Admin Panel and User Panel Will Work Together
You will recall from the last post about setting up an admin panel that the WordPress admin could set the content at the end of the post, whether code was shown in the header, and whether an author’s name was uppercase in the comments. The user’s panel allows users who aren’t admin to be able to specify whether they want these options or not.
We’re going to allow the user to decide if they:
- Want content at the end of the post to show (only if the admin has this enabled already).
- Wants the comment authors to be uppercase (only if the admin has this enabled already).
Set up the User’s Panel Function
The first thing we want to do is set up a function that will actually print out the user’s panel. The function’s name will be printAdminUsersPage. This next bit of code will read in the options we specified earlier and check to see if any post options have been submitted. All the code in this section is assumed to be within the printAdminUsersPage function.
[php]//Prints out the admin page
function printAdminUsersPage() {
global $user_email;
if (empty($user_email)) {
get_currentuserinfo();
}
$devOptions = $this->getUserOptions();
//Save the updated options to the database
if (isset($_POST[‘update_devloungePluginSeriesSettings’]) && isset($_POST[‘devloungeAddContent’]) && isset($_POST[‘devloungeAuthor’])) {
if (isset($user_email)) {
$devOptions[$user_email] = $_POST[‘devloungeAddContent’] . “,” . $_POST[‘devloungeAuthor’];
?>
Settings successfully updated.
adminUsersName, $devOptions);
}
}
//Get the author options
$devOptions = $devOptions[$user_email];
$devOptions = explode(“,”, $devOptions);
if (sizeof($devOptions) >= 2) {
$content = $devOptions[0];
$author = $devOptions[1];
}
?>
[/php]
The above code:
- Retrieves the user options (line 7)
- Saved post data (if available) to the database (lines 9 – 18)
- Reads in comma-separated variables for the user.(lines 19-25)
The next bit of code will display the HTML form that is necessary for the user’s panel. All the code is doing is displaying the form elements and reading in options that were already retrieved.
[php]