There is no regular output and hence no header sent before template_redirect
on the front end. If you need sessions on the back end too, use the action wp_loaded
to cover both.
Example:
add_action( 'template_redirect', function() {
$status = session_status();
if ( PHP_SESSION_DISABLED === $status ) {
// That's why you cannot rely on sessions!
return;
}
if ( PHP_SESSION_NONE === $status ) {
session_start();
}
$_SESSION[ 'foo' ] = 'bar';
});
Keep in mind that using sessions adds a whole set of very complex problems to your code, including security, scalability (load balancers), and following time consuming support issues. I don’t recommend it.
Related Posts:
- External Authentication, session_tokens not destroyed on logout
- Difference Between Filter and Action Hooks?
- How many times will this code run? (or, how rich is grandma?)
- Which hook should be used to add an action containing a redirect?
- add_action hook for completely new post?
- Add a new tab to WordPress Plugin install Listing
- Changing Plugin Load Order
- WordPress and PHP Sessions – Security and Performance
- Admin settings update updating every time home page is hit?
- WordPress Hook for user register
- Global Objects and Public Methods
- Using filters and actions for plugin API?
- Init action hook running late after PayPal’s return url?
- Is there widely accepted phpDoc syntax for documenting which hook calls a function?
- Explanation of the “posts_join” and “posts_fields” filter hooks?
- Does using set_transient() function can lead to MySQL problems?
- How do you use the plugin boilerplate loader class to hook actions and filters?
- How Do I Load My Action Earlier Enough?
- Does WP fire delete_post when trashed posts are automatically deleted?
- What’s the earliest point I can get the queried object ID?
- How do I Make a Theme “plugin-ready”?
- Why does do_action pass a blank string as the first parameter if no $arg is set?
- WordPress scheduled task is called but not executed
- How to add a link to WordPress Plugin install Listing
- How to modify post content before writing to database?
- I can’t find where a hook is being defined in a plugin – Easy Digital Downloads
- Call activation hook multiple times
- How do I handle multiple Submit buttons in plugin’s option page?
- What’s the difference between hooks, filters and actions? [duplicate]
- How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
- Two-step login process – Is it possible?
- How to check if certain plugin exists and in expected version
- Plugin development, hooks, generate content
- Is it possible to create an action hook using do_action() within add_action()?
- Sessions not creating correctly in custom function
- How can I hook into existing WordPress Bulk actions?
- Hook for post permalink update
- Dynamically Override Fancy Title
- Get returned variable from a function to add_shortcode function
- Hook event for upload image in the menu
- Help with callback using do_action_ref_array
- Earliest WP Hook to Safely and Reliably Redirect
- Define a function outside a class and call the function using action or filter hook
- Is it possible to modify the media library admin screen?
- What function to hook for changes made in status and visibility of a post
- Create a plugin to change the action to which a function is hooked
- Can I differentiate between “Delete Post Permanently” and “Empty Trash” and do something for each accordingly?
- Synchronize Custom post type tags to WordPress default posts tags
- Adding Automatically To In WordPress Using Filter Referencing?
- Order of Operation for these three hooks
- comment_email hook doesn’t seem to work for comment editor field
- Best Practices for Creating and Handling Forms with Plugins?
- Is there a better way to implement responsive images than what WordPress uses by default?
- Modifying values with add_action to be sent to db
- Woocommerce – Hide a Column in Cart Table
- unable to stop loop when using wp_insert_post with publish_post hook
- save_post hook not triggered in WP v3.6.1
- Hook into theme-switching later than `setup_theme`
- Add tab to profile.php wordpress plugin development
- Is it possible to delay execution of shortcode output callback?
- WordPress Ajax callback function from plugin – OOP
- Hook to get image filename when it is uploaded
- How to display a message about updates in the plugin list
- Why my database table not created on activation?
- How do I change the initial quantity of a product in woocommerce?
- How to delete an user including data from custom database tables
- Act on user meta updated, but only once
- Where and how does WordPress store and use its plugin and hook information?
- Is there any way to allow a plugin to write over a previous version?
- Which are the hooks run before/after when a category’s deletion?
- Should action callbacks start with a verb?
- Execute Hook on the footer or header after activating a plugin
- Why is my javascript not invoked in my hooks except wp_head?
- edit_{$taxonomy} | Hook
- WordPress after content Hook & external template part
- Namespaced action and filter tags
- What filter should I use to insert a button inside on Media>Add New
- best practice for query string values – get_query_var always empty for my value supplied in query string
- get_current_screen() return null
- Modifying meta tags after doing ajax call in plugin
- how to add custom functionality after woocommerce place order button
- .mo translation strings not loading in PHP scripts that handle AJAX calls
- How to use a hook to override an update_post_meta call in a plugin?
- Where to add hooks in a class
- Trying to understand order and frequency of action hooks firing
- How to use output buffering in WordPress hooked functions?
- Conditional hook based on the core function that is calling it
- Adding option to Gallery shortcode
- correct way to call javascript into hook function
- do_action and add_action on two different installed plugins won’t work
- Prefixing plugin hooks (actions/filters) with a wrapper class or functions
- Send push when new post is published in a certain category
- template_redirect or admin-ajax.php?
- WordPress custom taxonomy check box to dropdown
- Force HTTPS 301 redirect with hook
- captcha not working in my custom plugin
- I have 2 plugins using the same wp_login action hook and one is not working
- Add a new confirmation page before saving
- use of do_action() without any functions attached
- How dynamic action login_form_{action} is working