Wordpress - 挂钩保存帖子数据 [英] Wordpress - hooking into saving post data

查看:28
本文介绍了Wordpress - 挂钩保存帖子数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果帖子自定义字段设置为true,则这段代码应该设置帖子数据,如果不是,则将帖子状态设置为草稿.该操作需要在 post_save/post_updated 操作之后调用,所以我们的一所大学向我建议了方式(老问题:wordpress 在save_post"操作中将 post_status 设置为draft"):

This piece of code is supposed to set post data if post custom field is set to true, or set post status to draft, if it is not. The action needs to be called after post_save/post_updated action, so one of our colleges suggested me the way (old question: wordpress set post_status as "draft" in 'save_post' action):

add_filter( 'wp_insert_post_data', 'set_post_parameters', 99, 2 );
function set_post_parameters( $data, $postarr ) {
    if ( get_post_meta( $postarr['ID'], '_cs_tweet_ok_status', true ) == 'true' ) {
        $data['post_date']      = get_post_meta( $postarr['ID'], '_cs_system_tweet_date', true );   
        $data['post_date_gmt']  = get_gmt_from_date( get_post_meta( $postarr['ID'], '_cs_system_tweet_date', true ) );
    } else {
        $data['post_status'] = 'draft';
    }
    return $data;
}

它会做它应该做的 - 它将发布日期设置为提供自定义字段(之前保存的)的日期或将帖子设置为草稿.唯一的事情是,当 post-new.php 被调用时,它也会被触发,那些创建无法删除的空"帖子(以防用户没有填写所有数据并且没有保存帖子正确).我应该如何在该函数中创建一个检查",以便它仅在保存或更新帖子时运行?

And it does what it should - it sets published dates to one provided with custom fields (that are saved before) or sets a post to draft. The only thing is, that it is also fired when post-new.php is being called, those creating "empty" posts that cannot be deleted (in case user doesnt fill all data and doesnt save the post correctly). How should I create a "check" in that function, so it only runs when post is being saved or updated?

我在用函数设置那些 post_data 时也有一个小问题:

I have also a small problem with setting those post_data with function:

add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
    if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
        if ( isset($_POST['post_title']) ) :
            $url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
            $json = @file_get_contents($url,0,null,null);
            if ( $json != false ) {
                $json_output = json_decode($json);
                $post_date = date('c', strtotime($json_output->created_at));
                $post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
                update_post_meta( $post_id, '_cs_tweet_content', $json_output->text  );
                update_post_meta( $post_id, '_cs_tweet_date', $post_save_date  );
                update_post_meta( $post_id, '_cs_system_tweet_date', $post_date  );
                update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
            } else {
                update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'false'  );
            }
        endif;
    endif;
}

第一次保存时出现问题.似乎 add_filter( 'wp_insert_post_data', 'set_post_parameters', 99, 2 ); 首先触发,没有看到 custom_meta 并将 post_status 设置为草稿.然后 add_action( 'save_post', 'cs_twitter_save_meta' ); 触发,但第一个脚本已经晚了.

The problem shows while saving for the first time. It seems that add_filter( 'wp_insert_post_data', 'set_post_parameters', 99, 2 ); fires in first place, doesnt see custom_meta and sets post_status to draft. Then add_action( 'save_post', 'cs_twitter_save_meta' ); fires, but it is already to late for the first script.

另一个问题 - 我应该如何解决这种情况,以便首先正确设置所有数据:1. 用户创建新的 custom_post_type.2. 他输入要导入的推文ID,并推送发布.3. 脚本检查是否可以从 twitter api 获取数据4. 如果可以,它会获取所有必要的数据,发布帖子并将发布日期设置为推文的发布日期.5. 如果不是,则显示有关错误的信息并将post_status 设置为草稿.

So another question - how should I slove this situation, so all data is set correctly at first place: 1. User creates new custom_post_type. 2. He enters tweets id to import, and push publish. 3. Script checks if it can fetch the data from twitter api 4. If it can, it fets all necesary data, publishes the post and sets it's publish date to publish date of the tweet. 5. If not, it displays info about an error and sets post_status to draft.

推荐答案

这应该可以解决您的问题.而不是使用 wp_insert_post_data 钩子,只使用 save_post 并使用 $wpdb 来改变数据库条目.

This should resolve Your problem. Instead using of wp_insert_post_data hook, use only save_post and use $wpdb to alter database entry.

add_action( 'save_post', 'cs_twitter_save_meta' );
function cs_twitter_save_meta( $post_id ) {
    if ( isset($_POST['post_type']) && 'tweets' == $_POST['post_type'] ) :
        if ( isset($_POST['post_title']) ) :
            $url = 'https://api.twitter.com/1/statuses/show.json?id='.$_POST['post_title'];
            $json = @file_get_contents($url,0,null,null);
            if ( $json != false ) {
                $json_output = json_decode($json);
                $post_date = date('Y-m-d H:i:s', strtotime($json_output->created_at));
                $post_date_gmt = get_gmt_from_date( $post_date );
                $post_save_date = date('G:i - d M y', strtotime($json_output->created_at));
                update_post_meta( $post_id, '_cs_tweet_content', $json_output->text  );
                update_post_meta( $post_id, '_cs_tweet_date', $post_save_date  );
                update_post_meta( $post_id, '_cs_system_tweet_date', $post_date  );
                update_post_meta( $post_id, '_cs_tweet_user', $json_output->user->screen_name );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'true' );
                global $wpdb;
                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_date = '$post_date', post_date_gmt = '$post_date_gmt' WHERE id = $post_id", $post_id ));
            } else {
                update_post_meta( $post_id, '_cs_tweet_content', 'There is an error with tweet Api. Too many connections in one hour. Try again after 60 minutes' );
                update_post_meta( $post_id, '_cs_tweet_ok_status', 'false'  );
                global $wpdb;
                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'draft' WHERE id = $post_id", $post_id ));
            }
        endif;
    endif;
}

这篇关于Wordpress - 挂钩保存帖子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆