Solved

Wordpress and php advise about a custom link

Posted on 2013-06-05
14
668 Views
Last Modified: 2013-06-12
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
0
Comment
Question by:ezrhino5151
  • 7
  • 6
14 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 39224134
1)
use Plugin WPML Multilingual CMS

2)
or install WordPress Multisite
0
 

Author Comment

by:ezrhino5151
ID: 39224303
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39224416
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ezrhino5151
ID: 39230140
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39230418
Not sure I'm following what the behavior is.  Can you elaborate?
0
 

Author Comment

by:ezrhino5151
ID: 39235481
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39236383
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
 

Author Comment

by:ezrhino5151
ID: 39239607
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 250 total points
ID: 39240082
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
 

Author Closing Comment

by:ezrhino5151
ID: 39241670
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39241715
Great!  I do have a typo in my if statement (missing the closing } ) so please double check your copy-pasta before going live.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39241718
I swear this website is worth its weight in gold.

Do web sites have weight?
0
 

Author Comment

by:ezrhino5151
ID: 39241730
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
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 39243474
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question