Reputation:
I am trying to get the website url of the current page on which the person is. I would use it in the social share buttons that I am creating. The websites are all Wordpress websites. I got the following url for getting the current url,
<?php if (is_home()) { echo site_url(); } else { echo the_permalink(); } ?>
The script does not execute and when I click the button, the url does not generate but the php script is displayed in the browser as it is.
Please help out. Thanks
Upvotes: 1
Views: 265
Reputation: 1186
Using Javascript:
var currentURL = document.URL;
alert(currentURL);
Using PHP:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
If you want file name
Use PHP Magic Constants such as __FILE__
Upvotes: 1
Reputation: 11431
You would use
<?php echo $_SERVER['PHP_SELF']; ?>
This gives you the url of the current page. For example index.php
or something similar
Upvotes: 2