How to add or generate a unique id for the body using url
As a website developer, it is very useful for me to create an id for each pages of the website that I am creating. Creating pages with unique styles from other pages will need a unique id to target a specific page. For a solution we are going to generate an id from our unique page urls of each pages on our website. This may apply to other cms using php language as we will be using php in this example.
Problem: How to generate or create a unique id for each pages?
[html]<body id=’this-is-unique-id’ >[/html]
Given: We have unique urls for each pages so this is what we are going to use. The solution is perfect if our urls are in cleanurls of drupal cms or we are using permalinks using page titles in wordpress..
Above shows us that the word ‘donate’ will be our ideal id for the body.
On our template, we will look for the body tag and insert our php script on extracting the page url and will be used in generating our unique id.
[html]<body id="<?php echo str_replace("/","",$_SERVER[‘REQUEST_URI’]) ?>">[/html]
$_SERVER[‘REQUEST_URI’] for url https://www.sherkspear.com/donate/ , it’ll return the value “/donate/” which there are slashes included so we are going to replace those slashes with empty character that is why we are using the php string function str_replace for it to return only the string ‘donate’.
Hope this helps. Enjoy coding. 🙂