form action wordpress and php

时间:2019-02-25 作者:ZWPDev

我有这个代码可以在wordpress页面中提交一个联系人表单。问题是我没有使用WP api的经验,我找不到一种简单的方式来提交表单。有人能帮我吗?我在用表格action 但这种方式行不通。

<?php get_header(); ?>

<?php 

if( $_POST[\'send_message\'] ){

  $name = filter_var($_POST[\'name\'], FILTER_SANITIZE_STRING );

  $email = filter_var($_POST[\'email\'], FILTER_SANITIZE_EMAIL );

  $message = filter_var($_POST[\'message\'], FILTER_SANITIZE_STRING );

  $to = \'[email protected]\';
  $subject = \'Info request from\'. $name .\'dal sito demo.com\';

  $headers = \'From: [email protected]\' . "\\r\\n" .
  \'Reply-To: \'.$email.\' . "\\r\\n"\';

  mail( $to, $subject, $message, $headers );

}


?>

<!-- page cover -->
<div class="jumbotron jumbotron-fluid" style="background-image:url(\'<?php echo get_the_post_thumbnail_url($post->ID); ?>\');" id="contact-cover-wrapper">
  <div class="container" id="">
    <h1 class="scroll-icon text-center animated infinite slideInDown slow"><i class="fas fa-angle-down"></i></h1>
  </div>
</div>


<div class="container" id="contact-wrapper">
  <div class="row">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
    <div class="col-sm-12 col-md-6 col-lg-6">
      <?php the_content(); ?>
    </div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
    <div class="col-sm-12 col-md-6 col-lg-6">

      <from action="<?php get_permalink( $post->ID ) ?>" method="POST" id="">
        <input type="text" class="form-control" name="name" placeholder="Name" required />
        <br>
        <input type="email" class="form-control" name="email" placeholder="Email" required />
        <br>
        <textarea class="form-control" rows="5" name="message" placeholder="Messagearea>
        <br>
        <button type="submit" name="send_message" class="btn btn-outline-warning text-uppercase"><?php _e(\'Send\') ?></button>
      </form>
    </div>
  </div>
</div>

<?php get_template_part(\'assets/contacts\'); ?>

<?php get_footer(); ?>
编辑:我犯了一个打字错误。而不是form 我写道from. 现在提交按钮工作了,我只需要测试消息是否正确转发。看起来这些消息被托管提供商阻止了,我需要在wp中配置什么吗?谢谢你的帮助。

1 个回复
SO网友:Krzysiek Dróżdż

如果您使用的是WordPress,则不应将POST请求直接发送到当前URL。那里is an API to process such requests.

那么应该怎么做呢?

首先,您应该将请求发送到admin-ajax.php:

因此,与此相反:

<from action="<?php get_permalink( $post->ID ) ?>" method="POST" id="">
您应该有以下内容(另外,“form”一词中有一个拼写错误):

<form action="<?php echo esc_attr( admin_url(\'admin-post.php\') ); ?>" method="POST" id="">  // changed URL
    <input type="hidden" name="action" value="my_custom_form_submit" />  // action, because WP needs that
然后,您必须注册将处理此表单的操作:

function my_custom_form_submit_process_callback() {
    $name = filter_var($_POST[\'name\'], FILTER_SANITIZE_STRING );
    $email = filter_var($_POST[\'email\'], FILTER_SANITIZE_EMAIL );
    $message = filter_var($_POST[\'message\'], FILTER_SANITIZE_STRING );
    $to = \'[email protected]\';
    $subject = \'Info request from\'. $name .\'dal sito demo.com\';

    $headers = \'From: [email protected]\' . "\\r\\n" .
       \'Reply-To: \'.$email.\' . "\\r\\n"\';

    mail( $to, $subject, $message, $headers );  // <- you should use wp_mail instead

    wp_redirect( \'<URL>\' );  // <- replace with proper url that should be visible after sending the form;
    die;
}
add_action( \'admin_post_nopriv_my_custom_form_submit\', \'my_custom_form_submit_process_callback\' );  // for anonymous users
add_action( \'admin_post_my_custom_form_submit\', \'my_custom_form_submit_process_callback\' );  // for logged in users