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
ezrhino5151Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jason C. LevineConnect With a Mentor No oneCommented:
Here is what I am using now: <a style="float:right" href="<?php $newpath = "/zh-tw/".$_SERVER['REQUEST_URI']; ?>"

You may need to go back to basics with your PHP skills before trying to alter things at the code level.  The code I gave you assigned the new path to a variable.  You have to use the echo command to output it:

<a style="float:right" href="<?php echo $newpath; ?>"

Open in new window


You should be putting the second code snippet in your theme's header.php file so it works for all pages on the site.  The above snippet is what builds the link.
0
 
eemitCommented:
1)
use Plugin WPML Multilingual CMS

2)
or install WordPress Multisite
0
 
ezrhino5151Author Commented:
Update:
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>

Open in new window

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>

Open in new window

Am I getting close?
Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jason C. LevineNo oneCommented:
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.
0
 
ezrhino5151Author Commented:
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
0
 
Jason C. LevineNo oneCommented:
Not sure I'm following what the behavior is.  Can you elaborate?
0
 
ezrhino5151Author Commented:
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
0
 
Jason C. LevineNo oneCommented:
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'];

Open in new window


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'];

Open in new window

0
 
ezrhino5151Author Commented:
Jason,
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>

Open in new window

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'];

Open in new window

The site gave me an error referring to the last line of php code, which is empty.
Thanks
Richard
0
 
ezrhino5151Author Commented:
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
0
 
Jason C. LevineNo oneCommented:
Great!  I do have a typo in my if statement (missing the closing } ) so please double check your copy-pasta before going live.
0
 
Jason C. LevineNo oneCommented:
I swear this website is worth its weight in gold.

Do web sites have weight?
0
 
ezrhino5151Author Commented:
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
0
 
Jason C. LevineNo oneCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.