How to get Wordpress settings values ​​for customization

I can’t figure out how to get the value - regardless of whether they are set or not - from the checkboxes in the WP Configuration Manager.

This is the code in functions.php:

$wp_customize->add_setting('social_facebook', array(
    'type'       => 'option',
));

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'social_facebook',
        array(
            'label'          => __( 'Facebook', 'theme_name' ),
            'section'        => 'social-icons',
            'settings'       => 'social_facebook',
            'type'           => 'checkbox',
        )
    )
);

And here is how I am trying to get the value:

<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
    .facebook {display:inline!important;}
</style>
<?php }
?>

These flag values ​​have the value "(empty) or" 1 ", so the system logs their check. However, I don’t know how to get the value through the get_theme_mod method. In addition, they do not have name values, so I can not get the value in the usual way.

+4
source share
5 answers
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';

This is an example in my case - if this option is checked, it will echo the “sticky” class in my template.

+4

"my_theme_settings [social_facebook]" :

<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>

 //if setting is unchecked content here will be shown

<?php } // end if ?>

.: http://themefoundation.com/wordpress-theme-customizer

+2

:

function theme_name_custom_settings( $wp_customize ) {

    $wp_customize->add_setting( 'social_facebook', array(
        'default'   => 1, // Set default value
        'sanitize_callback' => 'esc_attr', // Sanitize input
        )
    );

    $wp_customize->add_control( 
        new WP_Customize_Control(
            $wp_customize,
            'social_facebook', // Setting ID
            array(
                'label'     => __( 'Facebook', 'theme_name' ),
                'section'   => 'social_icons', // No hyphen
                'settings'  => 'social_facebook', // Setting ID
                'type'      => 'checkbox',
            )
        )
    );

}

add_action( 'customize_register', 'theme_name_custom_settings' );

(0 1) :

if ( !get_theme_mod( 'social_facebook' ) ) { // false
    return;
}

// or

if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
    return;
}
+1

(, functions.php:

function mytheme_customize_register( $wp_customize ){
  $wp_customize->add_section(
  // ID
  'layout_section',
  // Arguments array
  array(
    'title' => __( 'Layout', 'my_theme' ),
    'capability' => 'edit_theme_options',
    'description' => __( 'social needs ;)', 'my_theme' )
  )
 );

 $wp_customize->add_setting(
  // ID
  'my_theme_settings[social_facebook]',
  // Arguments array
  array('type' => 'option')
  );

 $wp_customize->add_control(
  // ID
  'layout_control',
array(
  'type' => 'checkbox',
  'label' => __( 'Facebook', 'my_theme' ),
  'section' => 'layout_section',
  // This last one must match setting ID from above
  'settings' => 'my_theme_settings[social_facebook]'
 )
 );
}

add_action( 'customize_register', 'mytheme_customize_register' );

$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
0

WP_Customize_Setting:: value(), false ( ), - "0" "".

WP_Customize_Setting value(), .

<?php
class ForceBooleanSettings
extends WP_Customize_Setting {

  public function value() {
    return (bool) parent::value();
  }
}

// Example on using this extend class
$customizer->add_setting(new ForceBooleanSettings(
   $customizer, 
   'myuniquekey', 
   array(
    'default' => false,
    'transport' => 'refresh',
)));

?>
0

Source: https://habr.com/ru/post/1534846/


All Articles