web-dev-qa-db-fra.com

Paramètre API Wordpress: le titre de la section rendue ne s'affiche pas

S'il vous plaît voir le code mentionné ci-dessous →

    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' );

Mais le problème est que le texte Options d'en-tête ne s'affiche pas dans le panneau d'options de thème.

'Options d'en-tête', // Le titre de la section affichée à l'écran

quelle erreur je fais? On dirait une erreur stupide que je ne peux pas diagnostiquer.

J'ai oublié de mentionner que j'ai aussi écrit cette ligne de 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
);
1
The WP Novice

Le code que vous avez fourni manque beaucoup de sections et il ne génère rien lorsque je l'utilise sur mon hôte local. J'ai écrit et testé une classe complète pour vous qui fait ce que vous cherchez.

// 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 );
}

J'ai utilisé un exemple de saisie de texte simple, mais vous pouvez utiliser une case à cocher, des boutons radio, tout ce que vous voulez.

J'ai ajouté des explications partout où je pouvais, mais si vous avez des questions sur le fonctionnement d'une partie du code, assurez-vous de me le faire savoir.

1
Jack Johansson