如何在联系表单7成功提交后打开弹出窗口

时间:2018-05-18 07:44:39

标签: javascript wordpress contact-form-7 magnific-popup

我正在使用Wordpress和联系表单7。 我需要在成功提交联系表单后使用magnificPopup js显示弹出窗口。 为wpcf7_mail_sent添加了一个钩子,但是如何使用javascript调用popup来显示。

示例:

在functions.php中

add_action( 'wpcf7_mail_sent', 'after_send_mail_from_contact_form' );
function after_send_mail_from_contact_form($contact_form){
  // what to do here 
}
Custom.js文件中的

$('.pay_for_course').magnificPopup({
   disableOn: 700,
   type: 'iframe',
   mainClass: 'mfp-fade',
   removalDelay: 160,
   preloader: false,
   fixedContentPos: false
});

3 个答案:

答案 0 :(得分:3)

试试这个

创建一个bootstrap模式弹出窗口,然后在function.php

中添加此函数
 <?php add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
     <script type="text/javascript">
         document.addEventListener( 'wpcf7mailsent', function( event ) {
         if ( '34' == event.detail.contactFormId ) { // Change 123 to the ID of the form 
         jQuery('#myModal2').modal('show'); //this is the bootstrap modal popup id
       }
        }, false );
         </script>
       <?php  } ?>

OR

add_action('wpcf7_mail_sent', function ($cf7) {
    // Run code after the email has been sent
 $wpcf = WPCF7_ContactForm::get_current();
$wpccfid=$wpcf->id;
    // if you wanna check the ID of the Form $wpcf->id
     if ( '34' == $wpccfid ) { // Change 123 to the ID of the form 
echo '
 <div class="modal fade in formids" id="myModal2" role="dialog" style="display:block;" tabindex="-1">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content no_pad text-center">
         <button type="button" class="close" data-dismiss="modal">&times;</button>
        <div class="modal-header heading">
          <h3 class="modal-title">Message Sent!</b></h3>
        </div>
        <div class="modal-body">

            <div class="thanku_outer define_float text-center">
                <h3>Thank you for getting in touch!</h3>
            </div>
        </div>
        <div class="modal-footer">
        </div>
      </div>

    </div>
    </div>
';
    }
});

答案 1 :(得分:0)

$('。wpcf7-mail-sent-ok')。change(function(){         $('#form_success')。fadeIn(400).addClass('modal-show')。find('。modal-content-block-title')。text($(this).text()); });

答案 2 :(得分:0)

按照下面的步骤在格式正确的对话框中完全自定义联系人form7消息:

步骤1 :使用cPanel转到您的插件文件夹,打开contact-form-7文件夹。

第2步:在contact-form-7中探索包含文件夹并打开contact-form.php文件。

第3步:在上述文件中搜索form_response_output()函数,并将其替换为以下已编辑的函数。

public function form_response_output() {

  $status = 'init';

  $class = 'wpcf7-response-output';

  $role = '';

  $id='cookie-notice-popup';

  $content = '';

  if ( $this->is_posted() ) { 
    $role = 'alert';

    $submission = WPCF7_Submission::get_instance();
    $status = $submission->get_status();
    $content = $submission->get_response();

    switch ( $status ) {
      case 'validation_failed':
        $class .= ' wpcf7-validation-errors alert alert-danger';
        break;
      case 'acceptance_missing':
        $class .= ' wpcf7-acceptance-missing alert alert-warning';
        break;
      case 'spam':
        $class .= ' wpcf7-spam-blocked alert alert-dark';
        break;
      case 'aborted':
        $class .= ' wpcf7-aborted alert alert-info';
        break;
      case 'mail_sent':
        $class .= ' wpcf7-mail-sent-ok alert alert-success';
        break;
      case 'mail_failed':
        $class .= ' wpcf7-mail-sent-ng alert alert-primary';
        break;
      default:
        $class .= sprintf( ' wpcf7-custom-%s',
          preg_replace( '/[^0-9a-z]+/i', '-', $status )
        );
    }
  } else {
    $class .= ' wpcf7-display-none alert alert-info';
  }

  $atts = array(
    'class' => trim( $class ),
    'role' => trim( $role ),
    'id' => trim( $id ),
  );

  $atts = wpcf7_format_atts( $atts );


  $output = sprintf( '<div %1$s>%2$s <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; X </span></div>',
   $atts, esc_html( $content ) );

  $output = apply_filters( 'wpcf7_form_response_output',
    $output, $class, $content, $this, $status );

  $this->responses_count += 1;

  return $output;
}

步骤4 :将下面的CSS添加到样式表文件中。

#notice-popup 
{ 
    border-radius:0px!important; 
    color: #fff !important;
    background: #673ab7;
    left:20% !important; 
    right:20% !important;
}
#cookie-notice-popup
{
    bottom:5em !important;
    position:fixed;
    height:auto;
    z-index:100000;
    font-size:13px;
    line-height:40px;
    text-align:center;
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
    box-sizing:border-box;
}

第5步:如果您要在点击时关闭弹出消息对话框,请将下面的JS添加到您的javascript文件中:

$(document).ready(function(){$(".wpcf7-response-output").on("click",function(){$(this).hide("slow");})})

步骤6 :已完成。

注意:请清除浏览器历史记录以及缓存,Cookie以获取结果。