自定义WP表单,用于在提交时更改用户角色

时间:2013-09-22 15:29:11

标签: php wordpress roles

基本上我有一个页面只有在用户登录时才显示给用户,它上面有一个自定义联系表格。提交表单后,其用户角色应自动更改为“作者”。

我真的不知道从哪里开始,如果有人有任何想法或者可以帮助我一些令人惊讶的东西,因为我真的很挣扎。

修改

这就是我现在所拥有的,它现在所做的就是发送电子邮件。

<?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim(stripslashes($_POST['name']));
    $email = trim(stripslashes($_POST['email']));
    $subject = trim(stripslashes($_POST['subject']));
    $message = trim(stripslashes($_POST['message']));

    $email_from = $email;
    $email_to = 'email@email.com';

    $body = '<html><head></head><body>' .
            '<b>Name:</b> ' . $name . '<br><br>' .
            '<b>Email:</b> ' . $email . '<br><br>' .
            '<b>Nature of Enquiry:</b> ' . $subject . '<br><br>' .
            '<b>Message:</b> ' . nl2br($message) .
            '</body></html>';

    $headers = "From: " . strip_tags($email_from) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($email_from) . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    if( isset($_POST['checkbox']) ) {
      mail($email_to, $subject, $body, $headers);
      // Change user role to "author
      echo '<p>You are now an author.</p>';
    } else {
      echo '<p>It appears you are a spambot, if this is a mistake please try again and check the "I am not a spambot" checkbox.</p>';
    }
  }
?>

<form method="post" action="#">
  <label for="name">Name</label>
  <input type="text" id="name" name="name" value="<?php echo $name; ?>" required>

  <label for="email">Email Address</label>
  <input type="email" id="email" name="email" value="<?php echo $email; ?>" required>

  <label for="subject">Nature of Enquiry</label>
  <input type="text" id="subject" name="subject" value="<?php echo $subject; ?>" required>

  <label for="checkbox">I am not a spambot</label>
  <input type="checkbox" id="checkbox" name="checkbox">

  <label for="message">Message</label>
  <textarea id="message" name="message" required><?php echo $message; ?></textarea>

  <button type="submit">Submit</button>
</form>

1 个答案:

答案 0 :(得分:0)

这是使用class WP_User and its set_role method

的问题
$user_id = 2;
$role_name = 'author';
$user = new WP_User( $user_id );
$user->set_role( $role_name );