You are here: WTF » Documentation »

Adding Custom Options

Adding Custom Options

Adding custom options to the WTF options page is fairly straight forward and very similar to adding options to a normal theme options page. The only file you are going to need to edit is the wtf.php file in the “wtf” folder. There are three places where you need to edit the code.

In the wtf_admin() function you will see the following bit of code:

//set defaults
if(!get_option('wtf_top_nav')) add_option('wtf_top_nav', 'pages');
if(!get_option('wtf_rss_url')) add_option('wtf_rss_url', '');
if(!get_option('wtf_rss_email')) add_option('wtf_rss_email', '');
if(!get_option('wtf_tracking_code')) add_option('wtf_tracking_code', '');
if(!get_option('wtf_custom_css')) add_option('wtf_custom_css', '');
if(!get_option('wtf_exclude_rss_cats')) add_option('wtf_exclude_rss_cats', '');
//Add your custom option default
if(!get_option('wtf_custom_option1')) add_option('wtf_custom_option1', '');

What you need to do add the default value for your own option. In this case we have added custom_option1 (prefixed with wtf_). The default value is blank however you can make the default whatever you please.

In the wtf_page() function you will see the following bit of code:

update_option('wtf_top_nav', strip_tags(stripslashes($_POST['top_nav'])));
update_option('wtf_rss_url', strip_tags(stripslashes($_POST['rss_url'])));
update_option('wtf_rss_email', strip_tags(stripslashes($_POST['email_url'])));
update_option('wtf_tracking_code', strip_tags(stripslashes($_POST['tracking_code'])));
update_option('wtf_custom_css', strip_tags(stripslashes($_POST['custom_css'])));
update_option('wtf_exclude_rss_cats', strip_tags(stripslashes($_POST['exclude_rss_cats'])));
//Update your custom option
update_option('wtf_custom_option1', strip_tags(stripslashes($_POST['custom_option1'])));

This is where your option get’s updated when we click “save” on the options page. The value inside the $_POST is defined below in the HTML form. That’s it.

The HTML form sections of the page are in the following format:

<!-- START SECTION -->
<div class="section">
	<div class="section-title">
		<h3>Site Options</h3>
		<input type="submit" />
	</div>
 
	<!-- START OPTIONS -->
	<div class="option">
		<label>Label</label>
		<span class="description">Put your description here.</span>
		<input type="text" class="regular-text" />
		<div class="clear"></div>
	</div>
 
	... add more options here ...
 
	<!-- END OPTIONS -->
 
</div>
<!-- END SECTION -->

As you can see elements are split into sections. Each section can contain multiple options. Simply use this format to add your own options and sections. Notice that the id and name values of each input need to be the same as the $_POST value we used above. Also in this example we have used a text input but you can make your options whatever you want (e.g. radio buttons, textarea etc.).

Use your Option in your Theme

Now that you have added your custom option to use it in your theme you simply need to use the following line of code:

<?php echo get_option('wtf_custom_option1'); ?>

This can be used anywhere in your theme.