Link to home
Start Free TrialLog in
Avatar of Dan
DanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

using a shortcode inside a link in wordpress

How do I use a short code inside a link, I have a shortcode [ThisURL] and that returns the URL of the current page.

However if I have the code:

<a href="https://www.facebook.com/sharer/sharer.php?u=[ThisURL]">Share on Facebook</a>

This then makes a link to:

https://www.facebook.com/sharer/sharer.php?u=[show-jagged-ThisURL]

and not as I would like:

https://www.facebook.com/sharer/sharer.php?u=my-website.com
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

You don't need a short code.  If you edit the template file just drop this in:

<?php

$pageLink = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$outputURL = '<a href="https://www.facebook.com/sharer/sharer.php?u='.$pageLink.'">Share on Facebook</a>';

echo $outputURL;

Open in new window


It will grab your current page URL and drop it in as needed.
Avatar of Dan

ASKER

Nathan,
Thank you. Sorry for the novice questions but:

Which template file do I change (theme twentysixteen) and what controls when this is added to the page/links.

Thanks
No worries, are you placing this link on a detail page?  Such as a blog detail page?
Avatar of Dan

ASKER

I have a post and I have added a link to share on Facebook, but Facebook wants you to pass the URL to share and that was the issue.
Got it. Can you link me?

You added this in the post content for just this post?  I assume you'll want this on all article pages so your content can be shared right? If that's the case I can point out what template file to modify.
Avatar of Dan

ASKER

No not on all pages I want to be able to chose which pages and which points I add the code is that possible. Can you make the output of a short link go inside a herf?
Avatar of Dan

ASKER

I would also like the post writer to be able to use short codes in URLs they place on the page without having to access the php side of the server. Can it be done?
ASKER CERTIFIED SOLUTION
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial