我无法在自定义插件php类中加入wpcf7_mail_sent

时间:2018-11-24 14:05:05

标签: php wordpress contact-form-7

在我的Wordpress插件中,我有一个php类,我想在其中插入联系表7 hook wpcf7_mail_sent。但这对我不起作用。

do_something()不会进入挂接进程。

我想我已经在错误的位置(__construct())注册了该钩子。

能请你帮我吗?

<?php

class MyCF7 {

    public function __construct() {

        add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );

    }

    public function do_something() {

    }
}

扩展:

<?php

/*
Plugin Name: Contact Form 7 - My plugin
Description: My Integration
Version: 1.0
*/

class MyCF7 {

    public function __construct() {
        add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );
    }

    public function activate() {
        // add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );
        // This hook would not be registered in activate() method.
    }

    public function do_something( $contact_form ) {
        error_log( 'do_something was triggered.' );
        // Header( 'Location: https://google.com' );
    }
}

$my_cf7 = new MyCF7();
register_activation_hook( __FILE__, array( $my_cf7, 'activate' ) );

现在我的问题是:提交联系表格后,如何重定向到网址?

1 个答案:

答案 0 :(得分:0)

我从Contact Form 7 redirection插件中得到启发,并解决了我的问题。

当Contact Form 7 Module通过javascript处理表单提交时,我必须添加一个javascript事件侦听器并将js文件排队到我的Wordpress javascript文件中。

<?php

/*
Plugin Name: Contact Form 7 - My plugin
Description: My Integration
Version: 1.0
*/

class MyCF7 {

    public function __construct() {
        $this->plugin_url       = plugin_dir_url( __FILE__ );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend' ) );
    }

    public function enqueue_frontend() {
        wp_enqueue_script( 'wpcf7-redirect-script', $this->plugin_url . 'js/wpcf7-redirect-script.js', array(), null, true );
    }
}

wpcf7-redirect-script.js文件

jQuery(document).ready(function () {
    wpcf7_redirect_mailsent_handler()
})

function wpcf7_redirect_mailsent_handler () {
    document.addEventListener('wpcf7mailsent', function (event) {
        location.href = 'https://google.com'
    }, false)
}

对不起,我应该用另一种方式问这个问题。

相关问题