【问题标题】:Custom WP form which changes user role on submit自定义 WP 表单,在提交时更改用户角色
【发布时间】:2013-09-23 01:13:22
【问题描述】:

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

我真的不知道从哪里开始,如果有人有任何想法或可以帮助我一点,那将是惊人的,因为我真的在努力解决这个问题。

编辑

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

<?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>

【问题讨论】:

  • 开始构建表单及其提交操作。你已经拥有了吗?在表单成功过程中,触发角色更改。
  • @brasofilo 是的,我不确定如何实现角色转换部分。
  • 好吧,发布你的实际代码和你正在做的一些尝试。没有这个问题的问题通常是接近投票的,请参阅How to Ask 和 sscce.org
  • @brasofilo 用我当前的代码更新了它。

标签: php wordpress roles


【解决方案1】:

这将是使用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 );

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 2020-11-07
    • 1970-01-01
    • 2010-12-18
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2018-04-29
    相关资源
    最近更新 更多