How to modify the WordPress image address as a relative path

Source: Internet
Author: User
This article mainly describes how to modify the WordPress image address as a relative path, has a certain reference value, now share to everyone, there is a need for friends can refer to

WordPress image address In the default edit is the absolute path of use, so that others copy your article to other sites, pictures can also be displayed normally, but if I want to change the domain name of the blog, or path, then the address of these images are all invalid, not normal display. Excellent search network to find two ways to solve the WordPress template image using a relative path , hope to help everyone.

1. Modify the wordpress theme root directory wp-config.php, this file will only appear after the installation of WordPress, in the file to add two lines

Define (' Wp_home ', ");d efine (' Wp_siteurl ',");

Save it, OK! However, this can only be modified by the user Web site root directory, and using the default 80 port

If you are not using the root directory of the Web site, or use a non-80 port, then use the second method

2. Open the wp-includes/post.php file, modify the function Wp_get_attachment_url (3.7.1 in 4276 lines) for 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 fileif (($uploads = Wp_upload_dir ()) && false = = = $uploads [' ERROR ']) {//get upload directoryif (0 = = = Strpos ($file, $uploads [' Basedir ']))//check that the upload base exists in the file lo cation//$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 ') + +); $url = $file _dir. " /wp-content/uploads/". $file; else//$url = $uploads ['BaseURL ']. "/$file"; Its a newly uploaded file, therefor $file are 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 app Ly_filters (' Wp_get_attachment_url ', $url, $post->id);}

Save it, OK.

So you later write the article log inside the multimedia file path is used relative path, replace the domain name space after the image address will not expire!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.