0

I try to write my first own theme but currently I struggle with making the options with the Settings API. My input values are just not saved and I don't know why. I read this tutorial from Tom Mcfarlin and tried other tutorials but nothing worked for me.

First I add my own menu point and a sub menu.

(I omit a few functions like 'sanitize_social_options' which are hopefully not important for this problem to keep the code shorter. Explanations are at the end and in the comments)

// adds menu section for the theoretisch theme in the wordpress dashboard
function register_dshboard_menu_section() {
    add_menu_page(
            'theoretisch main menu',        //The text to be displayed in the title tags of the page
            'theoretisch',                  //The text to be used for the menu
            'administrator',                //The capability
            'theoretisch_theme_options',    //The slug name to refer to this menu
            'create_main_menu',             //The function to be called to output the content
            null,                           //The URL to the icon to be used for this menu
            25);                            //The position in the menu order
}
add_action('admin_menu', 'register_dshboard_menu_section');
//displays menu content
function create_main_menu(){
    echo "Admin Page Test";
}


//add submenu section for static page to the main menu
function  register_dshboard_submenu_staticPage(){
    add_submenu_page( 
            'theoretisch_theme_options',        //The slug name for the PARENT menu
            'Static page',                      //The text to be displayed in the title tags of the page
            'Static page',                      //The text to be used for the menu
            'administrator',                    //The capability
            'theoretisch_staticPage_options',   //The slug name to refer to this menu
            'create_static_page_menu');         //The function to be called to output the content
}
add_action('admin_menu', 'register_dshboard_submenu_staticPage');
//displays menu content
function create_static_page_menu(){
    new StaticPage;
}

Then in the StaticPage class (from the staticPage.php I registered) I have this

function __construct() {
        $this->buildFooter();
    }


    function buildFooter() {

        $hidden_field_name = 'sP_submit_hidden';
        // See if the user changed some informations
        // If they did, this hidden field will be set to 'Y'
        // same db reference in all sections -> if in one section something changes it will see it
        if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
            ?>
                    <!--  Put a "settings saved" message on the screen   -->
                    <div class="updated">
                        <p><strong><?php _e('settings saved', 'menu-test' ); ?></strong></p>
                    </div>

            <?php } ?>
        <form name="form3" id="footerform" method="post" action="options.php">
        <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
            <?php
            do_settings_sections( 'theoretisch_staticPage_options' );
            settings_fields( 'footer_section' ); 
            submit_button();
            ?>
        </form>
        <?php
    }
}

Here I used the slug from my sub menu page do_settings_sections( 'theoretisch_staticPage_options' ); and beneath that I wrote my sections.

/**---------------------------------------------------------------------------
 * make social options
 *---------------------------------------------------------------------------**/
    function intialize_social_options() {

        //is used to display the section heading and description.
        add_settings_section(
                'social_section',                   // ID used to identify this section and with which to register options
                'Social Options',                   // Title to be displayed on the administration page
                'social_options_callback',          // Callback used to render the description of the section
                'theoretisch_staticPage_options'    // Page on which to add this section of options
                );

        //is used to display the HTML code of the fields.
        add_settings_field(
                'facebook',                         // ID used to identify the field throughout the theme
                'Facebook',                         // The label to the left of the option interface element
                'socialnet_callback',               // The name of the function responsible for rendering the option interface
                'theoretisch_staticPage_options',   // The page on which this option will be displayed
                'social_section',                   // The name of the section to which this field belongs
                array('facebook')                   // The array of arguments to pass to the callback
                );
    register_setting(
                'footer_section',               // A settings group name.
                'facebook',                     // The name of an option to save.
                'sanitize_social_options'
                );
    }
    add_action( 'admin_init', 'intialize_social_options' );


// description for the social options section
function social_options_callback() {
    echo '<p>Provide the URL to the social networks you\'d like to display.</p>';
}

// callbacks for every social network
function socialnet_callback($args) {

        $url = '';
$socialName = $args[0];
$options = get_option( $socialName );
if( isset( $options ) ) {
    $url = $options;
}

    // Render the output
    echo '<input type="text" id="'. $socialName .'" size="50" name="social_options['. $socialName .']" value="' . $url . '" />';
}

/**---------------------------------------------------------------------------
 * make copyright
 *---------------------------------------------------------------------------**/

function intialize_copyright_options() {

    add_settings_section(
            'copyright_section',                // ID used to identify this section and with which to register options
            'Copyright',                        // Title to be displayed on the administration page
            'copyright_options_callback',       // Callback used to render the description of the section
            'theoretisch_staticPage_options'    // Page on which to add this section of options
            );

    add_settings_field(
            'copyright',
            'Copyright',
            'copyright_callback',
            'theoretisch_staticPage_options',
            'copyright_section'
            );

    register_setting(
            'footer_section',
            'copyright'
            );
    }
    add_action( 'admin_init', 'intialize_copyright_options' );

    // description for the social options section
    function copyright_options_callback() {
        echo '<p>Insert your copyright which will be displayed at the bottom</p>';
    }

function copyright_callback(){

    $options = get_option('copyright'); 
    $copyRightText = '';
    if( isset( $options) ) {
        $copyRightText = $options;
    }
    // Render the output
    echo '<input type="text" id="copyright" size="70" name="copyright_options[copyright]" value="' . $copyRightText . '" />';
}

So I make a group named "footer_section" and pass that to

settings_fields( 'footer_section' );

and later to the

    register_setting(
                'footer_section',               // A settings group name.
                'facebook',                     // The name of an option to save.
                'sanitize_social_options'
    );
    register_setting(
            'footer_section',
            'copyright'
    );

Here I passed the ID of the field I want safe too. Normally it should work now, shouldn't it ?

Everything is well displayed, but my values are not saved. I have few more examples where only one section is saved or where I get the error "options_page not found". In the last case I tried the workaround from here but it didn't help. So I think that's the best code version to work with.

I am really clueless what I make wrong. Do someone has an idea or sees my mistake? Thank you very much.

  • If you are running through a class, callback function need to be call like this : array($this, 'function_name'). add_action should provides an error if you really in a class with this code – Benoti Nov 16 '16 at 10:39
  • In the add_settings_section and in the add_settings_field you mean? – theoretisch Nov 16 '16 at 10:43
  • Never mind this, No, you're not in the class, I don't do it like you, you need to do this when your whole code stand in a class. Apparenty it's not the case – Benoti Nov 16 '16 at 10:55
  • Have you seen wordpress.stackexchange.com/questions/100023/…? – fuxia Nov 16 '16 at 11:27
  • Oh no, but it looks really good! Thank you, I will read it, maybe it helps. – theoretisch Nov 16 '16 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.