Selective refresh in the customizer css value does not work - css

I'm experiencing difficulties with a theme I developed.
I'm using Envato, where I cannot enter CSS anywhere other than wp_add_inline_style(); or enqueue style.
I use preview customization directly and add partial refresh.
I have a settings menu with the following options:
choice of layout A or B
choice of layout background color
The problem is that the layout changes when the background color is changed.
Does anyone have a solution?
Code snippet: customizer.php
function customizer_nav( $wp_customize ) {
// Section
$wp_customize->add_section( 'navigasi_sett',
array(
'title' => __( 'Navigasi Primary', 'mocita' ),
'priority' => 100,
'panel' => 'primary_panel',
)
);
// Background
$wp_customize->add_setting( 'nav_background',
array(
'default' => '#0663cc',
'transport' => 'postMessage',
'sanitize_callback' => 'sanitize_hex_color',
)
);
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'nav_background',
array(
'label' => __( 'Background Menus', 'mocita' ),
'section' => 'navigasi_sett',
'settings' => 'nav_background',
)
));
// Navigasi layout
$wp_customize->add_setting( 'nav_layout',
array(
'default' => 'tes1',
'transport' => 'postMessage',
'sanitize_callback' => 'mocita_sanitize_radio_img',
)
);
$wp_customize->add_control( new Mocita_Image_Radio( $wp_customize, 'nav_layout',
array(
'label' => __( 'Navigasi Layout', 'mocita' ),
'section' => 'navigasi_sett',
'choices' => array(
'tes1' => array(
'image' => trailingslashit( get_template_directory_uri() ) . 'images/navigasi/tes1.png',
'name' => __( 'tes1', 'mocita' )
),
'tes2' => array(
'image' => trailingslashit( get_template_directory_uri() ) . 'images/navigasi/tes2.png',
'name' => __( 'tes2', 'mocita' )
)
)
)
));
}
// selevtive_refresh
if ( isset( $wp_customize->selective_refresh ) ) {
// Navigasi Layout
$wp_customize->selective_refresh->add_partial( 'nav_layout',
array(
'selector' => '#primary',
'container_inclusive' => false,
'render_callback' => 'nav_layout',
)
);
}
// callback nav_layout
function nav_layout() {
$layout = get_theme_mod( 'nav_layout', 'nav1' );
if ( $layout === 'nav1' ) {
get_template_part( 'template-parts/navigasi/navigasi', 'primary1' );
} elseif ( $layout === 'nav2' ) {
get_template_part( 'template-parts/navigasi/navigasi', 'primary2' );
}
}
?>
customize-preview.js
(function( $ ) {
// ( Navigasi ) Background
wp.customize( 'nav_background', function( value ) {
value.bind( function( to ) {
$( '#menu' ).css( 'background', to );
});
});
} )( jQuery );

Related

Can't add a section to a Woocommerce Custom Setting Tab [duplicate]

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?
1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

Hide/display a link in BuddyPress toolbar based on user role

How do I hide/display a link in BuddyPress toolbar if the user has a particular role?
function your_bp_admin_bar_add() {
global $wp_admin_bar, $bp;
if ( !bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) )
return;
$user_domain = bp_loggedin_user_domain();
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
}
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}
add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_add', 300 );
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor. It is supposed to display Create Course.
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor.
Are you sure that the role / capability for instructors is lp_teacher?
If you want to only show Create Course to instructors, then try adding an else:
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
} else {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}

How can I render Nested elements on Visual Composer (WPBakery) for wordpress?

I'm trying to do some custom elements for Visual Composer plugin (WP-Bakery) for wordpress.
I have no problem with simple custom elements, but I'm trying to do some nested elements (a parent containing some child elements). I have no problems creating child elements, and if I create them alome, they are shown on wordpress, but when I try to create parent element, I can see setting elements without problem, but it's no rendered.
I think the problem is the render function (html) on parent class, but I can't get solve it.
PARENT CLASS
<?php
class vcInfoCardContainer extends WPBakeryShortCodesContainer {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocardcontainer_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocardcontainer_html' ) );
}
// Element Mapping
public function vc_infocardcontainer_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card Container', 'ex5_vc_elements'),
'base' => 'vc_infocardcontainer',
'description' => __('Info Card Container for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_parent' => array('only' => 'vc_infocard'),
'is_container' => true,
'js_view' => 'VcColumnView',
'params' => array(
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
)
),
));
}
//render
public function vc_infocard_html( $atts, $content = null ) {
// Params extraction
extract(
shortcode_atts(
array(
),
$atts
)
);
$html = '<div class="ex5-vc-info-card-container">' . do_shortcode($content) . '</div>';
return $html;
}
}
new vcInfoCardContainer();
CHILD CLASS
<?php
class vcInfoCard extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocard_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocard_html' ) );
}
// Element Mapping
public function vc_infocard_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card', 'ex5_vc_elements'),
'base' => 'vc_infocard',
'description' => __('Info Card for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_child' => array('only' => 'vc_infocardcontainer'),
'params' => array(
array(
'type' => 'attach_image',
'heading' => __( 'Main image', 'ex5_vc_elements' ),
'param_name' => 'image',
'group' => 'Images',
),
array(
'type' => 'attach_image',
'heading' => __( 'Icon', 'ex5_vc_elements' ),
'param_name' => 'icon',
'group' => 'Images',
),
array(
'type' => 'colorpicker',
'heading' => __( 'Icon background color', 'ex5_vc_elements' ),
'param_name' => 'icon_background_color',
'value' => __( '#000000', 'ex5_vc_elements' ),
'group' => 'Images',
),
array(
'type' => 'textfield',
'heading' => __('Title','ex5_vc_elements'),
'param_name' => 'Title',
'group' => 'Texts',
),
array(
'type' => 'textfield',
'heading' => __( 'Text', 'ex5_vc_elements' ),
'param_name' => 'text',
'group' => 'Texts',
),
array(
'type' => 'checkbox',
'class' => 'one-third',
'heading' => __( 'Show link button', 'ex5_vc_elements' ),
'param_name' => 'show_button',
'value' => 'show',
'description' => __( 'Indicates if link button is shown)', 'ex5_vc_elements' ),
'group' => 'Button',
),
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
),
array(
'type' => 'vc_link',
'heading' => __( 'Button link', 'ex5_vc_elements' ),
'param_name' => 'button_link',
'group' => 'Button',
),
),
));
}
//render
public function vc_infocard_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'image' => '',
'icon' => '',
'icon_background_color' => '#000000',
'title' => '',
'text' => '',
'show_button' => '',
'button_text' => 'Más info',
'button_link' => '',
),
$atts
)
);
if (empty($button_text)) $button_text = __( 'Más info', 'ex5_vc_elements' );
if ($show_button === 'true') {
if (!empty($button_link)) {
$button = '<div class="ex5-vcic-button">
<a href="'. $button_link .'" target="_self" class="ex5-vcic-link" title="' . $button_text . '">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</a>
</div>';
} else {
$button = '<div class="ex5-vcic-button">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</div>';
}
} else {
$button = '';
}
$image = wp_get_attachment_image_src($image);
$icon = wp_get_attachment_image_src($icon);
//vc_build_link(
$html = '
<div class="ex5-vc-infocard">
<div class="ex5-vcic-content">
<div class="ex5-vcic-image">
<span>
<img src="' . $image[0] . '" title="history_inner_14" alt="http://oxigeno.">
</span>
</div>
<div class="ex5-vcic-icon" style="background-color: ' . $icon_background_color . '">
<img src="' . $icon[0] . '" />
</div>
<header class="ex5-vcic-headline">
<h3>' . $title . '</h3>
</header>
<div class="ex5-vcic-text">
<p>' . $text . '</p>
</div>' .
$button
. '</div>
</div>';
return $html;
}
}
new vcInfoCard();
There was a problem with the name of container shortcode. It had to be
add_shortcode( 'vc_infocardcontainer', array( $this, 'vc_infocardcontainer_html' ) );
But there's still a problem. I have a problem with do_shortcode_tag function
Attempting to parse a shortcode without a valid callback
Anybody knows how can I solve it?
I had solved it. The shortcode call was wrong because it had the wrong function name too.
public function vc_infocard_html( $atts, $content = null ) {
must be
public function vc_infocardcontainer_html( $atts, $content = null ) {

WordPress login form: rename label submit field

I have created login form for a shortcode. I could rename the label for the username and the submit field. But I would also like to rename the submit field. Any suggestions?
Php:
add_action( 'init', 'my_add_shortcodes' );
function my_add_shortcodes() {
add_shortcode( 'my-login-form', 'my_login_form_shortcode' );
}
function my_login_form_shortcode( $attr ) {
if ( is_user_logged_in() )
return 'you0re already logged in';
$defaults = array(
'label_username' => 'num dutilisader',
'label_password' => 'plaid clav'
);
$attr = shortcode_atts( $defaults, $attr );
$attr['echo'] = false;
return wp_login_form( $attr );
}
Shortcode:
[my-login-form label_username="num d'utilisader" label_password="plaid clav" label_submit="s'annunziar"]
You can try the attribute label_log_in for the submit text
you can see the available parameters in the following code
`function wpfa_login_form( $args ) {
$defaults = shortcode_atts( array(
'echo' => false,
'redirect' => site_url( '/wp-admin/' ),
'form_id' => 'loginform',
'label_username' => __( 'Username', 'wpfa-textdomain' ),
'label_password' => __( 'Password', 'wpfa-textdomain' ),
'label_remember' => __( 'Remember Me', 'wpfa-textdomain' ),
'label_log_in' => __( 'Log In', 'wpfa-textdomain' ),
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
'remember' => true,
'value_username' => NULL,
'value_remember' => false
), $args, 'wpfa_login' );
$login_args = wp_parse_args( $args, $defaults );
return wp_login_form( $login_args );
} add_shortcode( 'wpfa_login', 'wpfa_login_form' );`
Here you can create a shortcode: [wpfa_login label_log_in="Show Me The Way"].
You can try altering your code based on this

How to get the values of Wordpress customize checkboxes

I can't figure out how to get the value - whether they are checked or not - from checkboxes in the WP customize 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 this is how I try to get the value:
<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
.facebook {display:inline!important;}
</style>
<?php }
?>
They values of the checkboxes are either "" (empty) or "1", so the system registers the checking of them. However, I don't know how to get the value through the get_theme_mod approach. Also, they don't have any name values, so I can't get the value through the usual way either.
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
This is an example in my case - if that option is checked, it will echo a "sticky" class in my template.
if the setting 'my_theme_settings[social_facebook]' checkbox is unchecked:
<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>
//if setting is unchecked content here will be shown
<?php } // end if ?>
For full article see: http://themefoundation.com/wordpress-theme-customizer
Try use and customize this (tested, in 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' );
to read in template
$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
the problem is in the WP_Customize_Setting::value() it expect to return false
to uncheck the checkbox (or leave the checkbox unchecked) while some program will return '0' or ''.
In my case I have to extend the WP_Customize_Setting and override the value() method to force returning boolean.
<?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',
)));
?>
Here is my working solution:
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' );
Then check it's value (0 or 1) like so:
if ( !get_theme_mod( 'social_facebook' ) ) { // false
return;
}
// or
if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
return;
}
<div class="facebook" <?php echo ( get_theme_mod( 'social_facebook' ) ) ? "style='display:none;'" : "" ?>>

Resources