Looking at your if-else block, I see there’s a $post_id
variable, so if it’s the ID of the post that you want to assign to the user you’re creating, then you could just call wp_update_post()
in your else
block, i.e. right after you create the user.
So just replace the wp_insert_user($userdata);
in your code with this:
$user_id = wp_insert_user($userdata);
if ( ! is_wp_error( $user_id ) ) {
wp_update_post( array(
'ID' => $post_id,
'post_author' => $user_id,
) );
}
Related Posts:
- get author of published post
- Using wp_mail to send email?
- How to show comments from different Plugins to same post type?
- How to check if `comment_meta` exists before inserting the comment?
- How to get the `comment_post_ID`?
- Send user activation email when programmatically creating user
- How to check username/password without signing in the user
- Add Custom User Capabilities Before or After the Custom User Role has Been Added?
- Writing a plugin that notify my friends of new post that mentions(@) them
- WordPress REST API call generates nonce twice on every call
- Custom user profile, registration, login page with theme
- wp_mail not recognizing cc and bcc headers
- How to use WP default post list tables in a plugin?
- How to add quick edit and bulk edit fields to users admin section
- Is there a faster way than wp_insert_post to add content to a blog
- Generating User(s) with Settings API
- WordPress admin deleted user details not removed in database. How to delete WordPress Users from Database
- Print WordPress username id inside JavaScript
- Function Get User Object By Username?
- Check if someone is editing a post (this content is currently locked)
- Use wp_set_post_terms() instead of wp_insert_post()
- Modify users.php page to create page/post on button clicked
- Subscribe to author?
- Comments do not respect display_name setting, how to make plugin to overcome this
- WordPress is automatically linking plain text email addresses
- update post meta for checkbox in the admin when inserted in the front-end
- How to filter users list on user_status field with get_users()
- Frontend language per user
- Set page template programaticlly in plugin
- How can I pass get_the_author_meta(‘user_email’) through the REST API?
- Display custom fields in frontside user profile
- wp_mail() headers multipart/alternative
- global $current_user not current user
- Display list of uploaded images, filtered by user under a specific user group
- What is the proper way to get logged in user id in a plugin?
- Warning: include(): https:// wrapper is disabled in the server configuration by allow_url_include=0
- How to build a fool proof AdSense revenue sharing model?
- Hide custom post type by user roles
- How do I have now a duplicated user entry if this is not allowed (and I cannot replicate it)?
- Remove all removed pages from wordpress
- How to allow only two (or more) users access to a plugin
- First and last name fields not filled when using wp_insert_user
- How to add a hyperlink to the email content in WordPress?
- Wp-admin Custom User Management
- why do I have to use required parametres?
- Associate multiple email addresses with the same user account, so they can log in with either
- Multiple array for post_content on plugin activation
- Update a user profile via frontend
- What is the way to hook all wordpress email
- Best practice for Designing a Plugin with this scenario
- Adding another field to user table
- Change author permalink to external URL
- how to get the top 10 popular blogs
- Plugin to create Posts and Forums then choose category and parent forum
- How can I change my assigned user role in WordPress 3.5.1?
- How to relate the Category to user?
- Serial Number from custom table not appear in woocommerce_email_before_order_table action
- Creating posts with links from a txt file
- wp_new_user_notifications to notify user and admin about new user registration
- Posts form with AJAX request – Plugin development
- Adding a dropdown on the user admin
- Custom plugin foreach wp_insert_post not working
- Allowing duplicating users with same user_login and user_email
- Why User_login key doesn’t work with wp_update_user()
- How to grab data after wp user search is complete
- Sanitize WordPress Array Input?
- How to prevent wp_insert_post from creating a new post every second?
- Email content as comment
- How to prevent users from deleting their accounts?
- User Registration Moderate
- How to retrieve custom profile fields associated with different users
- WordPress mails being sent from @locahost and being rejected
- Upgrade routine: Interaction with user confirmation
- Getting duplicate page when activating my plugin
- Multiple Users Logged In Causing Incorrect Account Returned
- Add Page With Parent ID
- Yet another wp_insert_post infinite loop. What is wrong?
- Create pages for authors
- first_name property missing inside register_user action hook
- How to set/change another post author by custom fields or something else?
- user can login from single account detail from multiple locations(computer) at the same time [closed]
- Create Unique and Customized User ID for Website Members in WordPress
- Add self-closing shortcode button to TinyMCE in WP 4.6
- Help adding image upload functionality to widget
- Is It Always a Best Practice to Decouple the Frontend from the Admin Area When Developing a WordPress Application?
- WordPress clean internationalization for menus?
- W3 Total Cache JS and css Minify folder are empty
- How to create custom settings page for custom plugin
- Taxonomies within plugin invalid
- Make Database query only when option is updated
- Service Worker Uncaught (in promise) DOMException
- Ajax action has 200 status but response of No response data available for this request
- Google Web Core Vitals – management, how to in wordpress and advice
- Adding customs fields on each product on the cart
- Why would one specify multiple arguments when using get_previous_posts_link() which takes only one parameter?
- I have a plugin where in I have to change the title of the page dynamically by sending parameter to the_title filter
- How can I map a specific URL pattern to a plugin for processing
- Change the class of wordpress menu
- How to change url’s in WordPress when changing domain [duplicate]
- Is wordpress plugin development active? [closed]