0

IN FUNCTIONS.PHP →

locate_template( 'inc/admin/class.settings-api.php', true );
locate_template( 'inc/admin/o-functions.php', true );

o-functions.php code:

<?php

/**
* Registers settings section and fields
*/
function wedevs_admin_init() {

    $sections = array(
        array(
            'id'    => 'wedevs_basics',
            'title' => __( 'Basic Settings', 'wedevs' )
        ),
        array(
            'id'    => 'wedevs_advanced',
            'title' => __( 'Advanced Settings', 'wedevs' )
        ),
        array(
            'id'    => 'wedevs_others',
            'title' => __( 'Other Settings', 'wpuf' )
        )
    );

    $fields = array(
        'wedevs_basics' => array(
            array(
                'name' => 'text',
                'label' => __('Text Input', 'wedevs'),
                'desc' => __('Text input description', 'wedevs'),
                'type' => 'text',
                'default' => 'Title'
            ),
            array(
                'name' => 'textarea',
                'label' => __('Textarea Input', 'wedevs'),
                'desc' => __('Textarea description', 'wedevs'),
                'type' => 'textarea'
            ),
            array(
                'name' => 'checkbox',
                'label' => __('Checkbox', 'wedevs'),
                'desc' => __('Checkbox Label', 'wedevs'),
                'type' => 'checkbox'
            ),
            array(
                'name' => 'radio',
                'label' => __('Radio Button', 'wedevs'),
                'desc' => __('A radio button', 'wedevs'),
                'type' => 'radio',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            ),
            array(
                'name' => 'multicheck',
                'label' => __('Multile checkbox', 'wedevs'),
                'desc' => __('Multi checkbox description', 'wedevs'),
                'type' => 'multicheck',
                'options' => array(
                    'one' => 'One',
                    'two' => 'Two',
                    'three' => 'Three',
                    'four' => 'Four'
                )
            ),
            array(
                'name' => 'selectbox',
                'label' => __('A Dropdown', 'wedevs'),
                'desc' => __('Dropdown description', 'wedevs'),
                'type' => 'select',
                'default' => 'no',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            )
        ),
        'wedevs_advanced' => array(
            array(
                'name' => 'text',
                'label' => __('Text Input', 'wedevs'),
                'desc' => __('Text input description', 'wedevs'),
                'type' => 'text',
                'default' => 'Title'
            ),
            array(
                'name' => 'textarea',
                'label' => __('Textarea Input', 'wedevs'),
                'desc' => __('Textarea description', 'wedevs'),
                'type' => 'textarea'
            ),
            array(
                'name' => 'checkbox',
                'label' => __('Checkbox', 'wedevs'),
                'desc' => __('Checkbox Label', 'wedevs'),
                'type' => 'checkbox'
            ),
            array(
                'name' => 'radio',
                'label' => __('Radio Button', 'wedevs'),
                'desc' => __('A radio button', 'wedevs'),
                'type' => 'radio',
                'default' => 'no',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            ),
            array(
                'name' => 'multicheck',
                'label' => __('Multile checkbox', 'wedevs'),
                'desc' => __('Multi checkbox description', 'wedevs'),
                'type' => 'multicheck',
                'default' => array('one' => 'one', 'four' => 'four'),
                'options' => array(
                    'one' => 'One',
                    'two' => 'Two',
                    'three' => 'Three',
                    'four' => 'Four'
                )
            ),
            array(
                'name' => 'selectbox',
                'label' => __('A Dropdown', 'wedevs'),
                'desc' => __('Dropdown description', 'wedevs'),
                'type' => 'select',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            )
        ),
        'wedevs_others' => array(
            array(
                'name' => 'text',
                'label' => __('Text Input', 'wedevs'),
                'desc' => __('Text input description', 'wedevs'),
                'type' => 'text',
                'default' => 'Title'
            ),
            array(
                'name' => 'textarea',
                'label' => __('Textarea Input', 'wedevs'),
                'desc' => __('Textarea description', 'wedevs'),
                'type' => 'textarea'
            ),
            array(
                'name' => 'checkbox',
                'label' => __('Checkbox', 'wedevs'),
                'desc' => __('Checkbox Label', 'wedevs'),
                'type' => 'checkbox'
            ),
            array(
                'name' => 'radio',
                'label' => __('Radio Button', 'wedevs'),
                'desc' => __('A radio button', 'wedevs'),
                'type' => 'radio',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            ),
            array(
                'name' => 'multicheck',
                'label' => __('Multile checkbox', 'wedevs'),
                'desc' => __('Multi checkbox description', 'wedevs'),
                'type' => 'multicheck',
                'options' => array(
                    'one' => 'One',
                    'two' => 'Two',
                    'three' => 'Three',
                    'four' => 'Four'
                )
            ),
            array(
                'name' => 'selectbox',
                'label' => __('A Dropdown', 'wedevs'),
                'desc' => __('Dropdown description', 'wedevs'),
                'type' => 'select',
                'options' => array(
                    'yes' => 'Yes',
                    'no' => 'No'
                )
            )
        )
    );

    $settings_api = new WeDevs_Settings_API();

    //set sections and fields
    $settings_api->set_sections( $sections );
    $settings_api->set_fields( $fields );

    //initialize them
    $settings_api->admin_init();
}

add_action( 'admin_init', 'wedevs_admin_init' );


/**
* Register the plugin page
*/
function wedevs_admin_menu() {
    add_options_page( 'Settings API', 'Settings API', 'delete_posts', 'settings_api_test', 'wedevs_plugin_page' );
}

add_action( 'admin_menu', 'wedevs_admin_menu' );


/**
* Display the plugin settings options page
*/
function wedevs_plugin_page() {
    $settings_api = new WeDevs_Settings_API();

    echo '<div class="wrap">';
    settings_errors();

    $settings_api->show_navigation();
    $settings_api->show_forms();

    echo '</div>';
}

/**
* Get the value of a settings field
*
* @param string $option settings field name
* @param string $section the section name this field belongs to
* @param string $default default text if it's not found
* @return mixed
*/
function my_get_option( $option, $section, $default = '' ) {

    $options = get_option( $section );

    if ( isset( $options[$option] ) ) {
    return $options[$option];
    }

    return $default;
}

Code for class.settings-api.php

But I think sub-menus are not registering thats why nothing is populating in the backend. see here →enter image description here

whats the Fix?

1
  • I will be obliged If some one can help me to fix the subtle error. I am feeling paralyzed because of this obstacle in my theme development.
    – WordCent
    Commented Sep 1, 2017 at 15:34

1 Answer 1

1

I don't know anything about the WeDevs thing, but I do kinda know the Settings API itself.

That being said I have a shot in the dark answer. In your wedevs_plugin_page() function, do the methods on the $settings_api object need a parameter for which settings section to display? Like $settings_api->show_navigation('wedevs_basic');?

Or maybe the object instantiation itself takes a parameter? Like $settings_api = new WeDevs_Settings_API('wedevs_basic');?

Again, I don't know the WeDevs framework, but it seems odd that the WeDevs_Settings_API object you create during the admin_init function would be able to later communicate with another WeDevs_Settings_API object created in the options page function. Unless $settings_api->admin_init(); does something really cool to facilitate that communication, it just seems like something is missing here.

EDIT:

OK I think you're not really supposed to use that Settings API plugin (as in don't activate it), but rather use it as an example for your own code in your own theme. I guess that's what the author means by "THIS PLUGIN IS FOR DEVELOPERS, NOT GENERAL USERS."

Anyway, take the class.settings-api.php file from the plugin and include that in your theme (which I think is what you're doing already). Don't change anything in this file.

Then in your o-functions.php file, just get rid of all that procedural stuff and use the oop-example.php file instead. Like copy the code from that file and paste it in your o-functions.php file.

Now edit that o-functions.php file to suit your needs. First thing you should do is change the name of the class from 'WeDevs_Settings_API_Test' to something more descriptive for your theme. Don't forget to change the class name in the conditional as well - if ( !class_exists('WeDevs_Settings_API_Test' ) ):

Then change the parameters for add_options_page( 'Settings API', 'Settings API', 'delete_posts', 'settings_api_test', array($this, 'plugin_page') );

You will want to change the first, second, and fourth parameters which are the page title, menu title, and menu slug respectively. The slug especially has to be unique.

Then you can change the sections and the fields to whatever you need.

So a quick recap - once you copy the code from oop-example.php, you will need to change the name of the class, change the parameters for add_options_page(), change the section names in function get_settings_sections(), and change the field data in function get_settings_fields(). Don't touch anything else in this code.

Now to finish it off instantiate an object using your new class after where you're using locate_template(), like this:

locate_template( 'class.settings-api.php', true );
locate_template( 'o-functions.php', true );
new My_Settings_API();

Note that when I renamed the class in o-functions.php, I called it "My_Settings_API". So you need to use whatever name you gave it.

4
  • Ok I looked a bit at that plugin, and I ran into the same issue you did. I get that blank screen too. However I can see a couple problems when using this plugin. For one, it loads that example "Settings API" page by default, so when you run that example code you have in o-functions.php (the "procdedural example") it conflicts with this default page that the plugin is trying to make. Looking even further into it, I'm not sure this is even how you're supposed to use this plugin. I think what you are supposed to do is copy the oop-example.php file and customize a new class based on that...
    – James
    Commented Sep 1, 2017 at 19:37
  • ... so that way it's loading a different class, with your own custom settings defined within it. All that being said, instead of using this plugin you could try generating your settings page with wpsettingsapi.jeroensormani.com. I've used this recently and it works pretty well. Fill in a couple naming fields, choose the fields you want, and it will give you all the code you need to copy/paste into your theme.
    – James
    Commented Sep 1, 2017 at 19:40
  • 1
    I have used it in projects, but only for simple text fields. Otherwise I use Advanced Custom Fields, which is the best thing that ever happened to WordPress development. Although I figured out how to use that plugin, I'll edit my answer to explain better.
    – James
    Commented Sep 1, 2017 at 20:10
  • sir, if you can look into this related and relevant question → wordpress.stackexchange.com/questions/278941/…
    – WordCent
    Commented Sep 3, 2017 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.