WooCommerce: how to wrap Recent products widget? - woocommerce

Using built-in WooCommerce widget "Recents products" displays a title (text) followed by an unordered list of products. Surprisingly these two main elements (the title and the list) are not wrapped in any container, making it hard to style, integrate into a theme and ultimately hard to use. There's no option either for that in the widget menu.
Still is there any way to wrap them I may have not thought of ?

WooCommerce makes use of Filters to help us override the HTML output by the default Widgets.
For example, the WooCommerce Products, which I believe is the one you are referring to makes use woocommerce_before_widget_product_list and woocommerce_after_widget_product_list filters. You can simply hook your functions and modify the default output. Something like this :
add_filter( 'woocommerce_before_widget_product_list', 'my_theme_before_widget_product_list', PHP_INT_MAX );
add_filter( 'woocommerce_after_widget_product_list', 'my_theme_after_widget_product_list', PHP_INT_MAX );
function my_theme_before_widget_product_list( $html ) {
return '<div class="my-theme-product-list">' . $html;
}
function my_theme_after_widget_product_list( $html ) {
return $html . '</div><!-- /.my-theme-product-list -->';
}
If you are using the widgets within a sidebar, you can use before_widget, after_widget, before_title and after_title arguments of register_sidebar function.

Related

Remove description meta box from custom taxonomy

I'm trying to remove these areas from one of my custom taxonomies.
I've built them using the two plugins: Custom Post Types UI (to add them) and Advanced Custom Fields (to add fields to the taxonomy).
I can't see anything in the plugin settings to remove these things, but I'm not sure if I'm missing something.
I'm assuming I might need to add a function to the functions.php file. I've seen that hiding things using jQuery is a possibility, but I hear that this might show it initially on load and then hide it, so id like to learn how to doit properly.
I managed to get there with the link above and help from another website.
This function removed the two instances of the 'description' box:
function hide_description_row() {
echo "<style> .term-description-wrap { display:none; } </style>";
}
add_action( "measure_sectors_edit_form", 'hide_description_row');
add_action( "measure_sectors_add_form", 'hide_description_row');
and this one removed the column from the right hand side:
add_filter('manage_edit-measure_sectors_columns', function ( $columns ) {
if( isset( $columns['description'] ) )
unset( $columns['description'] );
return $columns;
});
To use with other taxonomies, just replace 'measure_sectors' with your own taxonomy slug

Woocommerce add_to_cart hook empties cart

I'm using the woocommerce_add_to_cart action hook like so:
add_action('woocommerce_add_to_cart', 'display_more_modal', 10, 6);
function display_more_modal( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
echo "<h1>Test Title here...</h1>";
//or
?>
<h1>More text here</h1>
<?php
}
I have found that if I echo out HTML or output HTML, as illustrated above, it removes all items from the cart, once I go the cart to view what I have added.
Could you some illustrate what I am possibly doing wrong? I have a suspicion that I have to return a value instead of echoing it.
If this is the case, please help as to how I can echo out HTML when a user adds a product to the cart. This HTML will be a popup with related products. Thank you.
I figured that some hooks within Wordpress don't like echoing anything out. I'm not sure how hard & fast this rule is but I remember it generally tends to be bad practice to echo anything out within functions. Instead they should return something. I simply created the product which was created and returned that. In the view file I created an if statement which outputs the html for the modal if a product exists. That seems to have fixed it.

Replace Parent WordPress Title Tags in Child Theme Function

Is it possible to replace the HTML of a WordPress' parent theme's title tags? I'd like to know how.
A lot of SEO plugins adjust the title tags of themes with wp_title but often developers add additional characters and words like <title><?php bloginfo('name'); ?><?php wp_title(' - ', true, 'left'); ?></title>.
I've started to develop the following inside the child theme's functions.php:
function set_title($HTMLstr, $content)
{
return preg_replace('/<title>(.*)\</title>/i', wp_title() , $HTMLstr);
}
You want a "filter", which is a function that can modify the value of arbitrary data based on a filter name.
The filter you want is called "wp_title"
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title
You would set it up something like:
function set_title( $title, $sep ) {
return "Custom title";
}
add_filter( 'wp_title', 'set_title', 10, 2 );
The "10" may need raised so that this filter applies after any filters in your parent theme. You can access the $title and $sep values as well, which will be passed through other filters too.
Filters typically go in functions.php.

Get WordPress Sidebars Fom The Current Front End

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?
I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End
dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this
function wrap_dynamic_sidebar( $sidebar_id )
{
global $sidebars_in_this_template;
$sidebars_in_this_template[] = $sidebar_id;
return dynamic_sidebar( $sidebar_id );
}
And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).
If you want to display a list of all sidebars, you could use $wp_registered_sidebars
global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Resources