Functions.php代码解析分享

<?php
/*
你可以把本来要写在 function.php 中的代码写在这里,
而无须担心更新主题时 function.php 被覆盖。
*/

//加载的css和js后面的版本号的问题
if(!function_exists('cwp_remove_script_version')){
function cwp_remove_script_version( $src ){ return remove_query_arg( 'ver', $src ); }
add_filter( 'script_loader_src', 'cwp_remove_script_version' );
add_filter( 'style_loader_src', 'cwp_remove_script_version' );
}
//WP前台顶部清理
function cwp_header_clean_up(){
if (!is_admin()) {
foreach(array('wp_generator','rsd_link','index_rel_link','start_post_rel_link','wlwmanif
est_link') as $clean){remove_action('wp_head',$clean);}
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
foreach(array('single_post_title','bloginfo','wp_title','category_description','list_cat
s','comment_author','comment_text','the_title','the_content','the_excerpt') as $where){
remove_filter ($where, 'wptexturize');
}
/*remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );*/
wp_deregister_script( 'l10n' );
}
}
//移除某些WP自带的小工具
function coolwp_remove_meta_widget() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
//unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
// unregister_widget('WP_Widget_Search');
// unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Nav_Menu_Widget');
/*register my custom widget*/
}
add_action( 'widgets_init', 'coolwp_remove_meta_widget',11 );

//移除WP为仪表盘(dashboard)页面加载的小工具
function cwp_remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'cwp_remove_dashboard_widgets',11 );

//Bonus:加载自定义jQuery库的链接
/*Using SinaApp jQuery CDN*/
function cwp_modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js',
false, '1.9.1');
wp_enqueue_script('jquery');
}
}
add_action('init', 'cwp_modify_jquery');

/**
* WordPress 后台禁用Google Open Sans字体,加速网站
*/
add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
$translations = 'off';
}
return $translations;
}