How To Save Settings and Call That Value Again?

Discussion in 'PHP & Perl' started by wgn_white, Nov 30, 2009.

  1. wgn_white

    wgn_white Regular Member

    Joined:
    Oct 12, 2009
    Messages:
    246
    Likes Received:
    27
    Hello to all.

    I need help.

    I'm making a wordpress theme with some admin interface.
    My mission is towards "No Hand Code For User".

    After reading the WordPress codex about a week ago, I manage to register my admin page under the Appearance tab which what I want.

    Now I want to put form in that page which I already know how to and yes, the form appear at that page. But I don't know where to save that options and how to call back the value of the option.

    My goal is to make css selector for my users.

    This is how I do it so far.

    I put all the css files inside one directory and using scandir and readdir, I manage to display the css filename in <option> by using a loop.

    What I want, when the users select which css file name and the click save, that file name will be saved.

    And then, I will need to call the chosen filename again and then use add_action to hook it to <head> so it will import the css.

    Am I making myself clear?

    Please enlighten me, I have read the WordPress codex and still don't get it, and I don't have formal lesson on PHP and mySQL database. I'm just enthusiast.

    Best regards,
    wgn_white
     
  2. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    109
    Use update_option function

    For example
    Code:
    $css_filename = 'css_file';
    $css_filename_value = 'style1.css';
    update_option($css_filename,$css_filename_value);
    
    The code above would save the filename or update it if it is already present. Makesure $css_filename is unique and will not conflict with any wordpress options

    And to read the option
    Code:
    $css_file = get_option($css_filename);
    
    Hope that was clear.
     
    • Thanks Thanks x 1
  3. wgn_white

    wgn_white Regular Member

    Joined:
    Oct 12, 2009
    Messages:
    246
    Likes Received:
    27
    Alright, I'm trying to implement it now.
     
  4. wgn_white

    wgn_white Regular Member

    Joined:
    Oct 12, 2009
    Messages:
    246
    Likes Received:
    27
    Hey, guys.

    I manage to save the setting value and also being able to call the value.
    Thank you for the reply.

    But I have another problem.

    I get this error,

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\wordpress\wp-content\themes\wpthemestudiox2\wptsx2-includes\colour-switcher.php:12) in C:\xampplite\htdocs\wordpress\wp-includes\pluggable.php on line 865
    
    After the submit form being submit.

    I know some of you might say that delete whitespace, etc.
    Cannot find anything such as, I'm using dreamweaver cs3.

    Here's the code of the functions.

    Code:
    if ( is_admin() ){
    
    	add_action('admin_init', 'wptsx2_register_colourswitcher_settings');
    	add_action('admin_menu', 'wptsx2_admin_reg');
    
    }
    
    else {
    
    	// Nothing. Really.
    
    }
    
    function wptsx2_admin_reg() {
    
    	add_submenu_page('themes.php', 'WPThemestudio X2 Administration', 'WPTS X2', 'administrator', 'wptsx2-admin/admin-page.php', 'wptsx2_admin_page');
    
    }
    Code:
    <?php function wptsx2_admin_page() { ?>
    
    	<div id="wptsx2-admin" class="wrap">
    	
    		<h1>WPThemestudio X2 Administration Page</h1>
    		<p>Change settings for your WPThemestudio X2 theme here.</p>
    		
    		<div id="colour-switcher">
    		
    			<h2>Stylesheet</h2>
    		
    			<div class="widefat" style="padding:8px; width:600px;">
    			
    				<?php wptsx2_colour_switcher(); ?>
    			
    			</div>
    		
    		</div>
    		
    		<div style="margin-top:30px;">
    			<small>Powered by <a href="http://wordpress.org" title="WordPress">WordPress</a> & <a href="http://wpthemestudio.com/wordpress-themes/wpthemestudiox2/" title="WPThemestudio X2">WPThemestudio X2</a> by <a href="http://pangeran.org" title="Pangeran Wiguan">Pangeran Wiguan</a>.</small>
    		</div>
    	
    	</div>
    
    <?php } ?>
    Code:
    <?php function wptsx2_register_colourswitcher_settings() {
    
    	register_setting('wptsx2_colour_switcher_options', 'wptsx2_colourfile');
    
    }
    
    function wptsx2_colour_switcher() { ?>
    
    <form name="colourswitcherform" action="options.php" method="post">
    
    	<?php settings_fields('wptsx2_colour_switcher_options'); ?>
    	
    	<?php if($_POST['actions'] == 'update') { ?>
    
    		<div class="updated"><p><strong>Saved!</strong></p></div>
    
    	<?php } ?>
    
    	<div>
    		<p>Please type the colour scheme stylesheet that you would like to use.<br />
    		Example: <code>default-colour.css</code> or <code>blue-colour.css</code>.</p>
    		<p>Below are the available colour scheme stylesheet.<br />
    		You can add your own stylesheet at <code>wptsx2-theme</code> folder.</p>
    		<p>The default setting is <code>default-colour.css</code></p>
    		
    		<ol>
    		
    		<?php $colourdir = TEMPLATEPATH . "/wptsx2-theme/";
    		$colouragent = opendir($colourdir);
    		
    		while($colourfile = readdir($colouragent)) {
    		
    			if(is_dir($colourfile)) continue;
    			
    			echo "<li>" . $colourfile ."</li>";
    		
    		} ?>
    		
    		</ol>
    						
    		<p class="submit">
    			<input type="text" id="wptsx2_colourfile" name="wptsx2_colourfile" value="<?php echo get_option('wptsx2_colourfile'); ?>" /><input type="submit" class="button-primary" value="Save" />
    		</p>
    	</div>
    	
    </form>
    
    <?php }
    
    function the_colour() {
    
    	if(get_option('wptsx2_colourfile') == FALSE) { ?>
    
    		<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/wptsx2-themes/default-colour.css" type="text/css" media="screen" />
    	
    	<?php }
    	
    	else { ?>
    	
    		<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/wptsx2-themes/<?php echo get_option('wptsx2_colourfile'); ?>" type="text/css" media="screen" />
    	
    	<?php }
    } ?>
    
    Thank you very much.

    Regards,
    wgn_white
     
  5. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    109
    You are sending stuff that should be in the header after the
    Code:
    </head>
    tag.

    From your code the likely culprit are the
    Code:
    <link>
    
    tags in the function the_colour().

    That function should be called before the header tag is closed. I don't see the point at which you are calling this function. You are probably doing it in another file at the wrong location.

    You should use a wordpress hook to call the_colour() at the right time.
    This may be the right place to do it
    Code:
    if ( is_admin() ){
    
        add_action('admin_init', 'wptsx2_register_colourswitcher_settings');
        add_action('admin_menu', 'wptsx2_admin_reg');
    
    }
    
    else {
    
        // Nothing. Really.
    
    }
    [COLOR=DeepSkyBlue]
    add_action('wp_head', 'the_colour');[/COLOR]
    
    function wptsx2_admin_reg() {
    
        add_submenu_page('themes.php', 'WPThemestudio X2 Administration', 'WPTS X2', 'administrator', 'wptsx2-admin/admin-page.php', 'wptsx2_admin_page');
    
    }
     
    
     
    • Thanks Thanks x 1
  6. wgn_white

    wgn_white Regular Member

    Joined:
    Oct 12, 2009
    Messages:
    246
    Likes Received:
    27
    @BozoClown

    Yes, I put the add_action in other files called actions.php where all actions were execute.

    I've tried your suggested solution... It still doesn't resolve the issue.

    The warning appear each time I save the form.
    But the form manage to save the value into the database.

    I use register_settings to make my form.
    Ah, I really don't want to use those array method to make my form... :(
     
  7. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    109
    PM me your files, maybe I can see catch the bug that way.
     
  8. wgn_white

    wgn_white Regular Member

    Joined:
    Oct 12, 2009
    Messages:
    246
    Likes Received:
    27
    I've send you the files via PM. :)