关于 wordPress UX Blocks页面生成/更新

https://www.yuque.com/mangofish

老样子,废话少说,先上代码!!!

/**
* [xr_create_uxblocks 创建短代码的主方法]
* @param  [String] $page [要创建的短代码名]
* @return [void]       [无需返回值]
*/
function xr_create_uxblocks_page(String $name='test',$content = '' ,String $type = 'blocks')
{
    global $wpdb;
    $post['post_type'] = $type;
    $post['post_name'] = $name;
    $post['post_status'] = 'publish';
    $post['post_title'] = $name;
    if( !$content )
    {
        $content = 'You text contents';
    }
    $post['post_content'] = $content;
    $sql = 'SELECT * FROM wp_posts WHERE post_type="'.$type.'" AND post_status="publish" AND post_name = "'.$name.'"';
    $res = $wpdb->get_row($sql);
    if( !isset($res->ID))
    {
        wp_insert_post( $post );
    }
}

当然如果需要 更新,移除 等操作于此类似仅仅需要修改部分参数,和函数即可

例如 如下示例 实现  添加+更新 

    /**
    * [xr_create_uxblocks 创建/更新 短代码的主方法]
    * @param  [String] $page [要 创建/更新 的短代码名]
    * @return [void]       [无需返回值]
    */
    private function xr_create_uxblocks_page(String $name='TestTest',$content = '' ,$create_type='insert' ,String $type = 'blocks')
    {
        global $wpdb;
        $post['post_type'] = $type;
        $post['post_name'] = $name;
        $post['post_status'] = 'publish';
        $post['post_title'] = $name;
        if( !$content )
        {
            $content = '这只是一次普通的测试!!!of course !这还不是最后一次';
        }
        $post['post_content'] = $content;
        $sql = 'SELECT * FROM wp_posts WHERE post_type="'.$type.'" AND post_status="publish" AND post_name = "'.$name.'"';
        $res = $wpdb->get_row($sql);

        if( !isset($res->ID) && $create_type=='insert'){
            wp_insert_post( $post );
        }elseif(isset($res->ID) && $create_type=='update'){
            $post['ID'] = $res->ID;
            wp_update_post( $post );
        }elseif($create_type=='No'){
            // 暂不开放
        }

    }

同理 举一反三 

wp_insert_post( $post );
wp_update_post( $post );
wp_delete_post( $post );

......

如有疑问,或更多见解,欢迎指出不足

posted @ 2022-07-09 18:21  芒果鱼  阅读(163)  评论(0编辑  收藏  举报