2023-12-25 15次
/* 增加菜单功能
* wp_nav_menu($args);
*/
function caper_register_nav_menus()
{
register_nav_menus(array(
'header_menu' => __('顶部菜单', 'Bing'),
'footer_menu' => __('页脚菜单', 'Bing')
));
}
add_action('init', 'caper_register_nav_menus');
/* 增加友情链接
* wp_list_bookmarks ( $args );
*/
add_filter('pre_option_link_manager_enabled', '__return_true');
/* 缩略图功能
* if (has_post_thumbnail()):
* the_post_thumbnail();
* endif;
*/
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails', array('post', 'page'));
}
// 上传图片重命名
function caper_upload_filter($file)
{
$info = pathinfo($file['name']);
$ext = $info['extension'];
$filedate = date('YmdHis') . rand(10, 99);
$file['name'] = $filedate . '.' . $ext;
return $file;
}
add_filter('wp_handle_upload_prefilter', 'caper_upload_filter');
// 限制上传图片大小
function caper_set_limit_image_size($file)
{
$image_size = $file['size'] / 1024;
$limit = 2048;
$is_image = strpos($file['type'], 'image');
if (($image_size > $limit) && ($is_image !== false)) {
$file['error'] = '上传图片大于2M,请先处理';
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'caper_set_limit_image_size');
// 限制缩略图生成其他尺寸
function caper_disable_image_sizes($sizes)
{
unset($sizes['thumbnail']);
unset($sizes['medium']);
unset($sizes['large']);
unset($sizes['medium_large']);
unset($sizes['1536x1536']);
unset($sizes['2048x2048']);
return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'caper_disable_image_sizes');
add_filter('big_image_size_threshold', '__return_false');
function caper_disable_other_image_sizes()
{
remove_image_size('post-thumbnail');
remove_image_size('another-size');
}
add_action('init', 'caper_disable_other_image_sizes');
// 禁止压缩JGP图片质量
add_filter('jpg_quality', 'caper_high_jpg_quality');
function caper_high_jpg_quality()
{
return 100;
}
// 移除外观主题自定义功能
function caper_remove_customize($caps, $cap){
if ($cap == 'customize') {
return ['do_not_allow'];
}
return $caps;
}
add_filter("map_meta_cap", "caper_remove_customize", 10, 2);
// 禁用中的搜索功能
function caper_disable_wp_search($query, $error = true)
{
if (is_search()) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ($error == true)
$query->is_404 = true;
}
}
add_action('parse_query', 'caper_disable_wp_search');
add_filter('get_search_form', create_function('$a', "return null;"));
// 搜索结果排除页面,只显示文章类型
function caper_advanced_search_query($query)
{
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
//REST OF YOUR CODE
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts', 'caper_advanced_search_query', 1000);
// 只搜索文章的标题
function caper_search_by_title_only($query)
{
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$query->set('post_type', 'post'); // 只搜索文章类型
$query->set('s', get_search_query()); // 使用搜索关键词
$query->set('posts_per_page', -1); // 显示所有匹配结果
$query->set('exact', true); // 精确匹配标题
$query->set('sentence', true); // 匹配整个搜索词
}
}
add_filter('posts_search', 'caper_search_by_title_only', 500, 2);
// 搜索关键词高亮
function caper_search_word_replace($buffer){
if(is_search()){
$arr = explode(" ", get_search_query());
$arr = array_unique($arr);
foreach($arr as $v)
if($v)
$buffer = preg_replace("/(".$v.")/i", "<em>$1</em>", $buffer);
}
return $buffer;
}
add_filter("the_title", "caper_search_word_replace", 200);
add_filter("the_excerpt", "caper_search_word_replace", 200);
add_filter("the_content", "caper_search_word_replace", 200);
// 移除后台“显示选项”和“帮助”选项卡
function caper_screen_options_remove()
{
return false;
}
function remove_wp_help($old_help, $screen_id, $screen)
{
$screen->remove_help_tabs();
return $old_help;
}
add_filter('screen_options_show_screen', 'caper_screen_options_remove');
add_filter('contextual_help', 'remove_wp_help', 999, 3);
// 阻止站内文章互相Pingback
function caper_bing_noself_ping($links)
{
$home = get_option('home');
foreach ($links as $l => $link) {
if (0 === strpos($link, $home)) {
unset($links[$l]);
}
}
}
add_action('pre_ping', 'caper_bing_noself_ping');
// 在媒体库显示文件尺寸及文件大小
function caper_size_column_register($columns)
{
$columns['dimensions'] = __('Dimensions');
return $columns;
}
add_filter('manage_upload_columns', 'caper_size_column_register');
function caper_size_column_display($column_name, $post_id)
{
if ('dimensions' != $column_name || !wp_attachment_is_image($post_id))
return;
list($url, $width, $height) = wp_get_attachment_image_src($post_id, 'full');
$filesize = size_format(@filesize(get_attached_file($post_id)));
echo esc_html("{$width}×{$height} Size={$filesize}");
}
add_action('manage_media_custom_column', 'caper_size_column_display', 10, 2);
// 分类目录url追加/
// 启用下面的文章格式支持:aside, gallery, quote, image, and video
// 移除后台“显示选项”和“帮助”选项卡
// 限制文章标题输入字数
// 新文章自动使用ID作为别名
// 定义内容宽度(如图片),允许添加到网站的任何内容的最大宽度
// 为文章中所有链接添加target="_blank"属性
// 自定义登录界面
// 外链加上nofollow
// 使用<?php post_views('', '次');?>
function record_visitors()
{
if (is_singular()) {
global $post;
$post_ID = $post->ID;
if ($post_ID) {
$post_views = (int) get_post_meta($post_ID, 'views', true);
if (!update_post_meta($post_ID, 'views', ($post_views + 1))) {
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');
function post_views($before = '(', $after = ')', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int) get_post_meta($post_ID, 'views', true);
if ($echo) {
echo $before, number_format($views), $after;
} else {
return $views;
}
}
// 调用<?php echo caper_reading_time();?>
function caper_reading_time()
{
$content = get_post_field('post_content', $post->ID);
$word_count = str_word_count(strip_tags($content));
$readingtime = ceil($word_count / 200);
if ($readingtime == 1) {
$timer = " minute";
} else {
$timer = " minutes";
}
$totalreadingtime = $readingtime . $timer;
return $totalreadingtime;
}