简体   繁体   中英

Author for new post in wordpress

I have code to create a new account and a new post. All of this works correctly, but I want the newly account created to be the author for the new post.

How can I do that?

/* Create account */

if (isset($_POST['task']) && $_POST['task'] == 'register')
{
$psw = $wpdb->escape(trim($_POST['psw']));
$email = $wpdb->escape(trim($_POST['email']));

if (email_exists($email))
{
$err = "Email addres exist";
}
  else
{
$user_id = wp_insert_user(array(
'user_pass' => apply_filters('pre_user_user_pass', $psw) ,
'user_login' => apply_filters('pre_user_user_login', $email),
'user_email' => apply_filters('pre_user_user_email', $email),
'role' => 'author'
));
}

...

<form method="post" id="form_anunturi" enctype="multipart/form-data">
  <input type="text" name="title"/>
  <textarea name="desc"></textarea>
  <input type="text" name="email"/>
  <input type="text" name="psw"/>
  <input type="hidden" name="task" value="register"/>
  <input type="submit" value="Submit"/>
</form>

Update

$date = array(
    'post_title' => $title,
    'post_content' => $desc,
    'post_status' => 'publish',
    'post_category' => array(
        $categorie
    ) ,
    'post_author' => $user_ID,
);

$post_id = wp_insert_post($date);

If successful, wp_insert_user() returns the userID of the newly created user. So if the user creation was successful, then just call wp_insert_post, passing the new ID as the post_author :

$user_id = wp_insert_user(array(
    'user_pass' => apply_filters('pre_user_user_pass', $psw) ,
    'user_login' => apply_filters('pre_user_user_login', $email),
    'user_email' => apply_filters('pre_user_user_email', $email),
    'role' => 'author'
));

$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => $user_id
);

wp_insert_post( $my_post );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM