WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Creating/Displaying correctly Theme Options (3 posts)

  1. davewise
    Member
    Posted 2 years ago #

    I have a theme options page working just fine, I want to add the ability to select multiple categories for use with something else inside the theme.

    I currently have a very simple flat structure that's working

    $options = array(
    	'background_color' => '#fff',
    	'default_color' => '#ccc',
    	'title_color' => '#ccc',
    	'titlehover_color' => '#F60',
    	'strapline_color' => '#ccc',
    	'storytitle_color' => '#ccc',
    	'link_color' => '#ccc',
    	'linkhover_color' => '#F60',
    	'em_color' => '#F60',
    	'logo' => '',
    	'iconsize' => '32px',
    	'footer_copyright' => 'Copyright Protected 2012 © all rights reserved'
    );

    I want to add a multiple select to these saved options, which should be a case of just adding a new variable thus:
    'categories' => array()

    Which is fine, I am a little confused as to how I add the select statement to the options page.

    I currently have:
    <select id="categories[]" name="options[categories][]" multiple="multiple" size="10" style="width: 100px;" >

    But it doesn't seem to work (save) along with the other variables. Am I missing something really stupid?

    Thanks in advance!

    D

  2. davewise
    Member
    Posted 2 years ago #

    Never mind it was working, I was just looking in the wrong place for the answer, one of those "Staring at the screen to long" moments, as soon as I woke up I spotted it.

    For anyone that's interested it's for selecting multiple category ID's to pass to external plugins, I am just adapting a tile theme to insert available plugins (or widgets) into the space where a post would be. So not only do you get posts as part of the index tiles, you can now get ad's (via AdRotate and recent category news from Special Recent Posts) randomly added to the mix. The theme option is basically so you can supply a random category (selected) to the recent posts call. I build an array of known available (i.e.. active) plugins, build the parameters and functions and store the calling function in an array. Then at user selected intervals insert a plugin rather than a post.

    Not very exciting and I'm sure it's been done time and time again, but hey, might as well give it a go.

    Using the above options builds a Select statement as follows :

    <select id="categories" name="options[categories][]" multiple="multiple" size="10" style="width: 100px;" >
    
    <?php
    	$settings = get_option( 'options', $options );
    
    	$arg  = array(
    	'type'                     => 'post',
    	'child_of'                 => 0,
    	'parent'                   => '',
    	'orderby'                  => 'name',
    	'order'                    => 'ASC',
    	'hide_empty'               => 0,
    	'taxonomy'                 => 'category');
    
    	$siteCategories = get_categories($arg);
    
    	foreach ($siteCategories as $siteCat) {
    		$siteCatID = $siteCat->cat_ID;
    		$siteCatName = $siteCat->cat_name;
    		if (in_array($siteCatID, $settings['categories']))
    			echo "<option value='${siteCatID}' selected='selected'>$siteCatName</option>";
    		else
    			echo "<option value='${siteCatID}'>$siteCatName</option>";
    	}
    ?>
    </select>

    So now, when I insert a "Special Posts" Plugin I can supply random categories from the list selected rather than just the default.

    If you're interested in the actual code that builds an active plugin list with associated arrays and functions, here it is.

    // Lets start by using the wordpress built in functions to find out what we have
    	// and what's active that we recognise.
    
    	// Our returning array will contain a numerical index, a name and the code needed
    	// to make it work.
    
    	// These are the plugins we currently know about and know how to insert them via
    	// PHP into the main html output.
    
    	$specialposts = array('name' => 'Special Recent Posts', 'function' => create_function('', '$settings = get_option("infinity_options"); $totalCats = count($settings["categories"]); $randCat = $settings["categories"][rand(0, $totalCats - 1)]; $randName = get_cat_name($randCat); $args = array("srp_widget_title" => "$randName Recent Articles", "srp_number_post_option" => "3", "srp_filter_cat_option" => $randCat); special_recent_posts($args);'));
    	$wp125 = array('name' => 'WP125', 'function' => create_function('', 'echo "<center>"; wp125_write_ads(); echo "</center>"; '));
    	$adrotate = array ('name' => 'AdRotate', 'function' => create_function('', 'echo adrotate_group("1");'));
    
    	$knownplugins = array ($specialposts, $wp125, $adrotate);
    	$knownpluginsno = count($knownplugins);
    
    	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    	$allplugins = get_plugins();	
    
    	$activeplugins = 0;
    
    	foreach ($allplugins as $plugin => $pdata) {
    
    		// Check all plugins against our list of known plugins
    		for ($i=0; $i<($knownpluginsno); $i++) {
    
    			if ((strcmp($pdata['Name'], $knownplugins[$i]['name']) == 0) and is_plugin_active($plugin)) {
    				$siteplugins[$activeplugins++] = $knownplugins[$i];
    			}
    		}
    
    	}
    
    	return $siteplugins;

    Anyway, I hope that helps add inspiration to someone, somewhere, some time ;)

    Onwards and upwards as they say.

    D.

  3. davewise
    Member
    Posted 2 years ago #

    Oh, btw, here is how you execute the function by calling it in the place where you want it to occur.

    <?php
    	echo $siteplugins[rand(0, $totalPlugins -1)]['function']();
    ?>

    Just in case ;)

    D.

Topic Closed

This topic has been closed to new replies.

About this Topic