Print this page
Thursday, 26 December 2013 15:43

how to add and save options in wordpress admin setting page

Written by 
Rate this item
(0 votes)
If you want to save some options(settings) from wordpress
admin side setting page to make available those options
for your plugin then you are at the right place.At first add
menu page this way -

add_action( 'admin_menu', 'plugin_settings' );


function plugin_settings() {  
   add_menu_page('plugin settings', 'my plugin', 'administrator', 'plugin_settings', 'display_settings');
}

function display_settings()
{
 
 if (!current_user_can( 'manage_options' ) )  {
    wp_die( __(
     'You do not have sufficient permissions to access this page.'
    ) );
 }
 
 $options_follow = get_option('ms_fbwall_plugin_follow_settings');    
 if((isset($_REQUEST["msfb_active_tab"]))&&($_REQUEST["msfb_active_tab"] == "4"))
    {                   
       if((isset($_REQUEST["msfb_follow_setting"]))&&($_REQUEST["msfb_follow_setting"]=="Y")) {
       
       $msfb_followwidth=$_REQUEST["msfb_followwidth"]; $options_follow['msfb_followwidth']= $msfb_followwidth;
       $msfb_followid=$_REQUEST["msfb_followid"]; $options_follow['msfb_followid']= $msfb_followid;       
                                                                                                                                                                        
       update_option( 'ms_fbwall_plugin_follow_settings', $options_follow );
       }
              
    }                     
 $msfb_followwidth= ($options_follow['msfb_followwidth'] != '') ? $options_follow['msfb_followwidth'] : '480';
 $msfb_followid= ($options_follow['msfb_followid'] != '') ? $options_follow['msfb_followid'] : 'mridulcs';
    
 <form method="post" name="social_options" action="" class="form-horizontal">  
    <fieldset>  
      <legend>follow button settings</legend>
      <div class="control-group">  
        <label class="control-label" for="msfb_followid">follow button id</label>  
        <div class="controls">  
        <input type="text" class="input-xlarge" name="msfb_followid" value="<?php echo esc_attr_e($msfb_followid); ?>" id="msfb_followid" />
        </div>  
      </div>
      <div class="control-group">  
        <label class="control-label" for="msfb_followwidth">follow button width</label>  
        <div class="controls">  
        <input type="text" class="input-xlarge" name="msfb_followwidth" value="<?php echo esc_attr_e($msfb_followwidth); ?>" id="msfb_followwidth" />
        </div>  
      </div>  <br/>                                                                                              
      <div class="form-actions">
       <input type="hidden" name="msfb_active_tab" value="4" />    
       <input type="hidden" name="msfb_follow_setting" value="Y" />                                                                                            
        <input type="submit" name="submit" class="btn btn-primary" value="Update"/>    
      </div>  
    </fieldset>  
 </form>
 
}
code details: in the above code summarization, it's apparently clear that
at first i get saved option array at $options_follow, then i check if the
options form(shown below) is submitted,if yes i again update options by
update_option. Proceeding further along the codes, i populated form values
with the saved option values.
Read 2426 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
7