Solved

Wordpress and php advise about a custom link

Posted on 2013-06-05
14
664 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
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.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

839 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