admin管理员组

文章数量:1207718

class Singleton {
    public function __construct() {
        // Case #1
        add_action( 'woocommerce_before_shop_loop_item', [ $this, 'open_product_wrapper' ], 10 );
        add_action( 'woocommerce_shop_loop_item_title', [ $this, 'render_product_title' ], 10 );
        
        // Case #2
        add_action( 'woocommerce_before_shop_loop_item', [ $this, 'product_wrapper_open' ], 10 );
        add_action( 'woocommerce_shop_loop_item_title', [ $this, 'product_title' ], 10 );
    }
}

Are there any conventions or should I strive for consistency?

NOTE: In Laravel projects I always use verbs because each method is doing something.

class Singleton {
    public function __construct() {
        // Case #1
        add_action( 'woocommerce_before_shop_loop_item', [ $this, 'open_product_wrapper' ], 10 );
        add_action( 'woocommerce_shop_loop_item_title', [ $this, 'render_product_title' ], 10 );
        
        // Case #2
        add_action( 'woocommerce_before_shop_loop_item', [ $this, 'product_wrapper_open' ], 10 );
        add_action( 'woocommerce_shop_loop_item_title', [ $this, 'product_title' ], 10 );
    }
}

Are there any conventions or should I strive for consistency?

NOTE: In Laravel projects I always use verbs because each method is doing something.

Share Improve this question edited Mar 12, 2022 at 12:32 user557108 asked Mar 12, 2022 at 12:16 user557108user557108 1474 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Are there any conventions or should I strive for consistency?

No, as long as your actions are readable, unique, and it's clear what they do, there is no rule to follow.

Ideally the names you choose are consistent within the code you write. E.g. WooCommerce has chosen to use the woocommerce_ prefix, or when ACF uses acf\. If you think verbs works for you then use verbs, just do it consistently and clearly.

The one thing I would say, is never have a fully dynamic name, e.g. passing a variable from a separate source assuming they're all unique, e.g. add_action( $form_name, '....

本文标签: plugin developmentShould action callbacks start with a verb