wordpress create dynamic page URL?! Here is an absolutely new and exciting concept! How can I create a page with a dynamic URL? I mean, every time you refresh the page, the URL will change! How does this concept come in handy? Well, you can apply this capability in many cases, but I’m going to explain it with an example:
Suppose an online shop is going to keep its off-sale code on the page and doesn’t want the page to be accessible forever! Some managers may want to provide customers with a certain discount code, and if the user doesn’t use the code, he cannot access the code anymore because by refreshing the page, the former URL will be lost, and a new one will be produced! This was only a general example. This method has other security uses and it depends on you to choose to work with it. Stay with me to learn how to create a wordpress page with a dynamic URL.
WordPress Create Dynamic Page code!
Before starting we delve into the dynamic page URL project, I must warn you this could result in some unwanted problems, especially in SEO. So, consider the risk before starting. To create dynamic page URLs in wordpress, we use JAVASCRIPT. You may have some experience in developing JavaScript. If so, you can change the code as you desire, and if not, you can use this code as well.
Let’s create a new page to apply changes. I’m going to create a page named:” sample” and put test data on it, and I’ll publish the page. And now the code:
<script>
// Generate a random string for the dynamic part of the URL
var dynamicSlug = Math.random().toString(36).substring(7);
// Get the current URL
var currentUrl = window.location.href;
// Replace the static part of the URL with the dynamic slug
var newUrl = currentUrl.replace(/sample/, dynamicSlug);
// Set the new URL
history.replaceState({}, document.title, newUrl);
</script>
As you can see, I’ve created a variable named “dynamicSlug”. The data type that “dynamicSlug” is holding is string type. This “String” is produced randomly and is composed of a mixture of numbers and letters.
I’ve created another variable called “currentUrl”. This variable duty is holding the current page address, and it is going to change the current page address with the next page URL that will be created randomly.
var newUrl = currentUrl.replace(/sample/, dynamicSlug);
Note: sample is replaced with your own wordpress page URL.
You may come across the question of whether it is possible to guess a page URL? and what is the logic behind producing a random string for the page URL name.
These strings are produced with the “math.random() “ function in JavaScript. So, considering the logic behind this question, the answer is that if some expert people are familiar with math algorithms, the pattern will be revealed! And this is the reason you shouldn’t use this method for treating security problems!
WordPress Create Dynamic Page with plugin.
Open the wordpress dashboard and then click on “add new plugin”. Search for “Simple Custom CSS and JS” and then install and activate the plugin.
Use “Add Custom JS” from the “Custom CSS & JS” menu. You will see this page:
Choose a name. For example, I used “dynamic URL” in the title section.
Delete all comments in the code field. Copy and paste the code I mentioned before in the code field at last. Click on publish.
And now the time has come! We are going to test the page that we have created in the browser to see whether it works properly or not!
As you can see, there is a random dynamic wordpress page URL. Refresh the page and each time you can see the page URL address is different!
The job is done! Feel free to ask any questions! Please comment for me below this article!