Change a Wordpress Hook in Functions.php - php

I'm attempting to replace a hook name so that my breadcrumb is removed from one location and added to another spot in the page. I'm using a child theme and I'd like to accomplish this within the functions.php of my child theme. What happens is that the function is removed from one location, and added to the new location, but it fires twice, so I have two breadcrumbs stacked on top of each other.
The parent theme function looks like this:
/*-----------------------------------------------------------------------------------*/
/* Breadcrumb display */
/*-----------------------------------------------------------------------------------*/
add_action('woo_main_before','woo_display_breadcrumbs',10);
if (!function_exists( 'woo_display_breadcrumbs')) {
function woo_display_breadcrumbs() {
global $woo_options;
if ( isset( $woo_options['woo_breadcrumbs_show'] ) && $woo_options['woo_breadcrumbs_show'] == 'true' && ! (is_home()) ) {
echo '<section id="breadcrumbs">';
woo_breadcrumbs();
echo '</section><!--/#breadcrumbs -->';
}
} // End woo_display_breadcrumbs()
} // End IF Statement
And this is what I have in my functions.php file of the child theme.
add_action( 'woo_main_before', 'remove_woo_display_breadcrumbs', 0 );
function remove_woo_display_breadcrumbs() {
remove_action('woo_main_before','woo_display_breadcrumbs',10);
}
add_action('woo_content_before','woo_display_breadcrumbs',10);
By the way, this is the Mystile theme by Woo Themes. I'm really new to Hooks, so if you do have the answer, a short explanation of why would be really helpful.

OK, so first, to fix the problem you need to do the following:
remove_action('woo_main_before', 'woo_display_breadcrumbs', 10); // Removes from the original location
add_action('woo_content_before', 'woo_display_breadcrumbs', 10); // Adds to the new location
The '10' is the priority that a function runs for a particular action. Think of it like a slot.
So slot 0 is completely independent from slot 10.
All we're doing in the code sample above is taking woo_display_breadcrumbs out of one slot and inserting it into another.
I hope this helps.

Related

if body tag has class 'home' via child theme

I'm trying to simply show content with a php if statement.
I would like to add a footer element (to the footer.php) for the homepage only.
So, if body tag has class 'home'; then add new footer element. I'm trying below; but currently nothing is displaying at all.
$classes = get_body_class();
if (in_array('home',$classes)) {
function lastUpdated() {
echo '<div class="last-updated">Last Updated:<span class="date-update"></span></div>';
}
}
the above code is in my child theme's functions.php file;
First of all, how WP will know, where you want to output that markup?
To solve this question you can use hooks, you need to monitor parent theme for such code do_action('before_theme_footer'), to find place where you can "inject" your code.
And then you can apply your output to this hook, and inside callback function check if you on home page with is_front_page():
function footer_markup(){
if( is_front_page() ) {
echo '<div></div>';
}
}
add_action('before_theme_footer', 'footer_markup');
You can read about hooks here - https://designmodo.com/wordpress-hooks-filters/
If no any hooks in footer.php, you can copy footer.php to child theme and add your code in the right place where you want to output it:
if( is_front_page() ) {
echo '<div></div>';
}

Remove Woocommerce sidebar from any theme

I'm using WordPress 4.9.4 running Twenty Seventeen Child Theme theme with Woocommerce Version 3.3.4. I am trying to remove the sidebar… I have tried using this:
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);
But haven't found the right one yet.
How do I remove all sidebars?
The best and simple way that works with all themes is to use the get_sidebar Wordpress action hook this way:
add_action( 'get_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
if ( is_woocommerce() && empty( $name ) )
exit();
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You might need to make some CSS changes on some html related containers
This code works on any theme as all themes use get_sidebar() Wordpress function for sidebars (even for Woocommerce sidebar) and get_sidebar action hook is located inside this function code.
WooCommerce ads the side bar in code directed at this specific theme, in class WC_Twenty_Seventeen
/**
* Close the Twenty Seventeen wrapper.
*/
public static function output_content_wrapper_end() {
echo '</main>';
echo '</div>';
get_sidebar();
echo '</div>';
}
I used this code to replace that function
remove_action( 'woocommerce_after_main_content', array( 'WC_Twenty_Seventeen', 'output_content_wrapper_end' ), 10 );
add_action( 'woocommerce_after_main_content', 'custom_output_content_wrapper_end', 10 );
/**
* Close the Twenty Seventeen wrapper.
*/
function custom_output_content_wrapper_end() {
echo '</main>';
echo '</div>';
echo '</div>';
}
Use the is_active_sidebar hook - this should work in any theme as it's core WordPress functionality:
function remove_wc_sidebar_always( $array ) {
return false;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_always', 10, 2 );
You can also use conditional statements to only hide the sidebar on certain pages, e.g. on Product pages:
function remove_wc_sidebar_conditional( $array ) {
// Hide sidebar on product pages by returning false
if ( is_product() )
return false;
// Otherwise, return the original array parameter to keep the sidebar
return $array;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_conditional', 10, 2 );

Woocommerce | If page_id= .. { }

I am trying to apply specific functions to a certain page ID in Woocommerce.
However, it does not seem to apply. I have looked at both Wordpress and Woommerce conditional tags (https://docs.woocommerce.com/document/conditional-tags/).
The simple code I am testing is shown below, but even that is not working.
if ( is_product()) {
echo ' HALLO sdfdsfsdfsdfsdf ';
}
I have also tried with page_id and post_id but cannot get this to work.
I have added this code to my functions.php in my child theme.
Does anybody know to to write a specific function only for a specific product page (I know the value).
Method - 1
is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.
add_filter( 'template_include', function( $template ) {
if ( is_product() )
echo 'HELLO';
return $template;
});
Method - 2
The WordPress/WooCommerce conditional tags won’t work until after the wp action is fired, making it the earliest point at which you can use the tags. You can view the entire list, and the order of the core WordPress actions at the Action Reference page.
add_action( 'wp', 'init' );
function init() {
if ( is_shop() ) {
echo 'HELLO, this works!';
}
}

WooCommerce how to check if page is_shop() in functions.php?

In WooCommerce, My Category Listing page and product listing page are rendered from archieve-product.php ( By Default) . How to check if page is_shop() in functions.php? As is_shop function does not work in functions.php. I simply want to remove my sidebar from Category listing page not from product listing page.
When placed inside a hook, is_shop will work in functions.php
add_action( 'template_redirect', 'custom_template_redirect' );
function custom_template_redirect() {
if( is_shop() ) :
// code logic here
endif;
}
Here is a list of all WooCommerce conditionals
You can write a condition into "archive-product.php" for category page like,
$cate = get_queried_object();
if(is_product_category() && $cate->parent != 0 ){
// Write code here
//include sidebar here
}
By using this code this will check the page for product_category and also check for a parent.
call it using WordPress Hook pre get posts
add_action('pre_get_posts','nameTheFunction');
function nameTheFunction(){
if(is_shop()){
// your code here
}
}// function end here
Read more about pre get posts Hook
https://developer.wordpress.org/reference/hooks/pre_get_posts/
You can use function_exists
if( function_exists("is_shop") ) {
// call it or do something else
}
else {
// load it from somewhere
}
Official docs: https://secure.php.net/function_exists

how to disable the comments for specific category in wordpress?

How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}

Categories