WordPress:当表单适用于登录用户时,如何处理未登录用户的发布请求

时间:2017-10-02 08:54:06

标签: php wordpress

我在single-$ post_type.php中有一个表单,它将数据发送到我的主题函数文件。当用户登录时,表单会发布。

但是如果用户没有登录,则表单失败并重新加载主页。

简而言之,表格是一份工作申请表。表单将所有数据发送到函数,该函数创建两封电子邮件并将它们发送给HR和候选者。然后重定向到感谢页面。

FUNCTIONS.PHP中的表格处理器

add_action( 'admin_post_nopriv_email_appliaction_form', 'processform' );
add_action( 'admin_post_email_appliaction_form', 'processform' );

function processform() {
  //Handle the CV Upload
  require_once( ABSPATH . 'wp-admin/includes/image.php' );
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
  require_once( ABSPATH . 'wp-admin/includes/media.php' );         

  $attachment_id = media_handle_upload( 'cv', $_POST['post_id'] );
  $attachments = get_attached_file( $attachment_id );

  //Get variables for emai content
  $jobTitle = $_POST['jobTitle']; 
  $jobLocation = $_POST['jobLocation'];
  $fullName = $_POST['fullName'];
  $email = $_POST['email'];
  $telephone = $_POST['tel'];
  $quickmessage = strip_tags ( $_POST['message'] );

  $to = $_POST['email'];
  $admin_email = get_option( 'admin_email' );

  //Build the message to the candidate
  $messageCandidate = email_header();
  $messageCandidate .= candidate_application($jobTitle, $jobLocation);
  $messageCandidate .= reason_jobapplication();
  $messageCandidate .= email_footer();

  //Build the message to the head office
  $messageOffice = email_header();
  $messageOffice .= new_candidate_application($jobTitle, $jobLocation, $fullName, $email, $telephone, $quickmessage);
  $messageOffice .= reason_new_jobapplication();
  $messageOffice .= email_footer();


  $headers[] = 'Content-Type: text/html; charset=UTF-8';
  $headers[] = 'From: Greenfield IT Recruitment <info@greenfield-it.co.uk>';

  $mailCandidate = wp_mail($to, 'Application successfull', $messageCandidate, $headers );
  $mailoffice = wp_mail($admin_email, 'New Candidate Application', $messageOffice, $headers, array($attachments) );

  if ($mailCandidate) {
      wp_delete_attachment( $attachment_id, true );
      wp_redirect( get_site_url().'?p=227');
      exit;
  }



}

单页中的HTML表格 - $ POST_TYPE.PHP

   <form id="jobApplication" name="jobApplication" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" enctype="multipart/form-data" onsubmit="return validation(this)">
        <div class="col-sm-6">
            <input type="text" name="fullName" placeholder="Full Name: (required)" required>
            <input type="email" name="email" placeholder="Email: (required)" required>
            <input type="tel" name="tel" placeholder="Telephone: (required)" required>
            <textarea name="message" placeholder="Quick message"></textarea>
            <span>Please upload a copy of your cv in .doc or .docx format. We will accept a pdf.</span>
            <span><input type="file" name="cv" accept="application/pdf" required></span>
        </div>
        <div class="col-sm-6">
            <p><strong>Protecting your data</strong><br>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            <input type="hidden" name="action" value="email_appliaction_form">
            <input type="hidden" name="jobTitle" value="<?php the_title(); ?>">
            <input type="hidden" name="jobLocation" value="<?php echo $meta['city'][0]; ?>">
            <input type="hidden" name="post_id" id="post_id" value="<?php the_id(); ?>" />
            <input type="submit" class="btn" name="submit">
        </div>
    </form>

如果需要,很高兴在函数中发布其余代码。

由于

2 个答案:

答案 0 :(得分:0)

这不起作用的原因是因为我添加的主题内部有一个功能,只限制管理员访问wp-admin。这要求您登录,否则它将重定向主页。 我还没找到解决方法。

add_action( 'init', 'blockusers_init' );
 function blockusers_init() {
  if ( 
  is_admin() && ! current_user_can( 'administrator' ) && 
  ! ( defined( 'DOING_AJAX' ) && DOING_AJAX )  &&
  ! ($_SERVER['REQUEST_METHOD'] === 'POST') )
   {
   wp_redirect( home_url() );
   exit;
 }
}

答案 1 :(得分:0)

我遇到了这个问题,并且使用了与您使用的完全相同的“ blockusers_init”代码段(我似乎找不到它的原始来源)。无论如何,我添加了几行内容来使admin_post文件通过(受this启发)...

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
    $file = basename($_SERVER['PHP_SELF']);
    if ( is_admin() && ! current_user_can( 'administrator' ) && 
      ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) &&
      ( $file != 'admin-post.php')) {
        wp_redirect( home_url() );
        exit;
    }
}