WordPress默認安裝好之后使用的是絕對路徑,例如:安裝的時候使用的是127.0.0.1的話,那么除了本機能正常訪問之外,其他的ip訪問都會出現問題。如下圖:
因為在安裝后,數據庫中存儲了之前安裝時所使用的ip地址,並且在用戶訪問網站時,調用了該地址。
如果將數據庫中該地址修改為當前服務器的ip地址10.1.1.177,那么頁面能正常訪問。
但是如果WordPress的IP需要不斷的修改或者需要派發多個備份的話,那么就需要框架在調用時,使用相對地址或者能自動識別當前的ip地址。
查看網站的源代碼發現網站includes/option.php文件中存在函數get_option,該函數通過參數名來獲取數據庫中wp_options中的字段。
現在的要求是能使得wordpress使用任何ip都能正常解析,那么可以在這個函數中判斷參數為home或者siteul的請求,如有該請求不去查詢數據庫直接返回需要的地址。
將函數進行如下修改:在函數開始部分加上判斷並設置動態的ip地址,如果網站設置在根目錄則不需要后面的/wordpress目錄,該目錄根據實際情況設置。
修改完成之后再次訪問之前的兩個頁面便能正常解析了。
轉:https://www.jianshu.com/p/992218bf872c
WordPress圖片地址在默認編輯下是使用額絕對路徑,這樣別人復制你文章到其他的網站上,圖片也可以正常顯示,但是如果我想更改博客的域名,或者路徑,那么這些圖片的地址全部失效,不能正常顯示。優搜網在網上找到兩種解決Wordpress模板圖片使用相對路徑的方法,希望可以幫到大家。
1.修改Wordpress主題根目錄下的wp-config.php,這個文件只有在安裝好Wordpress之后才會出現,在該文件中加入一下兩行
define(‘WP_HOME’, ”); define(‘WP_SITEURL’, ”);
保存,OK了!但是這種修改方式是只能用戶網網站根目錄,並且使用默認的80端口
如果你不是用網站的根目錄,或者用非80端口,那就用第二種方法
2.打開wp-includes/post.php文件,修改函數wp_get_attachment_url(3.7.1在4276行)為如下代碼
function wp_get_attachment_url( $post_id = 0 ) { $file_dir=dirname(__FILE__); $server_root=$_SERVER[DOCUMENT_ROOT]; $file_dir=substr($file_dir,strlen($server_root)); $file_dir=substr($file_dir,0,-12); if($file_dir!=”){ $file_dir=’/’.substr($file_dir,1); } $post_id = (int) $post_id; if ( !$post =& get_post( $post_id ) ) return false; $url = ”; if ( $file = get_post_meta( $post->ID, ‘_wp_attached_file’, true) ) { //Get attached file if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location //$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location $url=$file_dir.”/wp-content/uploads/”.$file; elseif ( false !== strpos($file, ‘wp-content/uploads’) ) //$url = $uploads['baseurl'] . substr( $file, strpos($file, ‘wp-content/uploads’) + 18 ); $url=$file_dir.”/wp-content/uploads/”.$file; else //$url = $uploads['baseurl'] . “/$file”; //Its a newly uploaded file, therefor $file is relative to the basedir. $url=$file_dir.”/wp-content/uploads/”.$file; } } if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this. $url = get_the_guid( $post->ID ); if ( ‘attachment’ != $post->post_type || empty($url) ) return false; return apply_filters( ‘wp_get_attachment_url’, $url, $post->ID ); }
保存,OK了
這樣你以后寫的文章日志里面的多媒體文件路徑都是用相對路徑了,更換域名空間之后圖片地址不會失效!
原文:https://blog.csdn.net/lizhi125/article/details/16940617
