Unable to programmatically remove product from WooCommerce cart

I guess you should use your function on the hook woocommerce_add_to_cart instead of template_redirect Your function works fine for me. Try an alternative methow which also works: add_action(‘template_redirect’, function () { $product_id = 29690; foreach( WC()->cart->get_cart() as $key=>$item){ if($item[‘product_id’]==$product_id){ WC()->cart->remove_cart_item( $key ); } } // $product_cart_id = WC()->cart->generate_cart_id( $product_id ); // $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id … Read more

How to prevent action in ajax request, when in admin side?

After testing a lot of things, I found this function, which works: function is_admin_request() { /** * Get current URL. * * @link https://wordpress.stackexchange.com/a/126534 */ $current_url = home_url( add_query_arg( null, null ) ); /** * Get admin URL and referrer. * * @link https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/pluggable.php#L1076 */ $admin_url = strtolower( admin_url() ); $referrer = strtolower( wp_get_referer() ); … Read more

Run add_action hook if condition

I’d guess this code is being run directly from your plugin’s functions.php, meaning that you’re evaluating is_front_page() and is_archive() as you load the plugin. That’s too early – that’s before enough state has been initialised to know which page you’re on. To fix this, you either need to: move the logic into your say_hello functions, … Read more

remove_action with profile_personal_options

Sometimes you need to get a little creative when customizing the WordPress admin area. It’s often possible to do it without CSS, but it isn’t always straightforward: understanding what’s happening on the source files and digging through the many functions called in the ifs and elses is a must. Here’s something slightly hacky I came … Read more

How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?

Try filtering with wp_insert_post_data instead. It’s from wp-includes/post.php, line 2864. add_filter( ‘wp_insert_post_data’, ‘post_publish_filter_wpse_82356’ ); function post_publish_filter_wpse_82356( $data ) { // view/manipulate $data if (‘publish’ == $data[‘post_status’]) { $msg = ‘<pre>’ . var_export($data, true) . ‘</pre>’; wp_die($msg); } return $data; } publish is the post_status when you click the Publish or Update button. The four statuses … Read more

use add_action(‘wp_head’) in a widget

You can call wp_enqueue_script() directly in your Widget output, and WordPress will enqueue the script. No need to add it to a callback. Try changing this: else { add_action(‘wp_head’, ‘pieChartScripts’); function pieChartScripts() { wp_register_style(‘piechart-css’, get_template_directory_uri() . ‘/css/jquery.easy-pie-chart.css’); wp_enqueue_style(‘piechart-css’); } wp_register_script(‘piechart’, get_template_directory_uri() . ‘/js/jquery.easypiechart.min.js’, ”, ‘2.0.4’); wp_enqueue_script(‘piechart’); // etc } …to this: else { wp_register_style(‘piechart-css’, get_template_directory_uri() … Read more