It’s a problem as old as WP, or even older…
You can’t set cookies, or send any other headers, if any of the content of the site is already sent.
You try to set cookies using init hook. And it is OK in most cases, since no output should be printed yet. But… Not all code is written correctly.
There are many other thing done before init. Take a look at actions run during a typical request.
As you can see… Themes get loaded before init hook. And so do plugins.
So if their code cause any warning/notice (and the debug is on) or they generate any other output, then you’ll get error while setting cookie.
Another popular cause for this problem is using closing PHP tags (?>) at the end of files. If there is any white characters after that closing tag, then it’s treated as output also.
Related Posts:
- Setcookie works on admin but not front end
- Searching hook to set cookies before and avoid “headers already sent”
- How to get session token of current user in wp_login hook?
- Cookie cant be read even cookie is present
- Is there a way to send HTML formatted emails with WordPress’ wp_mail() function?
- Action hook for custom tax edit
- Is there a limit to hook priority?
- Hook after image is uploaded and image sizes generated
- Auto-retrieve YouTube Image for Thumbnail?
- How to call a REST endpoint when a post is published?
- __NAMESPACE__ with register_activation_hook
- Hook for URL Request
- Hook/notify when any option or setting is added or updated
- Which hooks is this? add_action(‘wp’,
- admin_notices after register_uninstall / deactivate_hook
- add_action on inherit post status
- Why does wp_enqueue_script ignore my ‘wp_head’ hook?
- wordpress custom modules/widget areas on the page? can i make my own?
- How to add hook to the_title() and get_the_title()
- action lifecycle
- Redirect users on specific post category or category page
- Returning ACF custom field from publish_post
- How to modify an add_action() inside a loop of core function
- Hooking after get_header()
- How to get post ID in post_updated action hook?
- How can I set status=’publish’ for all featured images?
- What is the best filter where to use register_block_type?
- How get list all users who edited post?
- Stuck in redirect loop after using wp_login action
- Hooks for Links Box [duplicate]
- What are the differences between comment_form_logged_in_after and comment_form_after_fields?
- Detect change to user_meta and retrieve old and new values
- Featured Image Action Hook
- Update post meta using pending_to_publish hook
- How to find a callback attached to a bbpress hook?
- Event Calendar using insert_post_data after save_post and insert_post after post_transition draft_to_schedule
- How to extend custom (non-core) blocks?
- Hooking into the init action will fire it too frequently?
- Implement Hooks Using Array
- What hook is used to display the admin_bar on the front end?
- Is there a JavaScript equivalent of get_post_field?
- User update hook
- register_activation_hook not updating
- Adding a hook to the default (page.php) template
- Add action hook into wp_localize_script
- PHP header() gives headers already sent
- read more, even if excerpt not trimmed
- How do I prevent term from being created on create_term hook?
- How to distinguish on hook profile_update user registering, user resetting password or user updating profile?
- Hook function prints output twice
- Hook priority in admin with custom plugin
- Embeding style into the header via the function.php
- ‘save_post’ hook not working in WP 3.5
- Change the Default Plugin page filter to Active intead of All
- How to run a function after wp() in the wp-blog-header.php file?
- How to execute a hook asynchronously?
- Issues with if, else, and elseif statements
- Is there a before_save_post hook or something similar
- wp query add array by if condition
- “Seen by” Feature in posts [closed]
- How to hook in to the “output buffer” and add html content
- hook for loading page
- What hook to use to redirect based on $post
- How can I modify the header of RSS feed items?
- Print results of a WP_Query to wp-admin/upload.php within ‘delete_attachment’ hook?
- upgrader_process_complete filter not fired using ajax updates
- How to debug wordpress hooked functions?
- remove_action(‘save_post’) in function that was triggered by save post not working
- How to get template name or category before publish
- WordPress How to reorder Page/Post Settings
- Is there a way to Insert a post to post list?
- “Intercept” enqueing of 3rd party’s JS file
- How to load another post if condition is true
- WP hook to add custom content after the_title()
- Struggling with plugin dev basics: add_action
- delete_user hook failed
- Prefixing widget_posts_args Hook
- wp_get_current_user Not working
- Display custom debug info inside page and not before head
- calculate values from a field and insert them into custom fields when publishing or updating post
- How WordPress understands what do with the (all) key in the $wp_filter array? [duplicate]
- Hide post completely and still reach it via cURL
- theme functions (hooks)
- frontend show edit profile with selected custom options
- How to change Akismet commenter privacy notice?
- How To Apply Different Styles To All Blocks Based on Post Meta Value?
- change position of element using hook [closed]
- The acction hook stop working if i move it from plugin file to theme’s functions.php file
- How to add action in wordpress custom plugin?
- How can I add new attributes in a Class when my addon is active?
- wp_footer content appearing in admin area
- Removing script tags that are loaded by other script tag
- Does wp_login hook fire on user registration?
- wp_trash_post not firing as expected (also applies to trashed_post)
- Hook on opening a media/document
- Prefix user login_name and validate it is unique on Registration
- Why does my function create extra empty files?
- The enqueue_block_assets is changing the styles of the editor interface
- update ACF field when saving post on back office
- Unable to trigger a frontend @wordpress/hooks action