File: /www/wwwroot/1.scey.cn/wp-content/themes/Leading/public/hooks.php
<?php
add_action('after_setup_theme', function(){
add_theme_support( 'woocommerce' ); //声明支持woo
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('post-formats', ['gallery','video','audio']);
register_nav_menus(['main' =>'主菜单', 'mobile-main' =>'手机端主菜单', 'd1' =>'底部菜单①', 'd2' =>'底部菜单②', 'd3' =>'底部菜单③', 'd4' =>'底部菜单④']);
register_sidebar([
'name' => '全站侧栏',
'id' => 'widget_right',
'before_widget' => '<div class="right_news %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="right_news_title"><p>',
'after_title' => '</p></div>'
]);
register_sidebar([
'name' => '文章页侧栏',
'id' => 'widget_post',
'before_widget' => '<div class="right_news %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="right_news_title"><p>',
'after_title' => '</p></div>'
]);
/*
register_sidebar([
'name' => '分类/标签/搜索页侧栏',
'id' => 'widget_other',
'before_widget' => '<div class="right_news %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="right_news_title"><p>',
'after_title' => '</p></div>'
]);
*/
register_sidebar([
'name' => '页面侧栏',
'id' => 'widget_page',
'before_widget' => '<div class="right_news %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="right_news_title"><p>',
'after_title' => '</p></div>'
]);
});
//去除自带小工具
function unregister_widgets() {
unregister_widget("WP_Widget_Pages");//页面
unregister_widget("WP_Widget_Calendar");//文章日程表
unregister_widget("WP_Widget_Archives");//文章归档
unregister_widget("WP_Widget_Meta");//登入/登出,管理,Feed 和 WordPress 链接
unregister_widget("WP_Widget_Search");//搜索
unregister_widget("WP_Widget_Categories");//分类目录
unregister_widget("WP_Widget_Recent_Posts");//近期文章
unregister_widget("WP_Widget_Recent_Comments");//近期评论
unregister_widget("WP_Widget_RSS");//RSS订阅
unregister_widget("WP_Widget_Links");//链接
unregister_widget("WP_Widget_Text");//文本
unregister_widget("WP_Widget_Tag_Cloud");//标签云
//unregister_widget("WP_Nav_Menu_Widget");//自定义菜单
unregister_widget("WP_Widget_Media_Audio");//音频
//unregister_widget("WP_Widget_Media_Image");//图片
unregister_widget("WP_Widget_Media_Video");//视频
unregister_widget("WP_Widget_Media_Gallery");//画廊
}
add_action("widgets_init", "unregister_widgets");
add_filter('pre_option_link_manager_enabled', '__return_true'); /*激活友情链接后台*/
function leading_mobile_menu_classes($classes, $item, $args) {
if($args->theme_location == 'mobile') {
$classes[] = 'swiper-slide';
}
return $classes;}add_filter('nav_menu_css_class','leading_mobile_menu_classes',1,3);
//载入JS\CSS
add_action('wp_enqueue_scripts', function () {
if (!is_admin()) {
wp_enqueue_style('index', get_stylesheet_directory_uri().'/static/css/index.css');
wp_enqueue_style('animate', get_stylesheet_directory_uri().'/static/css/animate.min.css');
wp_enqueue_style('swiper', get_stylesheet_directory_uri().'/static/css/swiper.min.css');
wp_enqueue_style('style', get_stylesheet_directory_uri().'/static/css/style.css');
wp_enqueue_style('fonts', get_stylesheet_directory_uri().'/static/fonts/iconfont.css');
//wp_deregister_script('jquery');
//wp_enqueue_script('jquery', "https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js", false, null);
//wp_deregister_script('jquery-migrate');
//wp_enqueue_script('jquery-migrate', "https://cdn.staticfile.org/jquery-migrate/3.0.1/jquery-migrate.min.js", false, null);
wp_enqueue_script('index', get_stylesheet_directory_uri() . '/static/js/index.js', ['jquery'], '', true);
wp_enqueue_script('lazyload', get_stylesheet_directory_uri() . '/static/js/jquery.lazyload.js', ['jquery'], '', true);
wp_enqueue_script('swiper-animate', get_stylesheet_directory_uri() . '/static/js/swiper.animate.min.js', ['jquery'], '', false);
wp_enqueue_script('swiper', get_stylesheet_directory_uri() . '/static/js/swiper.min.js', ['jquery'], '', false);
wp_enqueue_script('touchslide', get_stylesheet_directory_uri() . '/static/js/touchslide.1.1.js', ['jquery'], '', false);
wp_enqueue_script('fontsize', get_stylesheet_directory_uri() . '/static/js/fontsize.js', ['jquery'], '', false);
if (is_singular() && comments_open() && get_option('thread_comments')){
wp_enqueue_script( 'comment-reply' );
}
if( is_singular() ){
wp_enqueue_style('single', get_stylesheet_directory_uri().'/static/css/single.css');
//wp_enqueue_style('fancybox', 'https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css');
//wp_enqueue_script('fancybox3', 'https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js', ['jquery'], '', true);
wp_enqueue_style('fancybox', get_template_directory_uri().'/static/fancybox3/jquery.fancybox.min.css');
wp_enqueue_script('fancybox3', get_template_directory_uri().'/static/fancybox3/jquery.fancybox.min.js', ['jquery'], '', true);
}
}
});
add_filter('body_class',function ($classes) {
//固定导航
if ( wpjam_get_setting('wpjam_theme', 'navbar_sticky') ){
$classes[] = 'navbar-sticky';
}
//暗黑风格
if(wpjam_get_setting('wpjam_theme', 'dark_mode')){
$classes[] = 'dark-mode';
}
//启用宽版
if(wpjam_get_setting('wpjam_theme', 'width_1500')){
$classes[] = 'width_1500';
}
//前端切换暗黑模式 body 添加class
if(isset($_COOKIE['dahuzi_site_style']) && $_COOKIE['dahuzi_site_style'] == 'dark'){
$classes[] = 'dark-mode';
}
//首页图片轮播
if( wpjam_theme_get_setting('slide_type') == 'img' ){
$classes[] = 'with-hero hero-gallery';
}
return $classes;
});
//删除wordpress默认相册样式
add_filter( 'use_default_gallery_style', '__return_false' );
/* 评论作者链接新窗口打开 */
add_filter('get_comment_author_link', function () {
$url = get_comment_author_url();
$author = get_comment_author();
if ( empty( $url ) || 'http://' == $url ){
return $author;
}else{
return "<a target='_blank' href='$url' rel='external nofollow' class='url'>$author</a>";
}
});
//文章自动nofollow
add_filter( 'the_content', function ( $content ) {
//fancybox3图片添加 data-fancybox
global $post;
$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>(.*?)<\/a>/i";
$replacement = '<a$1href=$2$3.$4$5 data-fancybox="images" $6>$7</a>';
$content = preg_replace($pattern, $replacement, $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
});
//在文章内容的第二段后面插入广告
if(wpjam_theme_get_setting('single_middle_ad')){
add_filter('the_content', function ($content) {
$ad_code = wpjam_theme_get_setting('single_middle_ad');
$ad_code = $ad_code ? '<p style="text-indent:0;">'.$ad_code.'</p>' : '';
$ad_number = wpjam_theme_get_setting('single_middle_ad_number') ?: '4';
if ( is_singular('post') && ! is_admin() ) {
$paragraphs = explode( '</p>', $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= '</p>';
}
if ( $ad_number == $index + 1 ) {
$paragraphs[$index] .= $ad_code;
}
}
$content = implode( '', $paragraphs );
}
return $content;
});
}
add_action('pre_get_posts', function($wp_query) {
global $current_user, $pagenow;
if($wp_query->is_main_query()){
if(is_module('user', 'posts')){
$wp_query->set('ignore_sticky_posts', true);
$wp_query->set('post_type', 'post');
$wp_query->set('post_status', 'any');
$wp_query->set('author', get_current_user_id());
}elseif(is_home()){
$wp_query->set('ignore_sticky_posts',true);
}elseif(is_search()){
if(!is_admin()){
$wp_query->set('post_type', 'post'); //搜索结果排除所有页面
}
}elseif(is_tax('group')){
if(!is_admin()){
$wp_query->set('post_type', 'topic'); //搜索结果排除所有页面
}
}
}
return $wp_query;
});
/* 搜索关键词为空 */
add_filter( 'request', function ( $query_variables ) {
if (isset($_GET['s']) && !is_admin()) {
if (empty($_GET['s']) || ctype_space($_GET['s'])) {
wp_redirect( home_url() );
exit;
}
}
return $query_variables;
});
//删除分类描述P标签 http://www.xintheme.com/wpjiaocheng/49754.html
add_filter('category_description', function($description) {
$description = trim($description);
$description = wp_strip_all_tags($description);
return $description;
});
//去除自带小工具
add_action("widgets_init", function() {
//unregister_widget("WP_Widget_Pages");//页面
//unregister_widget("WP_Widget_Calendar");//文章日程表
//unregister_widget("WP_Widget_Archives");//文章归档
//unregister_widget("WP_Widget_Meta");//登入/登出,管理,Feed 和 WordPress 链接
//unregister_widget("WP_Widget_Search");//搜索
//unregister_widget("WP_Widget_Categories");//分类目录
//unregister_widget("WP_Widget_Recent_Posts");//近期文章
//unregister_widget("WP_Widget_Recent_Comments");//近期评论
unregister_widget("WP_Widget_RSS");//RSS订阅
//unregister_widget("WP_Widget_Links");//链接
//unregister_widget("WP_Widget_Text");//文本
//unregister_widget("WP_Widget_Tag_Cloud");//标签云
//unregister_widget("WP_Nav_Menu_Widget");//自定义菜单
//unregister_widget("WP_Widget_Media_Audio");//音频
//unregister_widget("WP_Widget_Media_Image");//图片
//unregister_widget("WP_Widget_Media_Video");//视频
//unregister_widget("WP_Widget_Media_Gallery");//画廊
});
//直接去掉函数 comment_class() 和 body_class() 中输出的 "comment-author-" 和 "author-"
//避免 WordPress 登录用户名被暴露
function xintheme_comment_body_class($content){
$pattern = "/(.*?)([^>]*)author-([^>]*)(.*?)/i";
$replacement = '$1$4';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('comment_class', 'xintheme_comment_body_class');
add_filter('body_class', 'xintheme_comment_body_class');
//添加@评论
add_filter('comment_text', function($comment_text) {
$comment_ID = get_comment_ID();
$comment = get_comment($comment_ID);
if ($comment->comment_parent) {
$parent_comment = get_comment($comment->comment_parent);
$comment_text = '<a href="#comment-' . $comment->comment_parent . '"><span class="parent-icon">@' . $parent_comment->comment_author . '</a></span> ' . $comment_text;
}
return $comment_text;
});
//自定义分类颜色
function leading_category_color() {
$categories = get_the_category();
$color = get_term_meta($categories[0]->term_id, 'color', true) ?: '#777';
$output = '';
if($categories[0]){
//foreach($categories as $category) {
$output .= 'style="background:'.$color.'"';
//}
echo trim($output);
}
}
include_once TEMPLATEPATH.'/admin/updater/theme-updater.php'; // 授权+在线更新
function xintheme_mobilemenu($loc = 'mobile-main') {
wp_nav_menu(array(
'container' => false,
'menu_id' => '',
'menu_class' => 'sf-mobile-menu clearfix',
'fallback_cb' => 'xintheme_nomobile',
'theme_location' => $loc)
);
}
function xintheme_nomobile() {
echo "<ul class='clearfix'>";
wp_list_pages(array('title_li' => ''));
echo "</ul>";
}