wordpress customizer get_theme_mod no output - php

i am working on a theme for wordpress right now to see how it works etc. But now i want to implement some customizer settings/controls. So here is what i tried
function myfirsttheme_customizer_register($wp_customize){
$wp_customize->add_section('mycustomtheme_colors', array(
'title' => __('Colors','mycustomtheme'),
'description' => 'Modify the theme colors'
));
$wp_customize->add_setting('background_color', array(
'default' => '#fff',
));
$wp_customize->add_setting('link_color', array(
'default' => '#4b4b4',
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'background_color', array(
'label' => __('Edit Background Color', 'mycustomtheme'),
'section' => 'mycustomtheme_colors',
'settings' => 'background_color'
) ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-color', array(
'label' => __('Edit Link Color', 'mycustomtheme'),
'section' => 'mycustomtheme_colors',
'settings' => 'link_color'
) ));
}
add_action('wp_head','mycustomtheme_css_customizer');
add_action('customize_register','myfirsttheme_customizer_register');
and the HTML/CSS here
function mycustomtheme_css_customizer(){
?>
<style type="text/css">
article { background-color:<?php echo get_theme_mod('background_color');?> ; }
</style>
<?php
}
So when i change the line echo get_theme_mod('background_color'); with an actual color like #fff it works fine, but for some reason the get_theme_mod doesn't give an output and i can't understand why not.

add these line in your setting array
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
following code will work
function myfirsttheme_customizer_register($wp_customize){
$wp_customize->add_section('mycustomtheme_colors', array(
'title' => __('Colors','mycustomtheme'),
'description' => 'Modify the theme colors'
));
$wp_customize->add_setting('background_color', array(
'default' => '#fff',
'type' => 'theme_mod',
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('link_color', array(
'default' => '#4b4b4',
'type' => 'theme_mod',
'capability' => 'edit_theme_options'
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'background_color', array(
'label' => __('Edit Background Color', 'mycustomtheme'),
'section' => 'mycustomtheme_colors',
'settings' => 'background_color'
) ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-color', array(
'label' => __('Edit Link Color', 'mycustomtheme'),
'section' => 'mycustomtheme_colors',
'settings' => 'link_color'
) ));
}
add_action('customize_register','myfirsttheme_customizer_register');

Related

Storefront overwrite customize register?

I am creating a child theme using the storefront as a parent theme so far I am doing well, I am trying to create a website where I can change the basic aspects of the style of the website like background-color, custom text in wp-admin/customize.php.
I know I can add more options using customize_register , but the storefront theme has its own action hook for that, in fact I already added my own action hook to enable more options see below
as you can see the problem is that it creates another "footer" option, is there any way to overwrite or insert custom options, within the existing "footer" option of the parent theme?
here the code im using to insert options
function footer_customize_register( $custom_vars ) {
$custom_vars ->add_section(
'layout_section',
array(
'title' => __( 'Footer', 'pre' ),
'capability' => 'edit_theme_options',
'description' => __( 'Allows you to edit your theme layout.', 'pre' ),
'priority' => 25,
)
);
$custom_vars -> add_setting('pre_layout_options[address_text]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '000 7th St NW',
));
$custom_vars -> add_control('pre_layout_options[address_text]', array(
'label' => 'adress',
'section' => 'layout_section',
'type' => 'text',
));
$custom_vars -> add_setting('pre_layout_options[phone_text]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '01234-567',
));
$custom_vars -> add_control('pre_layout_options[phone_text]', array(
'label' => 'ZIP code',
'section' => 'layout_section',
'type' => 'text',
));
$custom_vars -> add_setting('pre_layout_options[VAT]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '00.000.000/0000-00',
));
$custom_vars -> add_control('pre_layout_options[VAT]', array(
'label' => 'VAT number',
'section' => 'layout_section',
'type' => 'text',
));
$custom_vars -> add_setting('pre_layout_options[Location]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => 'Centro',
));
$custom_vars -> add_control('pre_layout_options[Location]', array(
'label' => 'Location',
'section' => 'layout_section',
'type' => 'text',
));
}
add_action('customize_register', 'footer_customize_register');
the source code is from here
in section you need to add storefront_footer and add priority to change position. check below code.
function footer_customize_register( $custom_vars ) {
$custom_vars -> add_setting('pre_layout_options[address_text]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '000 7th St NW',
));
$custom_vars -> add_control('pre_layout_options[address_text]', array(
'label' => 'adress',
'section' => 'storefront_footer',
'type' => 'text',
'priority' => 50,
));
$custom_vars -> add_setting('pre_layout_options[phone_text]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '01234-567',
));
$custom_vars -> add_control('pre_layout_options[phone_text]', array(
'label' => 'ZIP code',
'section' => 'storefront_footer',
'type' => 'text',
'priority' => 60,
));
$custom_vars -> add_setting('pre_layout_options[VAT]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => '00.000.000/0000-00',
));
$custom_vars -> add_control('pre_layout_options[VAT]', array(
'label' => 'VAT number',
'section' => 'storefront_footer',
'type' => 'text',
'priority' => 70,
));
$custom_vars -> add_setting('pre_layout_options[Location]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
'default' => 'Centro',
));
$custom_vars -> add_control('pre_layout_options[Location]', array(
'label' => 'Location',
'section' => 'storefront_footer',
'type' => 'text',
'priority' => 80,
));
}
add_action('customize_register', 'footer_customize_register');
Tested and works.

Adding Row settings to WPBakery plugin

How do I get the settings to the frontend? I need to add classes to vc_row.
/**
* Ken Burns Effect for Row.
*/
add_action( 'vc_after_init', 'ken_burns_effect_add_option_to_vc_row' );
function ken_burns_effect_add_option_to_vc_row() {
// Ken Burns Effect Attributes
$ken_burns_effect_attributes =
array(
array(
'type' => 'checkbox',
'heading' => __( 'Ken Burns for image background', 'ken_burns_effect' ),
'param_name' => 'enable_ken_burns_effect',
'value' => array(
__( 'Yes', 'ken_burns_effect' ) => 'yes',
),
'description' => 'Check this box if you want to enable ken burns effect for this row.',
),
array(
'type' => 'dropdown',
'heading' => __( 'Direction', 'ken_burns_effect' ),
'param_name' => 'direction_ken_burns_effect',
'value' => array(
'Zoom In' => 'zoom_in',
'Zoom Out' => 'zoom_out',
),
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
array(
'type' => 'textfield',
'heading' => __( 'Transition speed', 'ken_burns_effect' ),
'param_name' => 'transition_speed_ken_burns_effect',
'value' => '',
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
);
vc_add_params( 'vc_row', $ken_burns_effect_attributes);
}
You will have to override the vc_row.php by copying it to your theme or plugin.
You can check their documentation on how to set the override directory: https://kb.wpbakery.com/docs/inner-api/vc_set_shortcodes_templates_dir/
Then you can use your custom params inside vc_row.php using their param_name eg:
$enable_ken_burns_effect;
$direction_ken_burns_effect;
$transition_speed_ken_burns_effect;
Keep in mind that you have to override vc_row_inner.php too if you want to use options there. Same goes for section and columns.

WordPress: How to use active callbacks in the Customizer

In my theme, I set up 2 options use Theme Customization API, the code snippet below.
I want to display the radio option when the checkbox is true, when the checkbox is false, the radio hidden. I try to use active_callbackļ¼Œbut not working. So, How can achieve this function?
Thanks!
// Related Post.
$wp_customize->add_setting('_related_post', array(
'capability' => 'edit_theme_options',
'default' => 0,
'transport' => 'postMessage',
));
$wp_customize->add_control('_related_post', array(
'settings' => '_related_post',
'label' => __('Display Related Posts', 'typenow'),
'section' => '_theme_options',
'type' => 'checkbox',
'priority' => 30,
));
// Related Post Num.
$wp_customize->add_setting('_related_post_num', array(
'capability' => 'edit_theme_options',
'default' => '2',
'transport' => 'postMessage',
));
$wp_customize->add_control('_related_post_num', array(
'settings' => '_related_post_num',
'label' => __('Related Posts Number', 'typenow'),
'section' => '_theme_options',
'type' => 'radio',
'priority' => 35,
'choices' => array (
'2' => __('Two posts', 'typenow'),
'4' => __('Four posts', 'typenow'),
),
));
The solution:
$wp_customize->add_control('_related_post_num', array(
'settings' => '_related_post_num',
'label' => __('Related Posts Number', 'typenow'),
'section' => '_theme_options',
'type' => 'radio',
'priority' => 35,
'choices' => array (
'2' => __('Two posts', 'typenow'),
'4' => __('Four posts', 'typenow'),
),
'active_callback' => function(){
return get_theme_mod( '_related_post', false );
},
));

Get data from custom customize field

I've setup new fields below, there's two types for images and text how can I call it now on a php file I'm assuming there's a function for it.
I'm able to access the fields on the theme > customize and add data to it but now I want to paste on a template.
$wp_customize->add_section('travel_video', array(
'priority' => 16,
'capability' => 'edit_theme_options',
'title' => __(' - Featured Videos', 'travel-lite'),
'description' => ''
));
$wp_customize->add_setting('travel[fpvideo]', array(
'default' => __('Title 1', 'travel-lite'),
'capability' => 'edit_theme_options',
'sanitize_callback' => 'esc_textarea',
'type' => 'option'
));
$wp_customize->add_control('travel_fpvideo' , array(
'label' => __('Title 1', 'travel-lite'),
'section' => 'travel_video',
'settings' => 'travel[fpvideo]'
));
Try This code,you can look for reference customizer
function mytheme_customize_register($wp_customize) {
$wp_customize->add_section('travel_video', array(
'priority' => 16,
'capability' => 'edit_theme_options',
'title' => __(' - Featured Videos', 'travel-lite'),
'description' => ''
));
$wp_customize->add_setting('travel[fpvideo]', array(
'default' => __('Title 1', 'travel-lite'),
'capability' => 'edit_theme_options',
'sanitize_callback' => 'esc_textarea',
'type' => 'option'
));
$wp_customize->add_control('travel_fpvideo', array(
'label' => __('Title 1', 'travel-lite'),
'section' => 'travel_video',
'settings' => 'travel[fpvideo]'
));
}
add_action('customize_register', 'mytheme_customize_register');

wordpress customizer default settings not working

I'm working on my first theme for wordpress and I am running in to some problems.
I was busy building a customizer page for the theme.
It went great up until the customizer wouldn't parse the default settings to the css.
I've tried building the functions file all over again but nothing worked.
Could you guys take a look t the code and tell me if there is anything wrong, or if anyone else had this problem tell me what the solution is.
here is the code of my funtions.php file
<?php
add_theme_support( 'menus' );
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'header-menu' => 'main'
)
);
}
add_action( 'init', 'register_my_menus' );
add_theme_support( 'post-thumbnails' );
function claboxico_customize_register( $wp_customize ) {
//fonts array
$googlefonts = array(
'arial'=>'Arial',
'verdana'=>'Verdana, Geneva',
'trebuchet'=>'Trebuchet',
'trebuchet ms'=>'Trebuchet MS',
//this list contains way more fonts
);
//general panel
$wp_customize->add_panel( 'colors', array(
'title' => __( 'Colors' ),
'description' => 'Edit the general styling',
) );
//general colors section
$wp_customize->add_section('primairy_color', array(
'title' => __('Primairy Colors', 'claboxico'),
'panel' => 'colors',
'description' => 'Edit the primairy color of the theme.'
));
//primairy color setting
$wp_customize->add_setting('primairy_color', array(
'default' => '#43bfd8',
));
//primairy color control
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'primairy_color', array(
'label' => __('Primairy Color', 'claboxico'),
'section' => 'primairy_color',
'setting' => 'primairy_color',
) ));
//link kleur
$wp_customize->add_section('link_color', array(
'title' => __('Link Colors', 'claboxico'),
'panel' => 'colors',
'description' => 'Edit the color of the links.'
));
$wp_customize->add_setting('link_color', array(
'default' => '#0eb1ed',
));
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'link_color', array(
'label' => __('Link Color', 'claboxico'),
'section' => 'link_color',
'setting' => 'link_color',
) ));
//plain text color
$wp_customize->add_section('paragraph_color', array(
'title' => __('Paragraph Colors', 'claboxico'),
'panel' => 'colors',
'description' => 'Edit the paragraph text color.'
));
$wp_customize->add_setting('paragraph_color', array(
'default' => '#000',
));
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'paragraph_color', array(
'label' => __('Link Color', 'claboxico'),
'section' => 'paragraph_color',
'setting' => 'paragraph_color',
) ));
//fonts panel
$wp_customize->add_panel( 'fonts', array(
'title' => __( 'Fonts' ),
'description' => 'Edit the fonts',
) );
//heading fonts section
$wp_customize->add_section('heading_font', array(
'title' => __('Heading fonts', 'claboxico'),
'panel' => 'fonts',
'description' => 'Edit the font for h1, h2 etc.'
));
$wp_customize->add_setting('heading_font', array(
'default' => 'Rokkitt',
));
$wp_customize->add_control( 'heading_font',array(
'type' => 'select',
'label' => __('Heading font', 'claboxico'),
'section' => 'heading_font',
'setting' => '',
'choices' => $googlefonts,
)
);
}
function claboxico_css_customizer() {
?>
<style type="text/css">
#import url(http://fonts.googleapis.com/css?family=<?php
$font_sizes = ':300,400,700,900|';
$heading_google_font = get_theme_mod('heading_font');
$heading_font = str_replace(' ', '+', $heading_google_font);
echo $heading_font;
echo $font_sizes;
$paragraph_google_font = get_theme_mod('paragraph_font');
$paragraph_font = str_replace(' ', '+', $paragraph_google_font);
echo $paragraph_font;
echo $font_sizes;
?>);
body{font-family: '<?php echo get_theme_mod('paragraph_font'); ?>'; color: <?php echo get_theme_mod('paragraph_color'); ?> }
/* background color */
.header{background-color: <?php echo get_theme_mod('header_background_color'); ?> ;}
/* heading colors and font */
h1, h2, h3, header .logo{color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ; font-family:'<?php echo get_theme_mod('heading_font'); ?>';}
/*link colors*/
a, a:hover{color: <?php echo get_theme_mod('link_color');?>;}
/* primairy colors */
header{border-bottom-color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}
header .mobile-header-icon, header nav ul li a{color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}
footer{border-top-color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}
</style>
<?php
}
add_action('wp_head', 'claboxico_css_customizer');
add_action( 'customize_register', 'claboxico_customize_register' );
?>
I hope someone can help me with this problem.
Function get_theme_mod can take two parameters.
$name (required) = name of setting
$default (optional) = default value if there is no value set in the database
Check Codex page: https://codex.wordpress.org/Function_Reference/get_theme_mod
Example:
$color = get_theme_mod( 'link_color', '#ff0000' );
In this example default value of #ff0000 is passed. If value is set then that saved value will returned otherwise $color will get default value #ff0000.

Categories