• <?php
      /**
       * mytheme Customizer functionality
       *
       * @package WordPress
       * @subpackage mytheme
       * @since mytheme 1.0
       */
      class mytheme_Customize {
       /**
       * Add postMessage support for site title and description for the Customizer.
       *
       * @since mytheme 1.0
       *
       * @param WP_Customize_Manager $wp_customize Customizer object.
       */
    public static function register ( $wp_customize ) {
    
    // Sidebar Layout
    
    $wp_customize->add_section( 'mythem_sidebar_layout_section' , array(
        'title'         => __( 'Sidebar Layout', 'mythem' ),
        'capability'  => 'edit_theme_options', //Capability needed to tweak
    ) );
    
    $wp_customize->add_setting( 'mythem_cus[sidebar_position]', array(
        'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
        'sanitize_callback' => 'mythem_validate_sidebar_position',
    ) );
    
    $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'mythem_cus[sidebar_position]', array(
        'label'         =>   esc_html__( 'Sidebar Position', 'mythem' ),
        'type'          =>  'radio',
        'choices'       =>  array(
          'left'        => esc_html__( 'left', 'mythem' ),
          'right'       => esc_html__( 'right', 'mythem' ),
          'no-sidebar'  => esc_html__( 'no-sidebar', 'mythem' )
          ),
        'section'       =>  'mythem_sidebar_layout_section',
        'settings'      =>  'mythem_cus[sidebar_position]',
    
    ) ) );
    
    }
      }
    
      function mytheme_mod( $key , $default = false ){
        $mytheme_cus = get_theme_mod('mytheme_cus');
        return isset($mytheme_cus[$key]) && $mytheme_cus[$key] ? $mytheme_cus[$key] : $default;
      }
    
      function mytheme_sidebar_layout() {
    
        $mytheme_sidebar_position = mytheme_mod('sidebar_position');
    
        if($mytheme_sidebar_position === 'left') {
          echo "<style>";
          echo ".site-content .widget-area {float: $mytheme_sidebar_position; overflow: hidden;  width: 30%;}";
          echo ".content-area {float: right; margin: 0 0 0 -30%; width: 100%; }";
          echo ".site-main {margin: 0 0 0 32%;}";
          echo "</style>";
        }
    
        if($mytheme_sidebar_position === 'right') {
          echo "<style>";
          echo ".site-content .widget-area {float: $mytheme_sidebar_position; overflow: hidden; width: 30%;}";
          echo ".content-area {float: left; margin: 0 -30% 0 0; width: 100%; }";
          echo ".site-main {margin: 0 30% 0 0;}";
          echo "</style>";
        }
    
        if($mytheme_sidebar_position === 'no-sidebar') {
          echo "<style>";
          echo ".site-content {width: 100%; float: none;}";
          echo ".site-content .widget-area {display: none;}";
          echo ".site-main {margin: 0;}";
          echo "</style>";
        }
    
      }
    
      add_action( 'wp_head', 'mytheme_sidebar_layout' );
  • The topic ‘Theme Customizer not displaying saved values in wp_head’ is closed to new replies.