php - Remove an action based on screen size in WordPress / WooCommerce

81

I've added an action via hook like this

function my_wc_function(){

//function codes here;
}

add_action('woocommerce_before_shop_loop_item_title','my_wc_function' );

Now in my theme options, users have the option to remove this function, so I got this done:

if (get_option('wc_remove_function') == 1) {
 remove_action('woocommerce_before_shop_loop_item_title','my_wc_function' );
}

Everything goes well but my concern this, I have another option in my Theme Options where they should be able to remove the same function only on mobile screens (say less than 1024px).

How I can achieve this?

134

Answer

Solution:

Can you please try below code:

<?php
    // Create the function, so you can use it
    function isMobile() {
       return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
    }
    // If the user is on a mobile device, redirect them
    if(isMobile()){
       remove_action('woocommerce_before_shop_loop_item_title','my_wc_function' );
    }
?>

For more info check this link

People are also looking for solutions to the problem: PHP preg_replace: how to replace text between tags?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.