国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home CMS Tutorial WordPress How to optimize wordpress blog

How to optimize wordpress blog

Nov 15, 2019 am 10:52 AM
wordpress

How to optimize wordpress blog

wordpress博客怎么優(yōu)化?

我的優(yōu)化步驟是:

1.壓縮CSS和JS文件,并修改一部分插件,優(yōu)化頁面的載入進(jìn)程

一般需要另外加載JS或者CSS的插件都會(huì)存在add_action(”wp_head”,”xxxx”)或者add_action(”wp_footer”,”xxxx”)這兩句代碼,目的是把自己的腳本或者樣式插入到主題的wp_head()和wp_footer()處,使插件可以正常工作(那些反映說插件激活了但看不到效果的人注意了,我觀察到相當(dāng)一部分人所使用的主題不能正常加載插件的腳本,缺的就是這兩個(gè)函數(shù)了)。

下面轉(zhuǎn)回正題。我們需要優(yōu)化載入進(jìn)程,也就是流量整形,把CSS文件移到head里(這點(diǎn)100%的插件都能做到,不用擔(dān)心),把JS文件放在頁面最后。我們可以把a(bǔ)dd_action(xxxx)這句刪掉,然后手工把所需的文件插入到主題模板里。

推薦:《WordPress教程

2.壓縮CSS和JS,縮短文件的加載時(shí)間

經(jīng)常用jQuery寫腳本的人應(yīng)該比較清楚,未壓縮版的jQuery庫(kù)(1.3.2)大小為120K左右,但min版的只有56K。因?yàn)閖Query庫(kù)min版經(jīng)過YUI Compressor壓縮,去除了代碼里的注釋、無用的空格和換行符。我們也可以用YUI來壓縮一下自己的腳本,壓縮率能達(dá)到30%~70%。由于軟件版的YUI安裝過程比較復(fù)雜,這里有個(gè)在線版。

而CSS的壓縮就比較簡(jiǎn)單,就是去除換行符、空格和注釋,大家可以用在線工具壓縮一下。但主題的style.css頭部被注釋掉的主題信息不能刪掉,否則可能導(dǎo)致主題不正常。

對(duì)于CSS的壓縮,很多人用的PHP壓縮。編寫名為style.css.php的文件,內(nèi)容如下:

代碼如下:

if ( extension_loaded('zlib') and !ini_get('zlib.output_compression') and ini_get('output_handler') != 'ob_gzhandler' and ((version_compare(phpversion(), '5.0', '>=') and ob_get_length() == false) or ob_get_length() === false) ) {
ob_start('ob_gzhandler');
}else{
ob_start();
}
//檢查是否含有Gzip相關(guān)模塊,有的話就采用Gzip傳輸,如果主機(jī)有Apache mod_deflate.c或其它等效模塊的話,可以不寫這段
@header("Cache-Control: public");
@header("Pragma: cache");
//緩存文件
$expiresOffset = 3600*24*365;
@header( "Vary: Accept-Encoding" );
@header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
//設(shè)置緩存時(shí)間
@header('Content-Type: text/css');//聲明文件類型
$cssdata = file_get_contents('style.css');//讀取style.css的內(nèi)容
$cssdata = preg_replace(array('/\s*([,;:\{\}])\s*/', '/[\t\n\r]/', '/\/\*.+?\*\//'), array('\\1', '',''), $cssdata);//清除注釋和換行符等
echo $cssdata;//輸出代碼

把這個(gè)文件放在主題文件夾里,并把主題CSS的加載代碼改為

代碼如下:

<link rel="stylesheet" href="<?php bloginfo(&#39;stylesheet_directory&#39;); ?>/style.css.php" type="text/css" media="all" />
</link>

當(dāng)然JS文件也可以用PHP進(jìn)行優(yōu)化,但由于通常情況下加載的JS文件比較多,我通過另一個(gè)文件來間接加載JS。建立一個(gè)名為js.php的文件,內(nèi)容如下:

代碼如下:

if ( extension_loaded(&#39;zlib&#39;) and !ini_get(&#39;zlib.output_compression&#39;) and ini_get(&#39;output_handler&#39;) != &#39;ob_gzhandler&#39; and ((version_compare(phpversion(), &#39;5.0&#39;, &#39;>=&#39;) and ob_get_length() == false) or ob_get_length() === false) ) {
ob_start(&#39;ob_gzhandler&#39;);
}else{
ob_start();
}
//同樣是Gzip壓縮的語句
$js_folder = "js/";//JS文件所在目錄,相對(duì)路徑
$js_src = urldecode( htmlspecialchars( $_GET[&#39;src&#39;] ) );//獲取JS文件名
$js_file = $js_folder.$js_src;//JS文件位置
@header("Cache-Control: public");
@header("Pragma: cache");
//緩存文件
$expiresOffset = 3600*24*365;
@header( "Vary: Accept-Encoding" ); // Handle proxies
@header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
//設(shè)置緩存時(shí)間
@header(&#39;Content-Type: text/javascript; charset: UTF-8&#39;);//聲明文件類型
$jsdata = file_get_contents($js_file);
echo $jsdata;
//輸出內(nèi)容

把這個(gè)文件放在主題目錄下,在主題文件夾里建立一個(gè)JS文件夾,把所需的JS文件都放到這個(gè)文件夾里。改寫一下主題,用以下方式加載JS文件:

代碼如下:

<script type="text/javascript" src="<?php bloginfo(&#39;stylesheet_directory&#39;); ?>/js.php?src=library.js"></script>

如果你的主機(jī)有Apache mod_deflate.c模塊,基本上可以忽略上面的方法,因?yàn)橹恍枰?htaccess文件里加入以下代碼就可以實(shí)現(xiàn)全站Gzip傳輸了。而且壓縮率更高。

代碼如下:

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-httpd-php application/x-javascript
</ifmodule>

3.整合CSS和JS文件

經(jīng)過上面一番折騰以后,其實(shí)頁面載入速度已經(jīng)快很多了,但速度是沒有止境的,我們追求更快。搞無可搞以后,只能從減少HTTP請(qǐng)求數(shù)下手了,這一步的目的盡量整合所有的CSS和JS。

整合CSS比較簡(jiǎn)單,用各種主流瀏覽器測(cè)試幾個(gè)頁面,沒發(fā)現(xiàn)框架錯(cuò)位現(xiàn)象,把相關(guān)的CSS里的代碼粘貼到style.css里,并把相關(guān)的CSS-image也復(fù)制到主題目錄下,修改一下CSS里的圖片路徑就行了。

JS的整合方法則復(fù)雜點(diǎn),要搞清楚那些腳本需要在對(duì)象加載前加載,否則是無效的,并且要注意不同插件的JS沖突問題。

整合完CSS和JS后,重返第一步,把插件里加載腳本和樣式的語句刪掉。

The above is the detailed content of How to optimize wordpress blog. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

10 latest tools for web developers 10 latest tools for web developers May 07, 2025 pm 04:48 PM

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to add your WordPress site in Yandex Webmaster Tools How to add your WordPress site in Yandex Webmaster Tools May 12, 2025 pm 09:06 PM

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

How to set, get and delete WordPress cookies (like a professional) How to set, get and delete WordPress cookies (like a professional) May 12, 2025 pm 08:57 PM

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

How to fix HTTP image upload errors in WordPress (simple) How to fix HTTP image upload errors in WordPress (simple) May 12, 2025 pm 09:03 PM

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

How to register a wordpress account How to register a wordpress account Apr 20, 2025 am 11:45 AM

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.

See all articles