Home  >  Article  >  Backend Development  >  How to change WordPress image address to relative path

How to change WordPress image address to relative path

不言
不言Original
2018-07-09 09:56:523524browse

This article mainly introduces how to modify the WordPress image address to a relative path. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

WordPress image address is edited by default. I use an absolute path, so that when others copy your article to other websites, the pictures can be displayed normally. However, if I want to change the domain name or path of the blog, then the addresses of these pictures will be invalid and cannot be displayed normally. Yousou.com I found two methods on the Internet to solve the problem of using relative paths for WordPress template images. I hope it can help everyone.

1. Modify wp-config.php in the root directory of the

Wordpress theme. This file will only appear after WordPress is installed. Add the following two lines to the file.

define(‘WP_HOME’, ”);
define(‘WP_SITEURL’, ”);

Save, OK! But this modification method can only use the root directory of the user network website, and use the default port 80


If you are not using the root directory of the website, or using a non-80 port, then use the second one Method

2. Open the wp-includes/post.php file, modify the function wp_get_attachment_url (3.7.1 in line 4276) to the following code

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 );
}

Save, OK

In this way, the multimedia file paths in the article logs you write in the future will all use relative paths, and the image addresses will not become invalid after changing the domain name space!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

WordPress supports the implementation of multiple domain name binding/access

The above is the detailed content of How to change WordPress image address to relative path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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