隐藏了列表和搜索列表的隐藏栏目
隐藏了详情页前一后一文章的隐藏栏目
分类列表的隐藏分类取消了隐藏,因为会导致详情页获取不到完整分类,从而详情页隐藏失效
详情页对属于隐藏分类的文章做隐藏;若属于隐藏分类且设置密码方法,则显示密码
发布更新文章时,属于隐藏栏目,且为公布状态,且无密码的文章设置为私密
<div class="wrap"> <h2><?php _e( 'Ultimate Category Excluder Options', 'ultimate-category-excluder' ); ?></h2> <?php if ( isset( $message ) ) { echo $message; } ?> <p><?php _e( 'Use this page to select the categories you wish to exclude and where you would like to exclude them from.', 'ultimate-category-excluder' ); ?></p> <form action="options-general.php?page=ultimate-category-excluder.php" method="post"> <?php wp_nonce_field( 'uce_form' ); ?> <table class="widefat"> <thead> <tr> <th scope="col"><?php _e( 'Category', 'ultimate-category-excluder' ); ?></th> <th scope="col"><?php _e( 'Exclude from Front Page?', 'ultimate-category-excluder' ); ?></th> <th scope="col"><?php _e( 'Exclude from Feeds?', 'ultimate-category-excluder' ); ?></th> <th scope="col"><?php _e( 'Exclude from All Archives?', 'ultimate-category-excluder' ); ?></th> <th scope="col"><?php _e( 'Exclude from Search?', 'ultimate-category-excluder' ); ?></th> </tr> </thead> <tbody id="the-list"> <?php $args=array( 'hide_empty' => 0, 'order' => 'ASC' ); $cats = get_categories( $args ); $alt = 0; foreach( $cats as $cat ) { ?> <tr<?php if ( $alt == 1 ) { echo ' class="alternate"'; $alt = 0; } else { $alt = 1; } ?>> <th scope="row"><?php echo $cat->cat_name; //. ' (' . $cat->cat_ID . ')'; ?></th> <td><input type="checkbox" name="exclude_main[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_main'] ) ) { echo 'checked="true" '; } ?>/></td> <td><input type="checkbox" name="exclude_feed[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_feed'] ) ) { echo 'checked="true" '; } ?>/></td> <td><input type="checkbox" name="exclude_archives[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_archives'] ) ) { echo 'checked="true" '; } ?>/></td> <td><input type="checkbox" name="exclude_search[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_search'] ) ) { echo 'checked="true" '; } ?>/></td> </tr> <?php } ?> </table> <p><input type="checkbox" name="disable_for_api" id="disable_for_api" <?php if ($options['disable_for_api'] == true) { echo 'checked="true" '; } ?>/><label for="disable-for-xmlrpc"><?php _e( 'Disable category exclusion for authenticated API requests', 'ultimate-category-excluder' ); ?></label></p> <p class="submit"><input type="submit" value="<?php _e('Update', 'ultimate-category-excluder'); ?>" /></p> <input type="hidden" name="ksuce" value="true" /> </form> </div> <?php global $wpdb, $wp_version; echo "<h3>Support</h3>\n"; echo "<p>Please report this information when requesting support.</p>"; echo '<ul>'; echo '<li>UCE version: 1.1</li>'; echo '<li>PHP version: ' . PHP_VERSION . '</li>';// echo '
- MySQL version: ' . mysqli_get_server_info( $wpdb->dbh ) . ' '; echo '
- WordPress version: ' . $wp_version . ' '; $mbctheme = wp_get_theme(); echo "
- Theme: " . $mbctheme->Name . " is version " . $mbctheme->Version." "; $category_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = 'category'" ); echo '
- Number of categories is: ' . $category_count . ' '; if( !defined( 'AUTH_KEY' ) ) { echo "
- AUTH_KEY is empty. Please check your settings in your wp-config.php file. This doesn't affect UCE, but it will help make your WordPress sytem more secure. "; } echo "
function ksuce_process() { //echo '
'; print_r( $_POST );
if( !isset( $_POST[ 'exclude_main' ] ) ) { $_POST[ 'exclude_main' ] = array(); }
if( !isset( $_POST[ 'exclude_feed' ] ) ) { $_POST[ 'exclude_feed' ] = array(); }
if( !isset( $_POST[ 'exclude_archives' ] ) ) { $_POST[ 'exclude_archives' ] = array(); }
if( !isset( $_POST[ 'exclude_search' ] ) ) { $_POST[ 'exclude_search' ] = array(); }
if( !isset( $_POST[ 'disable_for_api' ] ) ) { $_POST[ 'disable_for_api' ] = false; }
$options['exclude_main'] = $_POST[ 'exclude_main' ];
$options['exclude_feed'] = $_POST[ 'exclude_feed' ];
$options['exclude_archives'] = $_POST[ 'exclude_archives' ];
$options['exclude_search'] = $_POST[ 'exclude_search' ];
$options['disable_for_api'] = $_POST[ 'disable_for_api' ];
update_option( 'ksuceExcludes', $options );
$message = "<div class='updated'><p>" . ( __( 'Excludes successfully updated', 'ultimate-category-excluder' ) ) . "</p></div>";
return $message;
}
function ksuce_get_options(){
$defaults = array();
$defaults['exclude_main'] = array();
$defaults['exclude_feed'] = array();
$defaults['exclude_archives'] = array();
$defaults['exclude_search'] = array();
$defaults['disable_for_api'] = true;
$options = get_option( 'ksuceExcludes' );
if ( !is_array( $options ) ){
$options = $defaults;
update_option( 'ksuceExcludes', $options );
}
// If the array exists but doesn't have a value for disable_for_api,
// default to false to ensure existing users have to opt in to new behavior.
if ( array_key_exists('disable_for_api', $options) == false ){
$options['disable_for_api'] = false;
update_option( 'ksuceExcludes', $options );
}
$options['disable_for_api'] = false;
return $options;
}
function ksuce_exclude_categories( $query ) {
if(current_user_can('administrator')){
return $query;
}
$backtrace = debug_backtrace();
$array2[0] = "";
unset( $array2[0] );
$options = ksuce_get_options();
global $ksuce_is_api_query;
//Don't modify the query if request is from authenticated API
if ( ($options['disable_for_api'] == true) && $ksuce_is_api_query && is_user_logged_in() ) { return $query; }
//wp_reset_query();
//error_log( print_r( debug_backtrace(), true ) );
//error_log( 'search for match: ' . in_array_recursive( 'WPSEO_Video_Sitemap', $backtrace ) );
//Exclude calls from the Yoast SEO Video Sitemap plugin
if ( $query->is_home && !in_array_recursive( 'WPSEO_Video_Sitemap', $backtrace ) ) {
$mbccount = 0;
foreach ( $options[ 'exclude_main' ] as $value ) {
$array2[$mbccount] = $value;
$mbccount++;
}
$query->set('category__not_in', $array2);
}
if ( $query->is_feed ) {
$mbccount = 0;
foreach ( $options[ 'exclude_feed' ] as $value ) {
$array2[$mbccount] = $value;
$mbccount++;
}
$query->set( 'category__not_in', $array2 );
}
if ( !is_admin() && $query->is_search ) {
$mbccount = 0;
foreach ( $options[ 'exclude_search' ] as $value ) {
$array2[$mbccount] = $value;
$mbccount++;
}
$query->set('category__not_in', $array2);
}
if ( !is_admin() && $query->is_archive ) {
$mbccount = 0;
foreach ( $options[ 'exclude_archives' ] as $value ) {
$array2[$mbccount] = $value;
$mbccount++;
}
$query->set( 'category__not_in', $array2 );
}
return $query;
}
// 移除分类列表里的某些分类
function exclude_category_from_terms($terms, $taxonomies, $args) {
if (is_admin() || current_user_can('administrator')){
return $terms;
}
$options = ksuce_get_options();
$excludeIds = $options['exclude_main'] ? $options['exclude_main'] : [];
foreach($excludeIds as $k => $id){
$excludeIds[$k] = abs(intval($id));
}
// 修改这里的分类目录名称为需要隐藏的分类目录名称
$category_slug = 'priv';
foreach ($terms as $key => $term) {
if (in_array($term->term_id, $excludeIds) ){
unset($terms[$key]);
// break;
}
}
return array_values($terms);
}
//add_filter('get_terms', 'exclude_category_from_terms', 10, 3);
function in_array_recursive( $needle, $haystack ) {
$found = false;
foreach( $haystack as $item ) {
if ( $item === $needle ) {
$found = true;
break;
} elseif ( is_array( $item ) ) {
$found = in_array_recursive( $needle, $item );
if( $found ) {
break;
}
}
}
return $found;
}
// 详情页上一篇下一篇内容,过滤掉屏蔽栏目
add_filter('get_previous_post_excluded_terms', 'exclude_category_next_previous_links');
add_filter('get_next_post_excluded_terms', 'exclude_category_next_previous_links');
function exclude_category_next_previous_links($excluded_terms)
{
if (!$excluded_terms){
$excluded_terms = [];
}
$options = ksuce_get_options();
$excludeIds = $options['exclude_main'] ? $options['exclude_main'] : [];
foreach($excludeIds as $k => $id){
$excludeIds[$k] = abs(intval($id));
$excluded_terms[] = abs(intval($id));
}
return $excluded_terms;
}
function empty_category_posts($content) {
if (is_admin() || current_user_can('administrator')){
return $content;
}
// 获取当前文章的分类信息
$categories = get_the_category();
// 设置要让其内容为空的分类名称或ID
$options = ksuce_get_options();
$excludeIds = $options['exclude_main'] ? $options['exclude_main'] : [];
foreach($excludeIds as $k => $id){
$excludeIds[$k] = abs(intval($id));
}
foreach ($categories as $category) {
// 检查当前文章的分类是否在列表中
if (in_array($category->term_id, $excludeIds)) {
if(get_post()->post_password){
return $content;
}
return '';
}
}
return $content;
}
add_action('the_content', 'empty_category_posts');
function private_post_some_category( $data, $postarr, $unsanitized_postarr){
// 隐藏的分类
$options = ksuce_get_options();
$excludeIds = $options['exclude_main'] ? $options['exclude_main'] : [];
foreach($excludeIds as $k => $id){
$excludeIds[$k] = abs(intval($id));
}
$categories = $postarr['post_category'];
foreach ($categories as $category) {
// 检查当前文章的分类是否在列表中
if (in_array($category, $excludeIds) && $data['post_status'] == 'publish' && !$data['post_password']) {
$data['post_status'] = 'private';
break;
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'private_post_some_category',10,3);
?>