How to use apply_filters() inside a plugin class?

Your code is good — you are using both apply_filters() and add_filter() correctly. But the timing/hook does not seem correct. I think it’s clear from what everyone is saying that it must be an issue of things happening in the wrong order, but don’t plugins always load before the theme? Wrong order: Yes — you … Read more

How to get post image caption by index

I have used function to extract data inside figcaption tags, and $position is stand for the index of the current image. function getImageCaption($postID, $position){ $post_id = $postID; // get the post object $post = get_post( $post_id ); // we need just the content $content = $post->post_content; // we need a expression to match things $regex … Read more

Use PHP Class in WordPress functions

If you have defined the class MyCart directly in functions.php with your hooks. I’d expect for the following to work: // add action $cart = new MyCart(); function emdr_add_to_cart(){ $items = $cartClass->get_users_cart_contents(); // process items } add_action( ‘woocommerce_add_to_cart’, ’emdr_add_to_cart’); I might also add that duplicating the “state” of the users cart in 2 places is … Read more

I want to show cart items number if any or nothing, simple CART text

You’re already on the right track. Simply add conditional if 0 <?PHP $cart_count = WC()->cart->get_cart_contents_count(); $cart_count = $cart_count ? “({$cart_count})” : ”; // if 0, become empty string $cart_link = wc_get_cart_url(); $cart_button = “<a href=”https://wordpress.stackexchange.com/questions/378487/{$cart_link}”>Cart {$cart_count}</a>”; echo $cart_button;