2

Hello the first thing I'm sorry for my poor english level.

I was looking some tutorial to use the customizer on wordpress for a theme from scratch. But I have some issues with get_option, I have no return. I put my code right bellow.

<?php
//==================================================
//============= Chargement des scripts =============
//==================================================

define('schweitzer_ver', '0.2');


// Chargement front end
function schweitzer_scripts(){

    // Chargement des styles
    //wp_enqueue_style( 'styles-bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css',
    //  '', schweitzer_ver, 'all' );
    wp_enqueue_style( 'styles', get_template_directory_uri() . '/style.css',
        array(), schweitzer_ver, 'all' );
    wp_enqueue_style( 'menu', get_template_directory_uri() . '/css/menu.css',
        array(), schweitzer_ver, 'all' );


    // Chargement des scripts
    wp_enqueue_script( 'schweitzer_script', get_template_directory_uri() . '/js/schweitzer.js',
        array('jquery'), schweitzer_ver, true );
    wp_enqueue_script( 'menu_script', get_template_directory_uri() . '/js/menu.js',
        array('jquery'), schweitzer_ver, true );
    }

    // Intégration et mise en forme du menu
    function clean_custom_menus() {
        $menu_name = 'primary';
        if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
            $menu = wp_get_nav_menu_object($locations[$menu_name]);
            $menu_items = wp_get_nav_menu_items($menu->term_id);
            // $menu_list = '<nav>' ."\n";
            $menu_list .= "\t\t\t\t". '<ul>' ."\n";

        foreach ((array) $menu_items as $key => $menu_item) {
            $title = $menu_item->title;
            $url = $menu_item->url;
            $menu_list .= "\t\t\t\t\t". '<li><a href="'. $url .'">'. $title .'</a></li>' ."\n";
            }

        $menu_list .= "\t\t\t\t". '</ul>' ."\n";
        $menu_list .= "\t\t\t". '</nav>' ."\n";
        }

        else {
        $menu_list = 'Pas de menu défini';
        }
    echo $menu_list;
}



add_action( 'wp_enqueue_scripts', 'schweitzer_scripts', 'clean_custom_menus');


//==================================================
//================= Configuration ==================
//==================================================

function schweitzer_setup ()
{

    // Active gestion des menus (avec plusieurs positions)
    register_nav_menus(array('primary'=>'principal'));

    // support des vignettes
    add_theme_support('post-thumbnails');

    // Retire générateur de version (sécurité !)
    remove_action('wp_head', 'wp_generator');

    // Retire guillemets français
    remove_filter ('the_content', 'wptexturize');

    // Support du titre géré par WP (meilleur SEO)
    add_theme_support('title-tag');

}

    add_action( 'after_setup_theme', 'schweitzer_setup' );

//==================================================
//================ Options du theme ================
//==================================================

function schweitzer_customizer()
{

    //===== Variables =====
    // Couleur du menu
    $menu_color = get_option('menu_color','#2a2a2a');

    // Transparence du menu
    $menu_opacity = get_option('menu_opacity','0.5');

    // Nombre d'éléments dans le menu
    // $menu_element = get_option( 'menu_element' );

    ?>
    <style>
        nav li:nth-of-type(1){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(2){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(3){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(4){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(5){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
    </style>
    <?php

}
    add_action( 'wp_head', 'schweitzer_customizer' );




function schweitzer_customize_register( $wp_customize ) {

        $wp_customize->add_section( 'menu_options' , array(
        'title' =>  'Réglage du Menu',
    ) );

    $wp_customize->add_setting( 'menu_color' , array(
        'default' => '#000',
        'sanitize_callback' => 'sanitize_hex_color',
    ) );

    $wp_customize->add_setting( 'menu_opacity' , array(
        'default' => '0.5',
    ) );

    $wp_customize->add_control(
        new WP_Customize_Color_Control($wp_customize,
            'menu_color_selection',
            array('label' => 'Choix de la couleur',
                'section' => 'menu_options',
                'settings' => 'menu_color',
        )
    ));

    $wp_customize->add_control(
        'menu_opacity_selection',
        array(
            'label'    => __( 'Transparence', 'schweitzer' ),
            'section'  => 'menu_options',
            'settings' => 'menu_opacity',
            'type'     => 'radio',
            'choices'  => array(
                '0'  => '0% (opaque)',
                '0.1' => '10 %',
                '0.2'  => '20 %',
                '0.3' => '30 %',
                '0.4'  => '40 %',
                '0.5' => '50 %',
                '0.6'  => '60 %',
                '0.7' => '70 %',
                '0.8' => '80 %',
                '0.9'  => '90 %',
                '1' => '100 % (transparent)',),
        )
    );
}
add_action( 'customize_register', 'schweitzer_customize_register' );

function schweitzer_register_settings() {
    register_setting( 'menu_settings', 'my_option_name', 'intval' );
}

add_action( 'admin_init', 'schweitzer_register_settings' );

This is my functions.php. The back end seems to work well but there is no trace of my "menu_color" options. Wp_head is in the index.php.

If you can help I will be very thanksfull.

1 Answer 1

1

I have found the solution on another forum but if someone has the same problem than me here's the solution :

For theme options modified through the $wp_customize, use get_theme_mod() instead of get_option() to retrieve the value.

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.