You can log a user in securely by simply calling wp_signon with the proper credentials. This has to be done before any output is produced, so that WP can set the users cookie properly.
Example:
$user = wp_signon(array('user_login'=>'example', 'user_password' = > 'swordfish'));
That will do a proper login from the front end of the site, set cookies, and return the user object. Then you can set the $commentdata[‘user_id’] = $user->ID and return, and the comment will be dealt with accordingly.
An auth failure will make that return a WP_Error object instead, so use is_wp_error on the result to see if it’s a WP_Error or a WP_User object that you get back.
Related Posts:
- Is there a hook that runs after a user logs in?
- Is there a hook before the user is authenticated?
- Run javascript code after wp_login hook?
- How can I do customizations on login, registration and password recovery forms?
- Can’t get ID of post that relates to the comment
- after login that will redirect user role into a page
- Firing a function AFTER redirect
- How to get session token of current user in wp_login hook?
- Custom action on login and “remember me”
- TinyMCE as comment editor – encoding issues
- Is it possible to add the_content filter upon login?
- Can’t get ID of post that relates to the comment
- Updating user meta data from external link, user not logged in
- How to add a new link to the default register form’s footer links?
- Does wp_login hook fire on user registration?
- Changing login url
- WooCommerce: change display order of product short description and price [closed]
- How to remove action hook done in a plugin from functions.php in my theme?
- Hooks for trashing, deleting, saving, restoring custom post type
- Is there a WordPress core & plugins update action hook?
- trigger save_post event programmatically
- is it possible to get the hook name in add_action?
- What hook is executed just after wp_query has been executed?
- How can I hook into the post editor title field in order to change the HTML?
- How to properly test a method that is called by an action hook
- An action hook where a comment meta is updated
- Change “logged in” link in (you must be logged in to post a comment)
- Searching hook to set cookies before and avoid “headers already sent”
- Custom form action hook
- How to send an automated user ‘inactivity’ email?
- wp_redirect() not working on form submission with init hook
- Customizing the default logout page of WordPress
- How do I successfully create a hook for an email override?
- Cast string to number
- Changing “Lost Password Email Link” to custom password reset page
- How to debug user_register hook
- Storing state between hook functions
- Using wp_editor tinyMCE in metabox cause form alert on leaving page
- What’s the difference between “wp” and “wp_loaded”?
- WordPress capabilities and restricted categories access
- When WP_CLI runs WordPress from the command line, are hooks called?
- How to use custom form on add_meta_boxes callback
- Is it possible to switch Gutenberg’s editor styles when document settings change?
- edit_comment_link is not showing for comment author
- schedule event in class oriented plugin
- How can I find the hook I need to rewrite a function?
- Show message in media-new.php
- Hooks are not being removed in child theme
- 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’]?
- post.php AJAX request not being called when publishing post
- 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
- What hook do I use if I want to update a user profile field when a new user is created?
- How to add custom tab page in backend?
- 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)?
- Use has_filter on comment_post
- Send admin to a different login than users?
- Is there a author_update action?
- How to set Media Box form elements default value?
- How to get product information using hooks?
- WordPress comment processing . Default unapproved comments detection before posting
- How to read and write session data?
- Print additional fields in wp_new_user_notification_email_admin
- 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?
- Change user role if it’s orders count more than
- Converting restricted html in comments to bbcode
- set_post_format called after wp_update_post when using bulk edit?
- Using actions, hooks and filters in a non-WordPress page
- Hook into create_category
- Trouble adding data to options table
- How to hook into publish_posttype?
- Fire a hook programmatically
- How do I override the user’s input when updating a custom post type’s permalink?
- $GLOBALS & global doesn’t work [closed]
- Hooks not working on live server
- How can I get the ID before after_setup_theme?
- Hook on opening a media/document
- Hooks to trigger a callback when adding, removing, rearranging or updating a widget in the widget area
- Why does before_delete_post run when opening a post in the backend?