1

Please see the below-mentioned code →

    function klogeto_initialize_theme_options() {
    // Let's introduce a section for the header options
    add_settings_section(
        'header_section',                           // The ID to use for this section in attribute tags
        'Header Options',                           // The title of the section rendered to the screen
        'klogeto_logo_display',                     // The function used to render the options for this section
        'klogeto-theme-header-options'              // The ID of the page on which this section is rendered
    );

    add_settings_field(
    'display_logo',                              // The ID (or the name) of the field
    'Upload a logo',                             // The text used to label the field
    'klogeto_logo_display_field',                // The callback function used to render the field
    'klogeto-theme-header-logo-options',         // The page on which we'll be rendering this field
    'header_section'                             // The section to which we're adding the setting
    );
}

add_action( 'admin_init', 'klogeto_initialize_theme_options' );

But the problem is that the text Header Options is not displaying in the theme options panel.

'Header Options', // The title of the section rendered to the screen

what mistake I am doing? Looks like some silly mistake that I cant diagnose.

I forget to mentioned that I have also written this line of code →

    register_setting(
    'header_section',                   // The name of the group of settings
    'header_options',                   // The name of the actual option (or setting)
    'klogeto_sanitize_header_options'       // The callback to footer sanitization option
);
  • Hi, If you are trying to add a theme option page, i would suggest you use the add_theme_page() instead. It's been introduced just for this purpose. – Jack Johansson Jun 5 '17 at 7:30
  • for now, I am trying to register sections and fields, but that is not populating as I have mentioned above in the question sir. Actually, I was trying to learn through this tutorial – The WP Novice Jun 5 '17 at 7:32
  • These things add_settings_section or dd_settings_field are obsolete or not in use? – The WP Novice Jun 5 '17 at 7:39
  • I don't see any error in your code, however it would be nice to see the entire code you are using to add the option page. – Jack Johansson Jun 5 '17 at 7:40
  • 1
    Sure. I'm trying your code in my localhost and it displays nothing. Please give me a few minutes. – Jack Johansson Jun 5 '17 at 8:09
1

The code you provided lacks a lot of sections and it outputs nothing when i use it on my localhost. I've written and tested a full class for you that does what you are looking for.

// Start Class
if ( ! class_exists( 'my_Theme_Options' ) ) {
    class my_Theme_Options {
        public function __construct() {
            // We only need to register the admin panel on the back-end
            if ( is_admin() ) {
                add_action( 'admin_menu', array( 'my_Theme_Options', 'my_add_admin_menu' ) );
                add_action( 'admin_init', array( 'my_Theme_Options', 'my_register_settings' ) );
            }
        }
        // Returns all theme options 
        public static function get_my_theme_options() {
            return get_option( 'my_theme_options' );
        }
        // Returns single theme option
        public static function my_get_theme_option_value( $id ) {
            $options = self::get_my_theme_options();
            if ( isset( $options[$id] ) ) {
                return $options[$id];
            }
        }
        // Add sub menu page
        public static function my_add_admin_menu() {
            add_options_page(
                esc_html__( 'my Options', 'text-domain' ),
                esc_html__( 'my Options', 'text-domain' ),
                'manage_options',
                'my-theme-settings',
                array( 'my_Theme_Options', 'my_create_admin_page' )
            );
        }
        // Register a setting and its sanitization callback.
        public static function my_register_settings() {
            register_setting( 'my_theme_options', 'my_theme_options', array( 'my_Theme_Options', 'my_sanitize' ) );
        }
        // Sanitization callback
        public static function my_sanitize( $options ) {
            // If we have options lets sanitize them
            if ( $options ) {
                // Input
                if ( ! empty( $options['sample_input'] ) ) {
                    $options['sample_input'] = sanitize_text_field( $options['sample_input'] );
                } else {
                    unset( $options['sample_input'] ); // Remove from options if empty
                }
            }
            // Return sanitized options
            return $options;
        }
        // Now we output our form to save and view our options
        public static function my_create_admin_page() { ?>
            <div class="wrap">
                <h1><?php esc_html_e( 'Theme Options', 'text-domain' ); // The heading for Option page ?></h1>
                <?php settings_errors(); // Output any error if exists ?>
                <form method="post" action="options.php">
                    <?php settings_fields( 'my_theme_options' ); ?>
                    <?php esc_html_e( 'Heading', 'text-domain' ); ?>
                    <table class="form-table">
                        <tbody>
                            <tr>
                                <th scope="row">
                                    <?php esc_html_e( 'Sample Input', 'text-domain' ); ?>
                                </th>
                                <td>
                                    <?php $value = self::my_get_theme_option_value( 'sample_input' ); // We retrieve and output this option in the input area ?>
                                    <input type="text" name="my_theme_options[sample_input]" value="<?php echo esc_attr( $value ); ?>">
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php submit_button(); // Let's have a button to save the form, shall we? ?>
                </form>
            </div>
        <?php }
    }
}
new my_Theme_Options();
// Helper function to use in theme to return a theme option value
function my_get_theme_option( $id = '' ) {
    return my_Theme_Options::my_get_theme_option_value( $id );
}

I used a simple text input as an example, but you can use checkbox, radio buttons, anything you want.

I've added explanation to everywhere i could, however if you have any questions about what does a part of the code do, make sure you let me know about it.

Your Answer

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

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