Constructing a WordPress Plugin User’s Panel

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
class DevloungePluginSeries {
var $adminOptionsName = "DevloungePluginSeriesAdminOptions";
var $adminUsersName = "DevloungePluginSeriesAdminUsersOptions";
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:
//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;
}
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:
function init() {
$this->getAdminOptions();
$this->getUserOptions();
}
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.
//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'];
?>
<div class="updated"><p><strong>Settings successfully updated.</strong></p></div>
<?php
update_option($this->adminUsersName, $devOptions);
}
}
//Get the author options
$devOptions = $devOptions[$user_email];
$devOptions = explode(",", $devOptions);
if (sizeof($devOptions) >= 2) {
$content = $devOptions[0];
$author = $devOptions[1];
}
?>
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.
<div class=wrap>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<h2>Devlounge Plugin Series User Options</h2>
<h3>Allow Content Added to the End of a Post?</h3>
<p>Selecting "No" will disable the content from being added into the end of a post.</p>
<p><label for="devloungeAddContent_yes"><input type="radio" id="devloungeAddContent_yes" name="devloungeAddContent" value="true" <?php if ($content == "true") { _e('checked="checked"', "DevloungePluginSeries"); }?> /> Yes</label> <label for="devloungeAddContent_no"><input type="radio" id="devloungeAddContent_no" name="devloungeAddContent" value="false" <?php if ($content == "false") { _e('checked="checked"', "DevloungePluginSeries"); }?>/> No</label></p>
<h3>Allow Comment Authors to be Uppercase?</h3>
<p>Selecting "No" will leave the comment authors alone.</p>
<p><label for="devloungeAuthor_yes"><input type="radio" id="devloungeAuthor_yes" name="devloungeAuthor" value="true" <?php if ($author == "true") { _e('checked="checked"', "DevloungePluginSeries"); }?> /> Yes</label> <label for="devloungeAuthor_no"><input type="radio" id="devloungeAuthor_no" name="devloungeAuthor" value="false" <?php if ($author == "false") { _e('checked="checked"', "DevloungePluginSeries"); }?>/> No</label></p>
<div class="submit">
<input type="submit" name="update_devloungePluginSeriesSettings" value="<?php _e('Update Settings', 'DevloungePluginSeries') ?>" /></div>
</form>
</div>
<?php
}//End function printAdminUsersPage()
Set up the User’s Panel Action
While setting up the administrative panel, we specified a function called DevloungePluginSeries_ap that helped initialize the admin panel. We’re going to piggy back on this function in order to add in our user’s panel.
//Initialize the admin and users panel
if (!function_exists("DevloungePluginSeries_ap")) {
function DevloungePluginSeries_ap() {
global $dl_pluginSeries;
if (!isset($dl_pluginSeries)) {
return;
}
if (function_exists('add_options_page')) {
add_options_page('Devlounge Plugin Series', 'Devlounge Plugin Series', 9, basename(__FILE__), array(&$dl_pluginSeries, 'printAdminPage'));
}
if (function_exists('add_submenu_page')) {
add_submenu_page('profile.php', "Devlounge Plugin Series User Options","Devlounge Plugin Series User Options", 0, basename(__FILE__), array(&$dl_pluginSeries, 'printAdminUsersPage'));
}
}
}
On line 12, you can see a line of code that:
- Adds a sub-menu to the profile.php page.
- Let users with a user’s level greater than or equal to zero access to the user’s panel.
- Calls our printAdminUsersPage function.
The access level (in this case a 0) is described in more detail at the Users Levels page in the WordPress codex.
Conclusion
We now have an user’s panel that allows users their own control whether certain content is shown or actions performed. The code to influence the behavior of the plugin was added to the downloadable example, but was not described here. You can download the current version of the Devlounge Plugin Series plugin regarding a user’s panel.
If you were to test out the plugin code above, the admin could specify the overall behavior, but a user could override that behavior depending on the settings specified.
Download the Code Used In This Post
For further reading, please check out these links:


















