WordPress Plugin to set options switch on/off stuff in template

von Joern am 18. Mrz. 2014 | Keine Kommentare

looking for a plugin to switch on or off some options used in different templates I found something finally.

I wanted to be able to switch different advertising in my dashboard, let’s say a header script, different ads in sidebar, index or single posts.

with credits to http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/ I give you here the plugin code and tell you how I use it.

following code you place in a php file into your plugin folder, name it as you like, mine is called ‚jw-add-options.php‘

The file content:


Plugin Name: my add Options
Plugin URI: http://planetozh.com/
Description: sets options to show adds or not
Author: Ozh
Author URI: http://planetozh.com/
/* from here 010214
add_action('admin_init', 'my_addoptions_init' );
add_action('admin_menu', 'my_addoptions_add_page');

// Init plugin options to white list our options
function my_addoptions_init(){
   register_setting( 'my_addoptions_options', 'my_add_sttings', 'my_addoptions_validate' );

// Add menu page
function my_addoptions_add_page() {
   add_options_page('My Add Options', 'ADD Options', 'manage_options', 'my_addoptions', 'my_addoptions_do_page');

// Draw the menu page itself
function my_addoptions_do_page() {
   <div class="wrap">
      <h2>Add Options</h2>
      <form method="post" action="options.php">
         <?php settings_fields('my_addoptions_options'); ?>
         <?php $options = get_option('my_add_sttings'); ?>
         <table class="form-table">
            <tr valign="top"><th scope="row">header adds</th>
               <td><input name="my_add_sttings[head_add]" type="checkbox" value="1" <?php checked('1', $options['head_add']); ?> /></td>
            <tr valign="top"><th scope="row">siderbar adds</th>
               <td><input name="my_add_sttings[sb_add]" type="checkbox" value="1" <?php checked('1', $options['sb_add']); ?> /></td>
            <tr valign="top"><th scope="row">siderbar adds 2</th>
               <td><input name="my_add_sttings[sb_add2]" type="checkbox" value="1" <?php checked('1', $options['sb_add2']); ?> /></td>
            <tr valign="top"><th scope="row">content adds</th>
               <td><input name="my_add_sttings[cont_add]" type="checkbox" value="1" <?php checked('1', $options['cont_add']); ?> /></td>
            <tr valign="top"><th scope="row">content adds 2</th>
               <td><input name="my_add_sttings[cont_add2]" type="checkbox" value="1" <?php checked('1', $options['cont_add2']); ?> /></td>
            <tr valign="top"><th scope="row">Some text</th>
               <td><input type="text" size="80" name="my_add_sttings[sometext]" value="<?php echo $options['sometext']; ?>" /></td>
         <p class="submit">
         <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />

// Sanitize and validate input. Accepts an array, return a sanitized array.
function my_addoptions_validate($input) {
   // Our first value is either 0 or 1
   $input['head_add'] = ( $input['head_add'] == 1 ? 1 : 0 );
   $input['sb_add'] = ( $input['sb_add'] == 1 ? 1 : 0 );
   $input['sb_add2'] = ( $input['sb_add2'] == 1 ? 1 : 0 );
   $input['cont_add'] = ( $input['cont_add'] == 1 ? 1 : 0 );
   $input['cont_add2'] = ( $input['cont_add2'] == 1 ? 1 : 0 );
   // Say our second option must be safe text with no HTML tags
   $input['sometext'] =  wp_filter_nohtml_kses($input['sometext']);
   return $input;

having this plugin in your WordPress plugin folder, you can enable it in your dashboard and will find a new entry under your dasboards ‚Settings‘

There are only 5 options to set, enabled or disabled, and a text field I use just for my own remarks.
All settings a saved in your wp_options table in just one row called ‚my_add_sttings‘ (forgot the letter e, I know..)

Now I can set an option to enabled – means tick and save in plugin settings.

to use it in a template we have to edit the template to check for the state of one of this options.

try it with a single.php or content-single.php like this:


<?php $options = get_option('my_add_sttings'); if( $options['cont_add'] != '' ) : ?>
here the stuff or code you want to switch on or off
<?php endif  ?>

I am used to keep advertising scripts in seperate files, so for one add I use a file with the respective code. I use a file in themes directory, here adcont1.php for this option ‚cont_add‘ and load it as follows:


<?php $options = get_option('my_add_sttings'); if( $options['cont_add'] != '' ) : ?>
<?php if (file_exists(TEMPLATEPATH . '/adcont1.php')) { include (TEMPLATEPATH . '/adcont1.php'); } ?>
<?php endif  ?>

Just thinking about it – I could also load one file from all locations I want to enable something. this file contains the check for all options and all scripts under each option.
Maybe not so good, sometimes need some additional div style or condition ( is_single, is_search ) – no, really not so good my last thoughts.

Having questions you are welcome to register here and ask – or check the site I got the code from. ( planetozh.com )
you find me on Google+, Twitter and Facebook

(von: Joern)

Sorry, no posts matched your criteria.


Du findest mich auch auf Google+, Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.

weitere forum Beiträge: