• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Building a Control Panel for Your WordPress Theme ...

The WordPress theme market has been growing rapidly, and theme developers are keen to develop the highest quality, premium WordPress themes. With growing demand from Bloggers on WordPress themes that are more-like CMS, magazine, or newspapers, It has been very important to learn the basics of creating a functional control panel for a WordPress theme.

Today, both bloggers and theme developers want to learn how to create a control panel for their themes. Basically, a control panel becomes mandatory If your wordpress theme uses magazine options, or you would like to allow the end-user a very easy customization of his theme via a user-friendly control panel interface.

We will get into this tutorial shortly, and this tutorial is suitable for any theme. We will discuss how to create a very basic control panel for WordPress theme.


The Basics

The control panel code goes in functions.php file. That makes sense, the functions file should contain all additional theme functions / options. So the first step is that you need to create a functions.php file if it does not exist in your theme folder.

I would prefer working with an empty functions file. Just to keep things arranged and clean!


Defining Goal from this Control Panel

You must have a set of goals before writing your control panel. Basically, which options you need to take care of in the control panel via Dashboard? To make this story short, our goal is:

Creating a control panel that displays a predefined sentence directly in the blog and controlled via the control panel.


The Code

The control panel code is pretty simple. I will explain it for you but first copy/paste it in your functions.php

<?php
/* New control panel class */
class ControlPanel {
 
/* Store DEFAULT SETTINGS in an array..and create a variable to store options. */
var $default_settings = Array(
quote => Welcome to my blog!
);
 
var $options;
 
/* Create new control panel function and defines administration menu, and head you do not need to modify anything in this part */
function ControlPanel() {
add_action(admin_menu, array(&$this, add_menu));
add_action(admin_head, array(&$this, admin_head));
 
/* The following code simply add options. and note the word amytheme this can be changed to your theme name actually you just need to make all of them look same. */
if (!is_array(get_option( amytheme)))
add_option( amytheme, $this->default_settings);
$this->options = get_option( amytheme);
}
 
/* This adds administration menu: Under Design, it adds a theme page called Simple Control Panel and also notice how I didn change mytheme name */
function add_menu() {
add_theme_page(Simple Control Panel, Simple Control Panel, 8, "mytheme", array(&$this, optionsmenu));
}
 
/* Here you put your form CSS. The control panel form has a class of themeform. so Just create your stylesheet here for themeform fields, inputs, etc...I will leave this part for you as its really easy */
function admin_head() {
print <style type="text/css">;
/* Obviously some css goes here.. for .themeform { */
print </style>;
}
 
function optionsmenu() {
if ($_POST[ iss_action] == isave) {
 
$this->options["quote"] = $_POST[cp_quote]; /* This is just to update the quote every time we update the form */
 
update_option( amytheme, $this->options); /* Tells wordpress to update your options.. */
 
echo <div class="updated fade" id="message" style="background-color: rgb(255, 251, 204); width: 400px; margin-left: 17px; margin-top: 17px;"><p>Your changes have been <strong>saved</strong>.</p></div>; /* Displays successful message.. like changes have been saved! */
}
 
/* That is all.. lets print the form to browser */
echo <form action="" method="post" class="themeform">;
echo <fieldset>;
echo <input type="hidden" id="ss_action" name="ss_action" value="save">;
 
/* Below is our nice field which will control the option: Quote */
echo <label for="cp_quote">Which quote do you want to display to the blog?</label><input class="widefat" style="width:300px" name="cp_quote" id="cp_quote" value=".$this->options["quote"]."><div style="clear:both"></div>;
 
/* The default value is Welcome to my blog! but you can always change that..just as a very simple example */
 
echo <p class="submit"><input type="submit" value="Save Changes" name="cp_save" /></p>;
echo </fieldset>;
echo </form>;
}
 
} /* Quit Control Panel Class. Very important! */
?>

Pre-Final Step

In the end of functions.php file after the above code, we need to initiat a new object of the control panel some how. How to do this?

Simple add this code below the above code:

<?php
$cpanel = new ControlPanel(); /* This initiates a new control panel object */
$mytheme_options = get_option( amytheme); /* This creates a variable and store your option in it to be able to call it in your template */
?>

Now you can display the quote in your WordPress template. Let us say header.php. Open it and add the following code where you wish the quote to appear:

<?php
 
global $mytheme_options;
print $mytheme_options[quote]; // This parses the quote (option) value from control panel
 
/* Done */
?>

Ofcourse, there are no limitations. Try to do some practice like creating more than one option, this is pretty basic but it should get you started.

 Original Source:
http://customthemedesign.com

AddThis Social Bookmark Button

Posted at 11:39:52 am | Permalink | Posted in WordPress Plugins  Wordpress Themes  

Related Stuff

Google Buzz Button Wordpress Plugin Released

If you are looking for a Google Buzz button to add into your Wordpress site then we have released the first Wordpress plugin exclusively ...

Plug And Play Ecommerce With Wordpress Plugins

Since 2003 Wordpress has slowly been gaining popularity amongst the elite of the internet, the bloggers. It is one of those few things ...

Add Google Search to Your WordPress Blog

The native WordPress search does not return very relevant results, thus it makes a lot of sense to add Google Search into your WordPress ...

Add More Sidebars to Your WordPress Theme

You can add more than one sidebar section to your WordPress site. For example, with the stc-intermountain.org site, I added a whole bunch ...

Series Posting in Wordpress

In my functions.php file, I have some code which implements series posting. This relies on the thematic ...

Top Stuff

Free Blogger templates Anime Themes

Wordpress Guestbook Generator Plugin

48 Unique Ways To Use WordPress

GeekLog

WordPress Single Post Templates

Get The Image WordPress Plugin


KoolIM.com

Access your instant messenger directly from the browser, have access to AIM, ICQ, MSN, Yahoo!, IRC, Jabber and many others.


About Webloglines

Webloglines is a project offers a comprehensive collection of blogging services, articles, themes and plugins from around the world. Whether you're looking to promote your own blog or find blogs on various topics, this site is for you.


Search


Topics

  • Adsense (12)
  • Blogging Tips (73)
  • Blogs Slides (25)
  • Blogs Websites (22)
  • Digg (20)
  • How to Blog (129)
  • Search Engines (9)
  • SEO (135)
  • WordPress Plugins (269)
  • WordPress Security (75)
  • Wordpress Themes (99)
  • Wordpress Tips (162)

© 2006 www.webloglines.com. All Rights Reserved. Powered by IRange