1

I have a basic question that has always stumped me. I'm trying to move the Star Rating into the comment meta, which will help me style it. Here's the template file review.php section:

<div class="comment-text">

           <?php
           /**
            * The woocommerce_review_before_comment_meta hook.
            *
            * @hooked woocommerce_review_display_rating - 10 //Want to unhook this.
            */
           do_action('woocommerce_review_before_comment_meta', $comment);

           /**
            * The woocommerce_review_meta hook.
            *
            * @hooked woocommerce_review_display_meta - 10
            */
           do_action('woocommerce_review_meta', $comment);

           do_action('woocommerce_review_before_comment_text', $comment);

           /**
            * The woocommerce_review_comment_text hook
            *
            * @hooked woocommerce_review_display_comment_text - 10
            */
           do_action('woocommerce_review_comment_text', $comment);

           do_action('woocommerce_review_after_comment_text', $comment);
           ?>

       </div>

I was thinking I could add the following to my functions.php:

    remove_action('woocommerce_review_display_rating', 10);

And add it back in the review-meta.php, but I can't remove / unhook the star rating. What am I doing wrong?

1
  • 1
    10 is not the name of a function
    – Tom J Nowell
    Jun 25 at 12:49
3

You need to unhook the function from that action specifically:

remove_action( 'woocommerce_review_before_comment_meta',
               'woocommerce_review_display_rating', 10 );

(e.g. look for the opposite add_action() line in includes/wc-template-hooks.php)

You'll also need to make sure that this is run after WooCommerce has loaded and added the action you're about to remove, e.g. place this in a plugins_loaded hook:

function unhook_display_rating() {
    remove_action( 'woocommerce_review_before_comment_meta',
                   'woocommerce_review_display_rating', 10 );
}
add_action( 'plugins_loaded', 'unhook_display_rating', 10, 0 );
3
  • Thank you, this worked. I appreciate the help.
    – sackadelic
    Jun 25 at 13:15
  • How would I then add the rating back into review-meta.php? Would I do_action('woocommerce_review_display_rating'); in the template file?
    – sackadelic
    Jun 25 at 13:22
  • 1
    No, you can't call it with an action anymore. You could just call the function: woocommerce_review_display_rating();
    – Rup
    Jun 25 at 15:04
3

remove_action requires that the second parameter is the name of the function/callable that was added, and the priority must match what was added.

The official documentation says its parameters are:

remove_action( string $tag, callable $function_to_remove, int $priority = 10 )

You can use also remove_all_actions:

remove_all_actions( 'hook name' );

Remember, hooks are like events, you can't remove something that hasn't been added yet, so when you call this function matters, not just how it's called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.