Applying "wp make link relative"
-
As referenced in http://codex.wordpress.org/Function_Reference/wp_make_link_relative.
“Removes the http or https protocols and the domain. Keeps the path ‘/’ at the beginning, so it isn’t a true relative link, but from the web root base.”
I tried adding the function to convert full URL paths to absolute paths by putting the php code ‘<?php wp_make_link_relative( $link ) ?>’ in the Child Theme’s functions.php. But it hasn’t worked…
Where am I supposed to put it exactly to make it work?
I have an Ecommerce Theme loaded with a Child Theme activated.
-
Did you replace $link with the full URL path?
aaaah – guh
let me try
πhmm when I replace $link
with ‘http://www.mydomain.com’
site loads with message:
Parse error: syntax error, unexpected ‘:’ and states line number of functions.php where I added the code.No, it hasn’t worked. I really need it to be spelt out.
Did you mean this:
take <?php wp_make_link_relative( $link ) ?>
replace ‘$link’ with ‘http://www.mydomain.com’
making it: <?php wp_make_link_relative( http://www.mydomain.com ) ?>
and placing that piece of code in first line of function.php file in Child Theme?
If that’s what was meant, then it gives the Parse error: syntax error, unexpected ‘:’
What am I supposed to do with <?php wp_make_link_relative( $link ) ?> to make it work?
I can’t see your functions.php file, but to fix that parse error, you need to add a semi colon after the function call, like this:
<?php wp_make_link_relative( http://www.mydomain.com ); ?>
Do you have any other code in functions.php beside this? If so, then you may not need the opening and closing PHP tags (<?php and ?>). If your functions.php file isn’t large, post the code here or at the pastebin. http://pastebin.com/
Hello,
I did notice that was missing, so I added it, unfortunately it didn’t work. But I will try it again to see what the error says and paste it here.
The functions.php appears to have a lot going on (it’s from a purchased e-commerce theme and it comes with the Child theme for any customisations (I haven’t attempted any changes to the functions.php before now). The main theme also has a functions.php…tried that as well.
Well here’s the functions.php–>http://pastebin.com/2iMS0gMc
(there for one day only)Thanks for your help!
the function expects a string as argument;
try something that can be made ralative:
<?php echo wp_make_link_relative( 'http://www.mydomain.com/wp-content/themes/twentyten/images/header.jpg' ); ?>here is the full code of the function, from /wp-includes/formatting.php – line 2355+:
/** * Convert full URL paths to absolute paths. * * Removes the http or https protocols and the domain. Keeps the path '/' at the * beginning, so it isn't a true relative link, but from the web root base. * * @since 2.1.0 * * @param string $link Full URL path. * @return string Absolute path. */ function wp_make_link_relative( $link ) { return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link ); }I tried putting the code as you set it in the functions.php, namely:
<?php echo wp_make_link_relative( 'http://mydomain.com/wordpress/wp-content/themes/ThemeChild/images/promotions/gift_cards.png' ); ?>It kind of worked but not in the way I want it to: it displays the absolute path on the actual page (read below) instead of in the source code (like I want it to).
/wordpress/wp-content/themes/ThemeChild/images/promotions/gift_cards.png
Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/ThemeChild/functions.php:9) in /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/Theme/header.php on line 2Warning: Cannot modify header information – headers already sent by (output started at /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/ThemeChild/functions.php:9) in /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/Theme/header.php on line 4
Warning: Cannot modify header information – headers already sent by (output started at /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/ThemeChild/functions.php:9) in /websites/123reg/LinuxPackage23/th/ek/ni/mydomain.com/public_html/wordpress/wp-content/themes/Theme/header.php on line 5
What I’m trying to do is set up my template to display the secure padlock when in https SSL mode. As long as the images are linked to http parts of the site, the page is always going to be only partially encrypted and seen as unsecure.
But if the image links are relative or absolute, then the SSL works fine.
I want to find a way to transform all the links into relative paths (in the code), and this piece of code seems like the one to do it. I just don’t know where to stick it π or adapt it to make it work…
the example with the ‘echo’ was only meant to show what the function is doing to a url string;
if you want use ‘wp_make_link_relative()’ in one of your own functions, or in your code, you could either assign it to a variable; for instance:
<?php $modified_link = wp_make_link_relative( 'http://mydomain.com/wordpress/wp-content/themes/ThemeChild/images/promotions/gift_cards.png' ); ?>or use ‘return’ instead of ‘echo’:
<?php return wp_make_link_relative( 'http://mydomain.com/wordpress/wp-content/themes/ThemeChild/images/promotions/gift_cards.png' ); ?>I plugged the code(s) in the ThemeChild functions.php
It doesn’t have any effect on the page that loads…all the links still display full URLs
the code is not a filter, that would filter all possible url; you would need to get the full link url into a variable first, the apply the function, then use the changed url in the code.
do i understand properly:
you want to change the image urls in the posts and pages ?or all urls in the whole web page ?
can you post the html code of a page (from the browser) as it shows now (if you need you can ‘neutralize’ your true identity), and point out the links that you want to show?
I want to change all image urls in the whole web page and all the site.
[huge chunk of code moderated – please use pastebin.com for code]
hmm,
I put the image links I want to be absolute paths in bold.
The topic ‘Applying "wp make link relative"’ is closed to new replies.