donglu7816 2011-07-28 11:33
浏览 15

自定义后验证,然后在服务器端验证检查后发布 - wordpress

Basically I am saving a custom post on wordpress. I have decided to custom validation on the server side. Below is a simplified version of my code. The problem I am having here is - despite the check, it seems to relegate itself into pending mode despite the field being filled in. Any ideas where I am going wrong?

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);' ) );

        }
    }
}
  • 写回答

1条回答 默认 最新

  • donglu3087 2012-02-18 06:31
    关注

    When the post is published, the action it calls is publish_post as per my knowledge.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?