【问题标题】:Redirect on a wordpress action into functions.php将 wordpress 操作重定向到 functions.php
【发布时间】:2016-12-30 20:42:18
【问题描述】:

我无法在 functions.php 中定义的 wordpress action 上实现 redirect。 动作是wpcf7_before_send_mail,这个函数在一个特定的条件下不能发送联系表电子邮件。

add_action( 'wpcf7_before_send_mail', 'my_function' ); function my_function($wpcf7_data) { //redirect to another page }

你能帮帮我吗?我对 wordpress 定制的准备并不充分。

【问题讨论】:

    标签: php wordpress redirect action contact-form-7


    【解决方案1】:

    如果我理解您的问题,您想中止发送邮件并重定向到页面吗?

    我对 Contact Form 7 不是很熟悉,但我认为您应该使用 wpcf7_skip_mail 而不是 wpcf7_before_send_mail,然后使用 wp_redirect() 重定向到另一个页面。所以你的代码应该是这样的:

    function skip_mail($skip_mail, $contact_form) {
    
        // Put in ur condition
        if ($condition) {
    
            // The redirect (look at the attached url)
            wp_redirect($url)
    
            // Return ture if you want to skip sending the mail.
            return true;        
        }
    
        // Return false if you want to send the mail.
        return false;   
    
    }
    
    add_filter('wpcf7_skip_mail', 'skip_mail', 10, 2);
    

    我没有测试过代码,但认为它应该可以工作。

    顺便说一句,这里是另一个使用wp_redirect with CF7.的例子

    【讨论】:

      【解决方案2】:

      由于 CF7 在 javascript 中引入了一些事件,您可以尝试将此 HTML 添加到表单中:

      <script>
          document.addEventListener( 'wpcf7mailsent', function( event ) {
              location = 'http://example.com/';
          }, false );
      </script>
      

      CF7 page 你可以找到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2023-03-03
        • 2016-03-04
        • 1970-01-01
        相关资源
        最近更新 更多