0

I want to add image link on wordpress site which redirect the user to the another page of same url but with different prefix. for example page url is mydomain.com/post1234 when user click the image on this page it redirect the user to the url mydomain.com/md/post1234

The following code print the current page url on every post page on site but i want to add "md" prefix in the url

<a href="<?php
$Path=$_SERVER['REQUEST_URI'];
$URI='http://www.example.com'.$Path;
?>"><img src="<?php bloginfo('template_url'); ?>/images/sgxx.png">Click Here</a>

Pls suggest the correct code to do this.

1 Answers1

0

Try this:

<?php
    $domain = get_site_url(); //You can use this or either "http://".$_SERVER[HTTP_HOST];
    $path = $_SERVER[REQUEST_URI];
    $prefix = "/md";
?>
<a href="<?= $domain.$prefix.$path ?>">Link</a>

For more info about $_SERVER, check the link below: http://php.net/manual/en/reserved.variables.server.php

Clyff
  • 4,046
  • 2
  • 17
  • 32
  • Thanks @Clyff It solved my problem. How can i do vice versa. If i want to remove prefix from the link on the prefix page. like hyperlink on the landing page mysite.com/md/post1234 to mysite.com/post1234 – Harendra Singh Jul 14 '15 at 17:10
  • Check this answers for a similiar problem: http://stackoverflow.com/questions/12061528/extract-first-url-segment-from-full-url If you need more help let me know. – Clyff Jul 14 '15 at 17:44
  • I used for vice versa `">Click Here` It worked fine – Harendra Singh Jul 14 '15 at 21:01