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
- Add Imports to Existing WordPress Import Map
- Change comments form title on a page by page basis
- Two-step login process – Is it possible?
- Display only text to WordPress loop without loosing the text formatting
- append PHP function to the_content
- Hooking new functions to actions + passing parameters
- Define a function outside a class and call the function using action or filter hook
- Replace existing content from specific WooCommerce admin orders list column
- WordPress template_include filter not working properly
- “The plugin generated 2694 characters of unexpected output…” on Plugin activation, CREATE TABLE sql command not working
- How to display a value from a radio button in the options menu in wordpress
- using add_action for a header hook that has an additional parameter
- WP add_action factory
- Get_avatar filter?
- How to show phpinfo() only in a new tab?
- Why does my callback function not work with this custom filter hook?
- How to use $_GET function WordPress backend
- Extend PHP regex to cover “srcset” and “style” attributes
- how to make custom ajax handler?
- Bind a function with its own argument to show something dynamically after every content
- Building a REST API for your web app exposes primary keys of DB records?
- Hook add_attachment error
- Update variable value via add_filter
- How do I create a secondary version of the_content
- How to find list of all functions bind to a particular hook from my plugin?
- Action Hook Inside WordPress Plugin Shortcode
- Add a custom class to the body tag using custom fields
- Submit form to db
- WordPress doesn’t load HTML
- Add a custom WooCommerce settings tab with sections
- Show related products by attribute based and certain conditions
- code that I can run, or a plug in to show what sql tables something pulls information from
- Automatic email message after manual user approval
- How to bind each “the_content” elements to a custom variables
- Custom Bulk Actions handler not firing
- How to properly escape in ternary operators – Wp Coding Standards?
- Add Admin Option w/ Anonymous Function
- Add_action not calling callback function
- How to use foreach and if statement within array of arrays?
- How to edit the default database of WordPress [closed]
- Cookie not created in AJAX request
- Transate plugin with js & wp_localize_script
- WP Custom tables query
- Import js variables loaded via wp_localize_script() into js module without global scope connection
- Redirect users by role to custom pages
- Add attribute to featured image with filter (wp_get_attachment_image_attributes)
- getJSON response to PHP
- Custom plugin with shortcode not working
- How rename wp-content and wp-admin folders correctly
- Unable to logout correctly after wp-login file was modified
- how to escape alert/window.location.replace with variable