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 theWordpress 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
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!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
