Wordpress入门到优化

必要插件

  • Autoptimize 优化你的网站, 整合并压缩 CSS 和 JavaScript 代码。
  • 云对象存储服务 一些文件资源可以扔上去
  • 注意:插件不宜过多,则情安装,以上两步足可以让网站速度有大的提升,若还不满意可以再加个CDN试一把。

一些必要设置

functions.php文件中进行修改

  • 设置隐藏顶部导航
1
add_filter('show_admin_bar', '__return_false');
  • 设置写文章自动推送百度搜索引擎
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if(!function_exists('Baidu_Submit')){
function Baidu_Submit($post_ID) {
$WEB_TOKEN = 'xxxxx'; //这里请换成你的网站的百度主动推送的token值
$WEB_DOMAIN = get_option('home');
//已成功推送的文章不再推送
if(get_post_meta($post_ID,'Baidusubmit',true) == 1) return;
$url = get_permalink($post_ID);
$api = 'http://data.zz.baidu.com/urls?site='.$WEB_DOMAIN.'&token='.$WEB_TOKEN;
$request = new WP_Http;
$result = $request->request( $api , array( 'method' => 'POST', 'body' => $url , 'headers' => 'Content-Type: text/plain') );
$result = json_decode($result['body'],true);
//如果推送成功则在文章新增自定义栏目Baidusubmit,值为1
if (array_key_exists('success',$result)) {
add_post_meta($post_ID, 'Baidusubmit', 1, true);
}
}
add_action('publish_post', 'Baidu_Submit', 0);
}

function annointed_admin_bar_remove() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);
  • 隐藏wordpress 版权信息
1
2
3
4
5
6
7
8
9

function my_admin_footer_text(){
return '';
}
function my_update_footer(){
return '';
}
add_filter( 'admin_footer_text', 'my_admin_footer_text', 10 );
add_filter( 'update_footer', 'my_update_footer', 50 );
  • 隐藏dashboard面板的一些模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

function remove_dashboard_widgets() {
global $wp_meta_boxes;

//unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']); //快速发布
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); //WordPress China 博客
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); //其它WordPress新闻
//unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']); //近期草稿

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_comments']);//近期评论
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );


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;
}
  • 投稿者图片上传支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if ( current_user_can('contributor') && !current_user_can('upload_files') ){
add_action('admin_init', 'allow_contributor_uploads');
}

function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}

//在文章编辑页面的[添加媒体]只显示用户自己上传的文件
function my_upload_media( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can( 'manage_options' ) && !current_user_can('manage_media_library') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
add_action('pre_get_posts','my_upload_media');

//在[媒体库]只显示用户上传的文件
function my_media_library( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
if ( !current_user_can( 'manage_options' ) && !current_user_can( 'manage_media_library' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter('parse_query', 'my_media_library' );
  • WordPress 给文章编辑器设置默认内容
1
2
3
4
function Bing_default_content(){
return '<h2 style="text-align: center;"><strong>设置默认内容</strong></h2>';
}
add_filter( 'default_content', 'Bing_default_content' );
  • 随机生成特色图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$random = mt_rand(1, 964);
$getImageFile= '2018/randombg/random'.$random.'.jpg';

$wp_filetype = wp_check_filetype( $getImageFile, null );
$attachment_data = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $getImageFile ),
'post_content' => '',
'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment( $attachment_data, $getImageFile, $post_id );

set_post_thumbnail( $post->ID, $attach_id );
}
} //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
  • 移除Head 标签内一些无关项 有助于页面加载速度优化
1
2
3
4
5
6
7
8
9
10
remove_action( 'wp_head', 'feed_links_extra', 3 ); //去除评论feed
remove_action( 'wp_head', 'feed_links', 2 ); //去除文章feed
remove_action( 'wp_head', 'rsd_link' ); //针对Blog的远程离线编辑器接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //Windows Live Writer接口
remove_action( 'wp_head', 'index_rel_link' ); //移除当前页面的索引
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); //移除最开始文章的url
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );//自动生成的短链接
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); ///移除相邻文章的url
remove_action( 'wp_head', 'wp_generator' ); // 移除版本号
  • 禁止wptexturize函数
1
2
3
4
5
6
remove_filter('the_content', 'wptexturize');
remove_action('pre_post_update', 'wp_save_post_revision' );
add_action( 'wp_print_scripts', 'disable_autosave' );
function disable_autosave() {
wp_deregister_script('autosave');
}