Wordpress - 挂钩保存帖子数据

时间:2013-04-03 16:30:02

标签: wordpress wordpress-plugin

如果帖子自定义字段设置为true,则此段代码应设置发布数据,或者如果不是,则将帖子状态设置为草稿。需要在post_save / post_updated行动后调用此操作,因此我们的一所大学向我提出了建议(旧问题: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时,它也会被触发,那些创建“空”的帖子无法删除(如果用户没有填写所有数据并且没有正确保存帖子)。 我该如何在该函数中创建“检查”,因此只有在保存或更新帖子时才会运行?

使用函数设置post_data时我也遇到了一个小问题:

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' );触发,但第一个脚本已经迟到了。

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

2 个答案:

答案 0 :(得分:1)

除非明确点击“保存”,否则您可以尝试阻止该功能运行。

在功能开头添加以下代码段:

// Autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;
// AJAX
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
        return;
// Post revision
if ( false !== wp_is_post_revision( $post_id ) )
        return;

答案 1 :(得分:1)

这应解决您的问题。而是使用wp_insert_post_data挂钩,仅使用save_post并使用$ wpdb来更改数据库条目。

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;
}
相关问题