首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从ajax 'wpcf7_mail_sent‘钩子调用公共静态方法

从ajax 'wpcf7_mail_sent‘钩子调用公共静态方法
EN

WordPress Development用户
提问于 2018-04-09 20:34:17
回答 1查看 550关注 0票数 0

我试图从'wpcf7_mail_sent‘钩子中调用操作(从前端提交的表单来自未登录用户)。这不管用。如果我从钩子中调用相同的方法,那么附加到'save_post‘钩子上的方法就能工作。我从管理面板更新帖子。

代码样本:

插件#1 (此部分工作)

代码语言:javascript
运行
复制
function event_updated( $post_ID, $post, $update ) {
/*
    $post_ID(int) - Post ID.
    $post(WP_Post) - Post object.
    $update(bool) - Whether this is an existing post being updated or not.
*/
    $post_type = get_post_type($post_id);

    if ( "event_post_type" != $post_type ) return;

    do_action('update_event_hook', $post_ID, $post);
}
add_action( 'save_post', 'event_updated', 10, 3 );

插件#1 (此部分抛出500个错误)

代码语言:javascript
运行
复制
add_action( 'wpcf7_mail_sent', 'set_event_booked' );
function set_event_booked($contact_form) {
$submission = WPCF7_Submission::get_instance();

if ( $submission ) {
    $posted_data = $submission->get_posted_data();
    $id = $posted_data['post-id'];
    $new_id = $posted_data['event-id'];

    $post = new \stdClass;
    $post->ID = intval($posted_data['post-id']);
    $post->post_type = 'event_post_type';

    update_post_meta($new_id, 'event_booked', 'on');
    try {
        do_action('update_event_hook', $id, $post);
    } catch (Exception $e) {
        $error_message = $e->getMessage();
        $log_message = 'Add reservaion ERROR: '. $error_message;
    }
}

插件#2

代码语言:javascript
运行
复制
class API_sync_class
{
    private static $options;

    public static function Init()
    {
        self::$options = get_option("api_sync");

        if (self::$options['use']) {

            add_action('update_event_hook', array('API_sync_class', 'AddEditReservation'), 10, 2);
        }
    }

    public static function AddEditReservation($post_id, $post)
    {
    API_sync_class::DebugToFile('AddEditReservation method:');

    $type = get_post_type($post_id);
    if ($type && ($type != 'event_post_type')) {
        return;
    }

    $appdata = self::AppData($post);
    API_sync_class::DebugToFile('Data to be sent to API (prepared)');
    API_sync_class::DebugToFile($appdata);

        ....... Some other code of meethod
    }
}
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-04-10 04:14:46

问题解决了。在我的例子中,出现了'PHP致命错误:调用未定义函数get_home_path()‘。是啊。这很简单。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/300269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档