如果你和我一样,使用 WordPress 多站点来做一个 SaaS 平台,比如我做的花生小店,那么就需要对限制每个站点的文章类型,分类模式和媒体素材的数量进行限制:
限制文章类型数
量
以商品文章类型为例,讲一下如何限制文章类型的数量:
function wpm_limit_post_type_number($current_screen){ global $pagenow; if($pagenow == 'post-new.php'){ $post_type = $current_screen->post_type; if($post_type == 'product'){ // 这里可以改成你需要限制的文章类型 $counts = wp_count_posts($post_type); $total = array_sum((array)$counts); if($total > 500){ wp_die('商品上限为:500。'); } } } } add_action('current_screen', 'wpm_limit_post_type_number');
使用上面这段代码之后,再点击新增商品,就会出现:
限制分类模式数量
以商品分类这个分类模式为例,讲一下如何限制分类模式的数量:
function wpm_limit_taxonomy_number($term, $taxonomy){ if($taxonomy == 'product_category'){ // 这里可以改成你需要限制的分类模式 if(wp_count_terms($taxonomy) > 10){ return new WP_Error('too_much_product_category', '商品分类上限为:10。'); } } return $term; } add_filter('pre_insert_term', 'wpm_limit_taxonomy_number', 10, 2);
使用上面这段代码之后,再新增商品分类的时候,就会出现:
限制媒体素材数量
媒体素材是最占资源的,这个运营 SaaS 就不得不限制了:
function wpm_limit_attachement_count($file){ $counts = wp_count_posts('attachment'); $total = array_sum((array)$counts); if($total > wpm_ShopCountLimit::get_limit('attachment')){ $error = wpm_ShopCountLimit::get_error('attachment'); $file['error'] = $error->get_error_message(); } return $file; } add_filter('wp_handle_upload_prefilter', 'wpm_limit_attachement_count');
使用上面这段代码之后,在后台再上传图片的时候,就会出现:
当然运营 SaaS 还有其他地方和做一个单独博客是不一样的,今天主要就是对资源限制最一些粗浅的介绍,你对 SaaS 平台技术和运营有什么看法,可以一起来探讨。