wp自动获取年份

//Auto Copyright function auto_copyright($year = ‘auto’) { if (intval($year) == ‘auto’) { $year = date(‘Y’); } if (intval($year) == date(‘Y’)) { echo intval($year); } if (intval($year) < date('Y')) { echo intval($year) . ' - ' . date('Y'); } if (intval($year) > date(‘Y’)) { echo date(‘Y’); } }

非管理员账户,只能看见自己发布的投稿或者媒体文件

都要添加到”pre_get_posts”这个钩子

仅见投稿:

function posts_for_current_author($query) {
   global $pagenow;

   if( 'edit.php' != $pagenow || !$query->is_admin )
       return $query;

   if( !current_user_can( 'edit_others_posts' ) ) {
       global $user_ID;
       $query->set('author', $user_ID );
   }
   return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

仅见媒体文件:

add_action('pre_get_posts','users_own_attachments');
function users_own_attachments( $wp_query_obj ) {
    $user = wp_get_current_user();
    if ( !current_user_can('administrator') ) {
        global $current_user, $pagenow;
    $is_attachment_request = ($wp_query_obj->get('post_type')=='attachment');
    if( !$is_attachment_request )
        return;
    if( !is_a( $current_user, 'WP_User') )
        return;
    if( !in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) ) )
        return;
    if( !current_user_can('delete_pages') )
        $wp_query_obj->set('author', $current_user->ID );
    return;
    }
}

仅见媒体文件和投稿:

add_action('pre_get_posts', 'query_set_only_author' ); 
function query_set_only_author( $wp_query ) {
 global $current_user;
 $current_user = wp_get_current_user();
 if( is_admin() && !current_user_can('edit_others_posts') ) {
    $wp_query->set( 'author', $current_user->ID );
    add_filter('views_edit-post', 'fix_post_counts');
    add_filter('views_upload', 'fix_media_counts');
 }
}

更改wp后台默认投稿名称

function revcon_change_post_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'News';
    $submenu['edit.php'][5][0] = 'News';
    $submenu['edit.php'][10][0] = 'Add News';
    $submenu['edit.php'][16][0] = 'News Tags';
}
function revcon_change_post_object() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'News';
    $labels->singular_name = 'News';
    $labels->add_new = 'Add News';
    $labels->add_new_item = 'Add News';
    $labels->edit_item = 'Edit News';
    $labels->new_item = 'News';
    $labels->view_item = 'View News';
    $labels->search_items = 'Search News';
    $labels->not_found = 'No News found';
    $labels->not_found_in_trash = 'No News found in Trash';
    $labels->all_items = 'All News';
    $labels->menu_name = 'News';
    $labels->name_admin_bar = 'News';
}
 
add_action( 'admin_menu', 'revcon_change_post_label' );
add_action( 'init', 'revcon_change_post_object' );

wp将自定义分类checkBox更改为radio

wp_terms_checklist() ;

function term_radio_checklist( $args ) {
    if ( ! empty( $args['taxonomy'] ) && $args['taxonomy'] === 'other_tag' /* <== Change to your required taxonomy */ ) {
        if ( empty( $args['walker'] ) || is_a( $args['walker'], 'Walker' ) ) { // Don't override 3rd party walkers.
            if ( ! class_exists( 'Radio_Checklist' ) ) {
                class Radio_Checklist extends Walker_Category_Checklist {
                    public function walk( $elements, $max_depth, ...$args ) {
                        $output = parent::walk( $elements, $max_depth, ...$args );
                        $output = str_replace(
                            array( 'type="checkbox"', "type='checkbox'" ),
                            array( 'type="radio"', "type='radio'" ),
                            $output
                        );

                        return $output;
                    }
                }
            }

            $args['walker'] = new Radio_Checklist;
        }
    }
    return $args;
}
add_filter( 'wp_terms_checklist_args', 'term_radio_checklist' );

规定时间内发布的文章显示new标签

<?php
$pub_date = strtotime(get_the_date("Y-m-d H:i:s")) + 30*24*60*60;
$cur_date = strtotime(date("Y-m-d H:i:s")); ?>
<span class="time <?php if($pub_date >= $cur_date ){ echo 'shownew';}?>"><?php the_time('Y.m.d'); ?></span>

Mange users columns

Add columns:

function new_modify_user_table( $column ) {
    $column['phone'] = 'Phone';
    $column['xyz'] = 'XYZ';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'phone' :
            return get_the_author_meta( 'phone', $user_id );
        case 'xyz' :
            return '';
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );

Remove columns:

$c = array( ‘cb’ => ‘<input type=”checkbox” />’, ‘username’ => __( ‘Username’ ), ‘name’ => __( ‘Name’ ), ’email’ => __( ‘E-mail’ ), ‘role’ => __( ‘Role’ ), ‘posts’ => __( ‘Posts’ ) );

If for example you need to remove E-mail column you should unset a column with ’email’ ID. Let’s go to the final step:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    if (current_user_can('moderator')) {
      unset($column_headers['email']);
    }
 
    return $column_headers;
}

We use manage_users_columns filter to achieve this purpose. This code removes ‘E-mail’ folder for users with ‘moderator’ role. Replace it with your own one.
In order to remove other column replace ’email’ column ID to that column ID: role, posts, etc.

Other variant for the list of roles:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    $roles = array('moderator', 'users-list-viewer');
    foreach($roles as $role) {
        if (current_user_can($role)) {
            unset($column_headers['email']);
            break;
        }
    }
 
    return $column_headers;
}

Another variant for the list of users ID:

add_filter('manage_users_columns','remove_users_columns');
function remove_users_columns($column_headers) {
    global $current_user;
 
    $users = array(27, 70530, 70531, 70532);
 
    if (in_array($current_user->ID, $users)) {
        unset($column_headers['email']);
    }    
 
    return $column_headers;
}

自定义显示文字字数

//custom excerpt length
function wpex_get_excerpt( $args = array() ) {

	// Defaults
	$defaults = array(
		'post'            => '',
		'length'          => 200,
		'readmore'        => false,
		'readmore_text'   => esc_html__( 'read more', 'text-domain' ),
		'readmore_after'  => '',
		'custom_excerpts' => true,
		'disable_more'    => false,
	);

	// Apply filters
	$defaults = apply_filters( 'wpex_get_excerpt_defaults', $defaults );

	// Parse args
	$args = wp_parse_args( $args, $defaults );

	// Apply filters to args
	$args = apply_filters( 'wpex_get_excerpt_args', $defaults );

	// Extract
	extract( $args );

	// Get global post data
	if ( ! $post ) {
		global $post;
	}

	// Get post ID
	$post_id = $post->ID;

	// Check for custom excerpt
	if ( $custom_excerpts && has_excerpt( $post_id ) ) {
		$output = $post->post_excerpt;
	}

	// No custom excerpt...so lets generate one
	else {

		// Readmore link
		$readmore_link = '<a href="' . get_permalink( $post_id ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';

		// Check for more tag and return content if it exists
		if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
			$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
		}

		// No more tag defined so generate excerpt using wp_trim_words
		else {

			// Generate excerpt
			$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );

			// Add readmore to excerpt if enabled
			if ( $readmore ) {

				$output .= apply_filters( 'wpex_readmore_link', $readmore_link );

			}

		}

	}

	// Apply filters and echo
	return apply_filters( 'wpex_get_excerpt', $output );

}