wp_customize and selecting layout - php

So I'm trying to build a conditional statement to display different layouts depending on the option selected.
Having a bit of trouble.
$wp_customize->add_setting('layout', array(
'default' => 'stream',
)); // add our default setting.
$wp_customize->add_control('layout', array(
'label' => __('Select layout', 'Ari'),
'section' => 'layout',
'settings' => 'layout',
'type' => 'radio',
'choices' => array(
'stream' => 'Stream',
'grid' => 'Grid',
),
)); // use radio buttons with (so far) two choices.
$wp_customize->add_section('layout' , array(
'title' => __('Layout','Ari'),
)); // add our layout section in the customize panel.
Displaying our layout.
<?php if ( get_theme_mod( 'stream' ) ) : ?>
<p>stream</p> if ( have_posts() ) etc
<?php elseif ( get_theme_mod( 'grid' ) ) : ?>
<p>grid</p> // if ( have_posts() ) etc
<?php else : //Nothing ?>
<?php endif; ?>
Just...nothing. I've trawled through the codex and I can't see what I'm doing wrong. It just doesn't show any output.
#Stewartside points out that that I should use get_setting, I can't get this working though. Anyone?

get_theme_mod() returns a string, not a boolean. Therefore, your if statements will always fail as they are expecting either 1 or true or 0 or false.
You should look into using get_setting within the $wp_customize functionality.
Documentation

Related

Wordpress customizer preview doesn't refresh functions.php

So I ran into a issue where the customizer preview doesn't fully refresh. Only when I manually refresh the page I see my changes. Some of my code to help explain below.
For my customizer settings I have code that looks something like this
$wp_customize->add_section( 'theme_layout', array(
'title' => __( 'Layout', 'theme' ),
'priority' => 30
) );
$wp_customize->add_setting( 'theme_header_layout', array(
'default' => 'default',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Control( $wp_customize,
'theme_header_layout', array(
'label' => __( 'Header', 'theme' ),
'section' => 'theme_layout',
'settings' => 'theme_header_layout',
'type' => 'select',
'choices' => array(
'default' => 'default',
'special_header' => 'Special Header',
)
) ) );
Now In my functions.php I have code like this
//this is the code that doesn't seem to execute when the customizer refreshes
if ( 'special_header' == get_theme_mod( 'theme_header_display' ) ):
function theme_special_header( $items ) {
$items .= do_shortcode('[special_header_shortcode]');//This shortcode exists I just didnt bother mentioning it here
}
add_action( 'wp_nav_menu_secondary-menu_items', 'theme_special_header' );//Adds shortcode to menu with id of secondary-menu
endif;
This all works great accept when I go to the customizer and select 'Special Header' the customizer refreshes and I don't see my changes until I completely refresh the page.
I had also faced similar issue earlier. Rather than adding conditional outside, I kept it inside the function and it worked. You can try similar approach for your code and it may help.
Following is not exact answer for your question but it may help to fix your problem.
function wpso_customize_menu($items, $args) {
if ( 'special_header' == get_theme_mod( 'theme_header_layout' ) ) {
if( $args->theme_location == 'menu-1' ) {
$items .= '<li>Custom Link</li>';
}
}
return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);

Custom built review plugin - pagination not working

I'm currently create a plugin that takes all comments from all products and places them under the review tab for all comments.
Inside Wordpress I have the following setting checked:
Settings > Discussion > Break comments into pages with...
This enables pagination on the default comment section. I've added this code with my code but it doesn't enable pagination, even when I remove the If statement.
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
) ) );
echo '</nav>';
endif; ?>
I'm currently using this function to call all reviews into one:
function products_all_reviews(){
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments );
}
What needs to be done to call/enable pagination properly? This is my first time creating plugin so the help is much appreciated

Override woocommerce class files

I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-frontend-scripts.php in woocommerce/includes/
The function I'm looking to modify is:
private static function get_script_data( $handle ) {
global $wp;
switch ( $handle ) {
case 'wc-single-product' :
return array(
'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options', array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
) ),
'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
'photoswipe_options' => apply_filters( 'woocommerce_single_product_photoswipe_options', array(
'shareEl' => false,
'closeOnScroll' => false,
'history' => false,
'hideAnimationDuration' => 0,
'showAnimationDuration' => 0
) ),
'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
);
break;
}
return false;
}
For my product slider, I need to show prev and next arrows on single product page. So I need to change 'directionNav' as true.
How do I do this without making changes to the core files?
Have you created a child theme yet? If not, that's your first step. Here's a link from the WordPress codex:
https://codex.wordpress.org/Child_Themes
Then instead of editing that core file, you hook into that function instead, and write that code inside of the functions.php file you created in your child theme. If you want to add code to the existing function, you'll want to use an action hook. If you want to modify the code, you use a filter. It looks like you're trying to change the code, so probably a filter is best.
Here's how it would look using a filter:
add_filter( 'get_script_data', 'change_nav_direction' );
function change_nav_direction( $variable ) {
//any other code you may need
'directionNav' => true,
//you must have a return
return $variable;
}
Here's a link to an article from WooCommerce that might help:
https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/

Can't use the variable $checkout with WooCommerce-Multistep-Checkout

I'm trying to create a new step in my checkout page and i'm currently using the plugin WooCommerce-Mulitstep-Checkout plugin. I followed the documentation to create a new step (here : http://woocommerce-multistep-checkout.com/documentation/). So here is my code in function.php:
add_action('woocommerce_multistep_checkout_before_order_info', 'add_my_custom_step_with_new_field');
function add_my_custom_step_with_new_field( $checkout ) {
wc_get_template( 'checkout/my-custom-step.php', array( 'checkout' => $checkout, 'test' => "test" ) );
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
And here is my code in checkout/my-custom-step.php :
<h1> Step Title</h1>
<div class="my-custom-step">
<?php
woocommerce_form_field('my_field_name', array(
'type' => 'text',
'required' => true,
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value('my_field_name'));
?>
</div>
}
When I use this code (mostly provided by the official documentation), I get this error on the browser console :
GET https://localhost/mywebsite/checkout/ 500 (Internal Server Error)
And when I remove , $checkout->get_value('my_field_name'), it works perfectly fine (but without prefilling the fields.
Moreover, when I use var_dump($checkout);, it displays me this : string(0) "".
Finally, when I want to display $test, it works perfectly fine. So I don't think I use badly the variable.
So my questions are :
Do you know all the mystery behind this empty variable ?
Did I misused something ?
Why is there a final curly bracket at the end of the template in the official documentation ?
Thanks

WordPress Meta Box Plugin Displaying Cloned Text

I am trying to output this custom meta in one of my WordPress page templates. The documentation of the plugin seems to be lacking. ( http://metabox.io/docs/get-meta-value/ )
Because that I have clone as true it is displayed as so in the custom post type
I am trrying to display it VIA html so maybe the output would be something like
<ul>
<li>Red LED footwell lighting</li>
<li>Red LED Trunk Lighting</li>
<li>etc...</li>
</ul>
Here is how I defined the item I am trying to display
array(
'name' => 'Interior Mods',
'desc' => 'List all of the interior mods',
'id' => $prefix . 'intmods',
'type' => 'text',
'std' => '',
'class' => 'custom-class',
'clone' => true,
),
Thanks
You can use plug-in codes for get value.
$values = rwmb_meta(
'YOUR_PREFIX_text',
$args = array(
'type'=>'text',
// other options for meta field
),
$post_id = $post->ID
);
if($values){
echo "<ul>";
foreach ($values as $key => $value) {
echo "<li>".$value."</li>";
}
echo "</ul>";
}

Categories