I am installing Disqus Comment system on my website. I understand that I can dynamically call the page url or id via php and thus do not need to hardcode the URL or page ID everytime I have a Discus comment box.
This question asked 7 years ago this answer was provided by @Yogus but I couldn’t get it to work.
<HTML>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
include 'testConnection.php';
?>
<b>Here is some more HTML</b>
<?php
//more php code
?>
</body>
</HTML>
I’ve hear that to use PHP I must change my website pages to end in .php rather than .html. I don’t want to do this for every page as it would probably affect SEO and I’ve currently 325 pages.
I’d like to do a call to a PHP file, have it return a variable with the URL or Page ID and then I can plug this into the Discus. I’ve read there is a dedicated variable maybe $_SERVER which returns the page URL or ID.
Any help would be appreciated! Oh, a link to one page where I’ve Discus installed is here {go to bottom}. This page is hard coded in.
coinsandhistory.com/countries/Ancients_Rome/Ancients_48_Rome_Empire_Tetrarchy-Constantine.html
2
Answers
I found a working answer on SO from ~10 years ago. Get current URL with jQuery?
The solution was to use javascript from within the HTML which has queries for such things.
To look at the results I just use the javascript to write out the answer, i.e
Thus php nor an external file wasn't needed. A note the javascript variable assigns MUST be done before the print commands, otherwise Chrome reports an error & nothing is displayed.
You can still keep your pages as HTML in this case.
You can make a call to the PHP page with Javascript, get the result via AJAX, and then use that result in a URL. In this example, I will feed the php a parameter that will determine what the URL will be. The HTML page will use javascript to populate the link in the anchor tag with the data coming from the php file.
example
the PHP code would look something like:
the HTML code would look something like: