Solved

Wordpress and php advise about a custom link

Posted on 2013-06-05
14
651 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
Comment Utility
1)
use Plugin WPML Multilingual CMS

2)
or install WordPress Multisite
0
 

Author Comment

by:ezrhino5151
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ezrhino5151
Comment Utility
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
Comment Utility
Not sure I'm following what the behavior is.  Can you elaborate?
0
 

Author Comment

by:ezrhino5151
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ezrhino5151
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I swear this website is worth its weight in gold.

Do web sites have weight?
0
 

Author Comment

by:ezrhino5151
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
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…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now