WordPress实用functions.php代码集
1、实现侧边栏文本工具运行PHP代码add_filter(& 39;widget_text& 39;, & 39;php_text& 39;, 99);function php_text($text) {if (strpo
1、实现侧边栏文本工具运行PHP代码
add_filter('widget_text', 'php_text', 99);function php_text($text) {if (strpos($text, '<' . '?') !== false) {ob_start();eval('?' . '>' . $text);$text = ob_get_contents();ob_end_clean();}return $text;}
2、禁用 Gutenberg(古腾堡) 编辑器
add_filter('use_block_editor_for_post', '__return_false');remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );3、指定分类不在首页显示function exclude_category_home( $query ) {if ( $query->is_home ) {$query->set( 'cat', '-20, -22' ); //你要排除的分类ID}return $query;}add_filter( 'pre_get_posts', 'exclude_category_home' );
4、搜索结果排除指定文章或页面
function wpsite_search_filter_id($query) {if ( !$query->is_admin && $query->is_search) {$query->set('post__not_in', array(40,819));//文章或者页面的ID}return $query;}add_filter('pre_get_posts','wpsite_search_filter_id');
5、搜索结果排除指定类别的文章
function wpsite_search_filter_category( $query) {if ( !$query->is_admin && $query->is_search) {$query->set('cat','-15,-57');//分类的ID,减号表示排除;直接写ID,则表示只在该分类ID中搜索}return $query;}add_filter('pre_get_posts','wpsite_search_filter_category');
6、搜索结果排除所有页面
function search_filter_page($query) {if ($query->is_search) {$query->set('post_type', 'post');}return $query;}add_filter('pre_get_posts','search_filter_page')
7、禁止版本、插件、主题自动更新和检查
// 彻底关闭自动更新add_filter('automatic_updater_disabled', '__return_true');// 关闭更新检查定时作业remove_action('init', 'wp_schedule_update_checks');// 移除已有的版本检查定时作业wp_clear_scheduled_hook('wp_version_check');// 移除已有的插件更新定时作业wp_clear_scheduled_hook('wp_update_plugins');// 移除已有的主题更新定时作业wp_clear_scheduled_hook('wp_update_themes');// 移除已有的自动更新定时作业wp_clear_scheduled_hook('wp_maybe_auto_update');// 移除后台内核更新检查remove_action( 'admin_init', '_maybe_update_core' );// 移除后台插件更新检查remove_action( 'load-plugins.php', 'wp_update_plugins' );remove_action( 'load-update.php', 'wp_update_plugins' );remove_action( 'load-update-core.php', 'wp_update_plugins' );remove_action( 'admin_init', '_maybe_update_plugins' );// 移除后台主题更新检查remove_action( 'load-themes.php', 'wp_update_themes' );remove_action( 'load-update.php', 'wp_update_themes' );remove_action( 'load-update-core.php', 'wp_update_themes' );remove_action( 'admin_init', '_maybe_update_themes' );
8、文章的段落首行自动缩进(空两格)
function Bing_text_indent($text){$return = str_replace('<p', '<p style="text-indent:2em;"',$text);return $return;}add_filter('the_content','Bing_text_indent');
9、去除评论中链接/日期时间/网站
// 删除评论中的链接function mytheme_remove_url($arg) {$arg['url'] = '';return $arg;}add_filter('comment_form_default_fields', 'mytheme_remove_url');// 删除评论日期时间function wpb_remove_comment_date($date, $d, $comment) {if ( !is_admin() ) {return;} else {return $date;}}add_filter( 'get_comment_date', 'wpb_remove_comment_date', 10, 3);// remove comment timefunction wpb_remove_comment_time($date, $d, $comment) {if ( !is_admin() ) {return;} else {return $date;}}//删除评论者的网站function disable_comment_author_links( $author_link ){return strip_tags( $author_link );}add_filter( 'get_comment_author_link', 'disable_comment_author_links' );
10、去掉分类页标题中的“分类”字样
注意:这里去掉的不是链接中的 category
add_filter( 'get_the_archive_title', function ( $title ) {if( is_category() ) {$title = single_cat_title( '', false );}return $title;});
11、评论必须含中文
function refused_spam_comments( $comment_data ) {$pattern = '/[一-龥]/u';if(!preg_match($pattern,$comment_data['comment_content'])) {err('评论必须含中文!');}return( $comment_data );}add_filter('preprocess_comment','refused_spam_comments');
12、屏蔽后台“显示选项”和“帮助”选项卡
function remove_screen_options(){ return false;}add_filter('screen_options_show_screen', 'remove_screen_options');add_filter( 'contextual_help', 'wpse50723_remove_help', 999, 3 );function wpse50723_remove_help($old_help, $screen_id, $screen){$screen->remove_help_tabs();return $old_help;}
13、屏蔽后台页脚版本信息
function change_footer_admin () {return '';}add_filter('admin_footer_text', 'change_footer_admin', 9999);function change_footer_version() {return '';}add_filter( 'update_footer', 'change_footer_version', 9999);
14、隐藏左上角WordPress标志
function hidden_admin_bar_remove() {global $wp_admin_bar;$wp_admin_bar->remove_menu('wp-logo');}add_action('wp_before_admin_bar_render', 'hidden_admin_bar_remove', 0);
15、去除后台标题中的“- WordPress”
add_filter('admin_title', 'zm_custom_admin_title', 10, 2);function zm_custom_admin_title($admin_title, $title){return $title.' ‹ '.get_bloginfo('name');}
16、去除登录标题中的“- WordPress”
add_filter('login_title', 'zm_custom_login_title', 10, 2);function zm_custom_login_title($login_title, $title){return $title.' ‹ '.get_bloginfo('name');}
17、上传文件重命名(按时间)
function git_upload_filter($file) {$time = date("YmdHis");$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);return $file;}add_filter('wp_handle_upload_prefilter', 'git_upload_filter');
18、修改作者存档页面 Author 前缀
add_action('init', 'wenshuo_change_author_base');function wenshuo_change_author_base() {global $wp_rewrite;$author_slug = 'user'; // 更改前缀为 user$wp_rewrite->author_base = $author_slug;}
19、限制文章评论的内容
add_action('preprocess_comment','yundanran_preprocess_comment');function yundanran_preprocess_comment($commentdata){$comment_post_ID = $commentdata["comment_post_ID"];$comment_content = $commentdata["comment_content"];$comment_author = $commentdata["comment_author"];$comment_author_email = $commentdata["comment_author_email"];$comment_author_url = $commentdata["comment_author_url"];$comment_author_IP = $commentdata["comment_author_IP"];$comment_agent = $commentdata["comment_agent"];// 验证合法$nonce = wp_create_nonce($comment_post_ID);if(!isset($_POST["comment_nonce"]) || $_POST["comment_nonce"] !== $nonce){wp_die("请勿恶意评论!");}// 必须输入中文if(!preg_match("/[\x{4e00}-\x{9fa5}]+/u",$comment_content)){wp_die("请说中国话!");}// 是否在黑名单if( wp_blacklist_check($comment_author,$comment_author_email,$comment_author_url, $comment_content, $comment_author_IP, $comment_agent )){wp_die("您已被禁止评论!");}// 是否登录if(!is_user_logged_in()){wp_die("您必须登录之后才能评论!");}// 过滤HTML标签$comment_content=strip_tags($comment_content);return $commentdata;}
20、记录和显示会员最后登录的时间
add_action('wp_login','user_last_login');function user_last_login($login) {global $user_ID;$user = get_user_by('id', $user_ID);update_user_meta($user->ID, 'last_login', date('Y-m-d H:i:s'));}在需要显示的地方插入:global $userdata;get_currentuserinfo();get_user_meta($userdata->ID, 'last_login');
21、文章中的 URL 自动加超链接
add_filter('the_content', 'make_clickable');
22、文章分类按照 ID 排序
function get_post_cat(){global $post;$cat_arr = get_the_category($post->ID);$new_cat = array();$cat_count = count($cat_arr);for($i=0; $i<$cat_count; $i++){for($j=$cat_count-1; $j>$i; $j--){if($cat_arr[$i]->term_id > $cat_arr[$j]->term_id){$tem_cat = $cat_arr[$i];$cat_arr[$i] = $cat_arr[$j];$cat_arr[$j] = $tem_cat;}}}return $cat_arr;}在需要显示的地方插入调用:echo get_post_cat()
23、移除无效的 WP-Cron 定时任务
add_action('wpjam_remove_invild_crons', 'wpjam_remove_invild_crons');function wpjam_remove_invild_crons(){global $wp_filter;$wp_crons = _get_cron_array();foreach ($wp_crons as $timestamp => $wp_cron) {foreach ($wp_cron as $hook => $dings) {if(emptyempty($wp_filter[$hook])){foreach( $dings as $sig=>$data ) {wp_unschedule_event($timestamp, $hook, $data['args']);}}}}}if(!wp_next_scheduled('wpjam_remove_invild_crons')) {wp_schedule_event( time(), 'daily', 'wpjam_remove_invild_crons' );}
24、禁止英文单双引号自动转换成中文全角
remove_filter('the_content', 'wptexturize');
25、禁止评论中的网址自动转换为超链接
remove_filter( 'comment_text', 'make_clickable', 9 );