Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Settings API functions WordPress

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 145
    Comment on it

    Welcome to FindNerd.

    Today we are going to discuss settings api functions and how we can use these functions to add extra field on general settings page. Here we will use three functions. Please have a look.

       add_settings_section()
       add_settings_field()
       register_setting()

     

    I will explain these three functions one by one so let's start with add_settings_section() function.

    add_settings_section()

    This function is useful to create a separate section on setting page. Instead of creating a new separate page, you can simply add a section on setting pages and save these options. Through this, you can simply manage the different settings on a single page. It is easy as well time-saving option.

     

     Syntax  add_settings_section( $id, $title, $callback, $page );
     Return Value  None(void)

     

    Parameter Description
     $id(string)  This is  required one. set the id for the section.
     $title(string)  This is required one. set the title for the section
     $callback(string)  This is required one. Useful to set the content for the section.
     $page(string)  This is also required one and set the setting page on which the section will added. values can be general,reading.media etc.

     

    add_settings_section()

    This function is useful to add a new field on setting page or section which you can create using above function. name attribute of html input field must be same as  register_setting() function. You can check description of this function below.

     

     Syntax  add_settings_field( $id, $title, $callback, $page, $section, $args );
     Return Value  None(void)

     

    Parameter  Description
       $id(string)  This is required one. set the id attribute of the field.
      $title(string)   This is required one. Set the title for the field.
        $callback(string)  This is required one. Callback to fill the field with other inputs. you need to use the echo to print the output.
     $page(string)  This is required one and useful to set the setting page on which the field will be displayed.
     $section(string)  This is optional one. You can pass the section id on which you want to display the field.
     $args(array)  This is optional one. Useful to pass the additional argument to the callback function.

     

    register_setting()

    This is useful to register the setting options and its sanitization callback in the wordPress and make it authorized.

     

    Syntax  register_setting( $option_group, $option_name, $sanitize_callback );
    Return Value  None(void)

     

    Parameter Description
     $option_group(string)  This is required one. Set the settings group name and must match with passed value to settings_fields() function.
     $option_name(string)  This is required one. Set the option name to save.
     $sanitize_callback(callback)  This is optional one. Useful to sanitizes the option's value

     

    function findnerd_box()
    {
    
    add_settings_section(
            'advertise_section',         // section ID 
            'Advertise Options',                  // section Title 
            'advertise_options_callback', // Callback to set the description of the section
            'general'                           // Page on which section will be displayed
        );
    
    add_settings_field( 
            'enable_advertise',                      // field ID 
            'Advertisement',                           //  label to the left side
            'advertise_field_callback',   // callback to render the field
            'general',                          // Page on which field will be displayed
            'advertise_section',         // section name
            array(                              //  arguments to pass to the callback.
                'Enable advertisement section.'
            )
        );
    
     register_setting(
            'general',
            'enable_advertise'
        );
    
    }
    
    
    // section callback
    
    function advertise_options_callback()
    {
    	echo '<p>Select You want to enable advertisement section</p>';
    }
    
    // field callback
     
    function advertise_field_callback($args) {
         
        $field_html = '<input type="checkbox" id="enable_advertise" name="enable_advertise" value="1" ' . checked(1, get_option('enable_advertise'), false) . '/>'; 
         
        
        $field_html .= '<label for="enable_advertise"> '  . $args[0] . '</label>'; 
         
        echo $field_html;
         
    }
    
    add_action('admin_init', 'findnerd_box');

    In above example we tried to put an section with checked box on a general settings page. We used all three functions and mentioned these importance as well. Thank you for being with us.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: