No, constructors should not define hooks.
Constructors should be used to set the initial state of the object. Hooks have nothing to do with the object’s initial state, so they don’t belong in the constructor. Constructors should not have “side effects”, so that using new ClassName()
does not affect any other parts of the program, which is what registering hooks does.
A good overview of this issue is this article by Tom McFarlin. It also offers an alternative.
Related Posts:
- How to set a custom path, for ajax image upload folder in admin?
- How to call function at the bottom of post using plugin?
- Using register_activation_hook in classes
- Custom plugin: Trying to show saved data on frontend
- Change the footer text on the login page
- How can I hook into and edit the text of a wp_nav_menu tag?
- Check if page parent has certain template
- How to remove “out of stock” variation options from size dropdown in woocommerce?
- Find variables available at a given hook
- Getting my head round WordPress filter
- How To Change The Html of Products filtration sidebar in Woocommerce?
- WooCommerce: Add Payment Gateway Field to Webhooks [closed]
- action hook wp_head higher priority as all other plugins/hooks
- When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?
- WordPress php filter admin_body_class not working
- wp_loaded hook block script enquequing
- Handling Body class based on Template
- Custom filter in admin edit custom post type responding with invalid post type?
- How can I remove a function that has been added to wordpress with add_filter?
- Edit incorrect password message WordPress
- Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
- Remove a div from RSS feed
- How to add an arrow to menu items has submenus
- WordPress Gravatar filter is removing my custom attributes
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- Adding widgets to my plugin page instead of WordPress dashboard
- Adding function to Genesis genesis_header [closed]
- How to remove the message ‘We could not find any results for your search’ without changing template files and without adding posts/pages?
- How to have different site identity logos on each page on Astra Theme [closed]
- Can I remove or edit an include() from a function with a filter?
- Must filter functions receive all arguments passed to them?
- How to sort WooCommerce products page by latest in-stock items first?
- How to Create custom block for displaying information in content section which act like shortcode
- Filter wordpress posts without searching the keywords in the post content
- Variation prices breakdown only for single product page
- How to edit post meta data before publishing the post it self wordpress?
- Fatal error: Uncaught Error: Class ‘WP_Block_Styles_Registry’
- get Woocommerce product format json for WP_Query
- Register/enqueue scripts only on certain admin pages
- Is there an option to execute javascript file only on plugin activation
- How do I remove an action hook inside a class that is called by another class?
- add_filter() inside another add_filter()
- Add class to all meta boxes for a custom post type
- Add custom filter to register data in array
- How to add custom checkout field in user details mail template
- For each loop will not append to the_content hook
- Assign new post author IF another user in custom field on post transition
- Is there a hook that I can use when a fatal error occurs?
- how to append ACF field data using one of the following filter/ hooks
- Edit password change email text or disable it
- Custom filter not affecting gtag script position in WordPress footer
- Using send_password_email filter after wp_update_user
- Add Imports to Existing WordPress Import Map
- Any ideas why I am getting “I’m getting “wp-admin/admin-ajax.php 401 (Unauthorized)”
- Can a plugin redirect product page based on IF condition?
- Cant register rest routs from class instance
- Save html content of a widget textarea
- Plugin development and composer
- How to Schedule Cronjobs for start of every month and year
- How can I hook into the wp_mail function used by BackWPup?
- Can not add admin notices from the edit_user_profile_update hook (notices not being displayed)?
- Save user total active time after login in wordpress [closed]
- Can’t upload CSV file to plugin directory using custom upload form in admin panel
- wordpress frontend editor to add extra css to website
- Validate and Sanitize WP REST API Request using WP JSON Schema?
- WordPress: Add custom add_filter for custom functions
- how to Update 15k products on plugin activation with meta_option
- Get user custom field value on function.php
- How do I create a secondary version of the_content
- Why does my settings form redirect to the homepage?
- Get the_content surrounded by instead of
- Using admin-post.php for admin form but it directs me to admin-post.php white screen
- Custom query vars filters problem with pagination
- Filter by field with array value in ACF on WP REST API
- Set default Database Storage Engine when creating tables with plugins?
- Hooks for post saving make a post-new.php to load latest post’s data
- Configure WordPress to Generate Scheme-less Relative URLs
- Send notification email to admin for every new post published
- Adding function to child theme’s function.php
- wrap a span tag around author’s post count
- Custom Registration username_exists / email_exists
- How can I search all plugins for composer’s vendor/autoload.php?
- WordPress WP_Query without query GET parameters
- Get the name of menu item with wp_nav_menu
- Where to copy woocommerce files to in my custom theme to avoid editing the core plugin?
- Comapare get_user_meta value
- How to find list of all functions bind to a particular hook from my plugin?
- Action Hook Inside WordPress Plugin Shortcode
- Displaying content on posts page based on category
- How can I display Custom Post type Custom Columns and its Content in a Dashboard Widget?
- custom post type with role Vendor
- update_post_meta() not saving data inside of save-post filter
- How to override a plugins script
- How to add aria role and schema markup to custom walker container
- Call to undefined function error in plugin
- Adding Additional Variables on Menus Page
- Dynamically adding filters
- How do I make this Metabox show current DB value?
- Woocommerce custom Plugin in wordpress [closed]
- Add a custom class to the body tag using custom fields