【问题标题】:Custom post validation and then publishing after validation checked on serverside - wordpress自定义发布验证,然后在服务器端进行验证检查后发布 - wordpress
【发布时间】:2011-07-28 11:33:12
【问题描述】:

基本上,我在 wordpress 上保存自定义帖子。我决定在服务器端自定义验证。下面是我的代码的简化版本。我在这里遇到的问题是-尽管进行了检查,但尽管已填写了该字段,但它似乎仍将自己降级为挂起模式。有什么想法我哪里出错了吗?

add_action ('save_post', 'save_campaigns', 10, 2);

add_action ('save_post', 'completion_validator', 20, 2);

function save_campaigns($pid, $post)
{
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_tatus == 'auto-draft' ) return $pid;
    if ( $post->post_type != 'work' ) return $pid;

    update_post_meta($pid, 'campaign_client', $_POST['campaign_client']);

}

function completion_validator($pid, $post) 
{
    // don't do on autosave or when new posts are first created
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' ) return $pid;
    if ( $post->post_type != 'work' ) return $pid;

    // init completion marker (add more as needed)
    $meta_missing = false;

    // retrieve meta to be validated
    $clientmeta = get_post_meta( $pid, 'campaign_client', true );

    // just checking it's not empty - you could do other tests...
    if ( empty( $clientmeta) or empty( $shortcopymeta) or empty( $longcopymeta) or empty( $gallerymeta) or empty( $thumbnailmeta)) 
    {
        $meta_missing = true;
    }

    // on attempting to publish - check for completion and intervene if necessary
    if ( ( isset( $_POST['publish'] ) || isset( $_POST['save'] ) ) && $_POST['post_status'] == 'publish' ) 
    {
        //  don't allow publishing while any of these are incomplete
        if ($meta_missing == true) 
        {
            global $wpdb;
            $wpdb->update( $wpdb->posts, array( 'post_status' => 'pending' ), array( 'ID' =>$pid ) );
            // filter the query URL to change the published message
            add_filter( 'redirect_post_location', create_function( '$location','return add_query_arg("message", "4", $location);' ) );

        }
    }
}

【问题讨论】:

    标签: php wordpress validation custom-post-type


    【解决方案1】:

    据我所知,当帖子发布时,它调用的操作是publish_post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      相关资源
      最近更新 更多