In general: Yes, wait for a dedicated hook to start your own code. Never just throw an object instance into the global namespace. But init
is rarely necessary.
You hook in as late as possible. If your first code runs on wp_head
do not use an earlier hook. You can even cascade hooks:
add_action( 'wp_head', 'first_callback' );
function first_callback()
{
// do something
// then
add_action( 'wp_footer', 'second_callback' );
}
Regarding the init
hook: Use wp_loaded
instead. That runs after init
and after ms_site_check()
was called. This way you avoid to run your plugin on an invalid sub site in a multi-site installation. Everything else is the same.
Related Posts:
- Difference between after_setup_theme and init action hooks?
- Difference between hooks Plugin_loaded and admin_int?
- wp_redirect() not working on form submission with init hook
- What is the best filter where to use register_block_type?
- Prevent attachment from being deleted
- Get log of function/method calls made by do_action() on WordPress initialization
- Hooking into the init action will fire it too frequently?
- Plugins rewrite rules the right way
- PHP header() gives headers already sent
- Why can’t a custom postype be registered with “admin_init” hook?
- How to cancel an action hooked to untrash_post? or any hook
- Session management issues with WordPRess 404 Error page
- WP_mail() Issue. Duplicate emails if $_GET[‘A’] == email
- Function Hooked on Init Running Multiple Times
- Redirect back to origin page after using get_delete_post_link()
- Which hook should be used to add an action containing a redirect?
- WooCommerce: change display order of product short description and price [closed]
- Is there a way to set default custom fields when creating a post?
- Add_action to wp_head via functions.php
- Hook that fires when admin setting is saved
- Making Custom Fields Standard in the Admin UI
- What is the difference between update_post_meta and update_postmeta hooks?
- How to remove action hook done in a plugin from functions.php in my theme?
- Hooks for trashing, deleting, saving, restoring custom post type
- Implementing advanced add_* function wrappers
- My add_action (wp_footer, ‘method’) is not calling?
- Hook into wp_head(); in a plugin
- Use $query->set multiple times with pre_get_posts?
- How can I hook into the post editor title field in order to change the HTML?
- WordPress v5.0.3 Gutenberg & JS error “Uncaught SyntaxError: missing ) after argument list”
- Can’t get ID of post that relates to the comment
- Searching hook to set cookies before and avoid “headers already sent”
- Hook before inserting user into database [duplicate]
- Schedule cron event from widget
- Customizing the default logout page of WordPress
- Add my own function to existing WooCommerce hook
- WooCommerce – Overwrite action hook [closed]
- Admin Hook at the Login Page
- Which Hook? Hide attachments from specific post types in media library
- Custom action on login and “remember me”
- How to hook into Add New Member in BuddyPress
- Using wp_editor tinyMCE in metabox cause form alert on leaving page
- Hook when new CPT published AND postmeta inserted
- Need to check is_archive during init
- When WP_CLI runs WordPress from the command line, are hooks called?
- Executing Arbitrary Code at a Specific URL without Creating a Post or Page?
- Query categories that have a description
- Save User Meta Email Address in Lowercase
- Trying to run a compile command while saving post
- Best place for short bio,image and button [closed]
- How to cancel the trash action inside wp_trash_post
- Action hook for custom tax edit
- wp cron job fires at every second or so, if callback is wrapped with DOING_CRON check it never fires at all
- load-* hook for dashboard
- I don’t understand how add_action and do_action work in tandem. The former executes the code already…what is do_action for?
- Insert HTML in post, below nav bar but above content?
- Convert Gravity Form Entry Date on Export
- Hook Adding content after title
- Fatal error: Call to a member function get_queried_object_id()
- “admin_enqueue_scripts” hook or $_GET[‘page’]?
- How to add content to suscriptor dashboard frontend?
- Add PHP code to header section from plugin
- How to change a WordPress term’s slug before saving
- dynamic add_action according to child pages (for homepage control)
- Remove action in a parent theme from the child theme
- Remove action within a class in a parent theme’s includes folder from the child theme
- Action hook save_post for newly created posts – $post object data is all empty
- add_filter to the_content from plugin function
- Add html code in admin page
- What hook should I use that will fire whenever I open a post for editing in the WP back-end?
- Can not using get_header() cause trouble (later on)?
- Is there a author_update action?
- Editor capabilities – admin_init
- How to set Media Box form elements default value?
- How to get product information using hooks?
- How to read and write session data?
- Print additional fields in wp_new_user_notification_email_admin
- Save_post – Warning: Cannot modify header information
- How to use WP conditional tag in a functions.php with OCEANWP Theme?
- remove_action not working for a function
- Is there no concise way, a library maybe, to help with unhooking class functions and so on?
- Debug a WP install: how to find which functions write post updates during a process (a woocommerce checkout in my case)
- What hook should I use to add post meta data with on update?
- Problem with call_user_func_array front end
- How to I prevent WordPress from switching external HTTP links to HTTPS?
- Stylesheet is getting removed/deregistered automatically
- How to override get_avatar() function?
- How to append the excerpt to the content in the single post page?
- Change user role if it’s orders count more than
- set_post_format called after wp_update_post when using bulk edit?
- Checking if some condition is matched before inserting post into database
- Using actions, hooks and filters in a non-WordPress page
- Display_rows() and column_cb() strange behaviour
- Hook into create_category
- Trouble adding data to options table
- How to hook into publish_posttype?
- How wordpress plugin hooks works? [duplicate]
- How to add a new link to the default register form’s footer links?
- $GLOBALS & global doesn’t work [closed]
- Hooks not working on live server