ezrhino5151
asked on
Wordpress and php advise about a custom link
Hello,
I think this is simple issue for a php programmer but is one that escapes me.
I am using a premium wordpress theme. It comes with its plugins and features. However, I need to use a plugin that translates the pages to another language. My current plugin will add a link to the matching translated url top of most free wordpress themes but will not, using this premium theme. I can add the widget to the column and footer but that looks very unappealing.
My best bet is to add a link in the header or page template that allows to echo the page url and add the dir /zh-tw/.
This way each page will have the corresponding translated url:
http://mydomain.com/zh-tw/<?php echo $current_page; ?>
So far the php code I have been able to find does not allow for a dir to be added. I end up with:
http://mydomain.com/page/zh-tw/
or
/zh-tw/http://mydomain.com/page/
or
http://mydomain.com/http://mydomain.com/page/
Thanks
Richard
I think this is simple issue for a php programmer but is one that escapes me.
I am using a premium wordpress theme. It comes with its plugins and features. However, I need to use a plugin that translates the pages to another language. My current plugin will add a link to the matching translated url top of most free wordpress themes but will not, using this premium theme. I can add the widget to the column and footer but that looks very unappealing.
My best bet is to add a link in the header or page template that allows to echo the page url and add the dir /zh-tw/.
This way each page will have the corresponding translated url:
http://mydomain.com/zh-tw/<?php echo $current_page; ?>
So far the php code I have been able to find does not allow for a dir to be added. I end up with:
http://mydomain.com/page/zh-tw/
or
/zh-tw/http://mydomain.com/page/
or
http://mydomain.com/http://mydomain.com/page/
Thanks
Richard
ASKER
Update:
This is what I have so far:
I feel like this should work as well but the $site_url does not return any url in front of the /zh-tw/ directory.
Thanks
This is what I have so far:
<a style="float:right"
href="/zh-tw/<?php echo get_site_url(); ?>" >
<img src="/wp-content/themes/dt-nimble/images/soc-ico/Traditional.png"
width="40" height="20" /></a>
I am able to insert the /zh-tw/ directory in front of the domain and before the echo string but the echo string returns my full domain without the path of the current page.I feel like this should work as well but the $site_url does not return any url in front of the /zh-tw/ directory.
<a style="float:right" href="/zh-tw/<?php $site_url = $blog->path ?>" >
<img src="/wp-content/themes/dt-nimble/images/soc-ico/Traditional.png"
width="40" height="20" /></a>
Am I getting close?Thanks
Short answer: You are kind of close. get_site_url() is the wrong function.
Use $_SERVER['REQUEST_URI'] instead.
Longer answer: this is probably not the best way to build the link if the theme is shared by the Chinese translations as it will build a second link with zh-tw/zh-tw unless you do a bunch of extra code to prevent it. I would urge you to read the plugin documentation as there is almost certainly a way to build the link using hooks and filters.
Use $_SERVER['REQUEST_URI'] instead.
Longer answer: this is probably not the best way to build the link if the theme is shared by the Chinese translations as it will build a second link with zh-tw/zh-tw unless you do a bunch of extra code to prevent it. I would urge you to read the plugin documentation as there is almost certainly a way to build the link using hooks and filters.
ASKER
Jason,
That was great step the function worked but only gives me the domain when i use the "/zh-tw/". Without the /zh-tw/ the full url is displayed. Any advice to fix this would be welcome.
Thanks
Richard
That was great step the function worked but only gives me the domain when i use the "/zh-tw/". Without the /zh-tw/ the full url is displayed. Any advice to fix this would be welcome.
Thanks
Richard
Not sure I'm following what the behavior is. Can you elaborate?
ASKER
Jason,
First thanks for that function. Second, I have translated pages that exist in domain.com/zh-tw/dir1/dir2 /
The current page at any given instance looks like this: domain.com/dir1/dir2/ I want to have a icon at the top of the page that duplicates domain.com/dir1/dir2/ but inserts /zh-tw/ between the domain name and the url path.
The function you suggested, adds the path nicely but does not allow for the /zh-tw/. Alternatively, if there is a way to insert a subdomain for instance zh.mydomain.com before the url?
Thanks
Richard
First thanks for that function. Second, I have translated pages that exist in domain.com/zh-tw/dir1/dir2
The current page at any given instance looks like this: domain.com/dir1/dir2/ I want to have a icon at the top of the page that duplicates domain.com/dir1/dir2/ but inserts /zh-tw/ between the domain name and the url path.
The function you suggested, adds the path nicely but does not allow for the /zh-tw/. Alternatively, if there is a way to insert a subdomain for instance zh.mydomain.com before the url?
Thanks
Richard
The function you suggested, adds the path nicely but does not allow for the /zh-tw/
Sure it does. You just need to be a little creative:
$newpath = "/zh-tw/".$_SERVER['REQUEST_URI'];
To avoid recursively creating /zh-tw/zh-tw/ links:
$mystring = $_SERVER['REQUEST_URI'];
$findme = 'zh-tw';
$pos = strpos($mystring, $findme);
if ($pos === false) {
$newpath = "/zh-tw/".$_SERVER['REQUEST_URI'];
ASKER
Jason,
Thanks again for your patients.
Here is what I am using now.
When I added:
Thanks
Richard
Thanks again for your patients.
Here is what I am using now.
<a style="float:right" href="<?php $newpath = "/zh-tw/".$_SERVER['REQUEST_URI']; ?>" ><img src="/wp-content/themes/dt-nimble/images/soc-ico/Traditional.png" width="40" height="20" /></a>
Still no /zh-tw/ :-(When I added:
$mystring = $_SERVER['REQUEST_URI'];
$findme = 'zh-tw';
$pos = strpos($mystring, $findme);
if ($pos === false) {
$newpath = "/zh-tw/".$_SERVER['REQUEST_URI'];
The site gave me an error referring to the last line of php code, which is empty.Thanks
Richard
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jason,
Thanks for your help on this, you rock! My boss walked in just as I inserted the code correctly and showed showed him that its working now. I swear this website is worth its weight in gold.
Richard
Thanks for your help on this, you rock! My boss walked in just as I inserted the code correctly and showed showed him that its working now. I swear this website is worth its weight in gold.
Richard
Great! I do have a typo in my if statement (missing the closing } ) so please double check your copy-pasta before going live.
I swear this website is worth its weight in gold.
Do web sites have weight?
ASKER
Jason,
Just a follow up. How do you use the second snippet in the header. I know I need to get back to basics on my php. Could really use that advice.
Thanks
Richard
Just a follow up. How do you use the second snippet in the header. I know I need to get back to basics on my php. Could really use that advice.
Thanks
Richard
If you embed the second snippet in the header.php file it should detect if the page being displayed is the translation or not. You can then have it create or not create the link.
use Plugin WPML Multilingual CMS
2)
or install WordPress Multisite