Close Menu
wpexpelorwpexpelor
  • WordPress Beginner tips
  • WordPress Security
  • about us
  • contact us
Facebook X (Twitter) Instagram Pinterest
Facebook X (Twitter) Instagram Pinterest
wpexpelorwpexpelor
  • WordPress Beginner tips
  • WordPress Security
  • about us
  • contact us
wpexpelorwpexpelor
Home»WordPress Beginner tips»WordPress Create Dynamic Page URL
WordPress Beginner tips

WordPress Create Dynamic Page URL

NinaBy NinaDecember 16, 2023Updated:December 16, 2023No Comments4 Mins Read
Wordpress Create Dynamic Page URL
Wordpress Create Dynamic Page URL
Share
Facebook Twitter LinkedIn Pinterest Email
5/5 - (1 vote)

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.

Contents show
1 WordPress Create Dynamic Page code!
2 WordPress Create Dynamic Page with plugin.

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.

wpexpelor wordpress create dynamic page URL
wpexpelor wordpress create dynamic page URL

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:

Simple Custom CSS and JS : add javascript code
Simple Custom CSS and JS : add javascript code

Choose a name. For example, I used “dynamic URL” in the title section.

Simple Custom CSS and JS title.webp
Simple Custom CSS and JS title.webp

Delete all comments in the code field. Copy and paste the code I mentioned before in the code field at last. Click on publish.

wpexpelor dynamic page url wordpress
wpexpelor dynamic page url wordpress

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!

wpexpelor dynamic page URL WordPress result
wpexpelor dynamic page URL WordPress result

The job is done! Feel free to ask any questions! Please comment for me below this article!

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit VKontakte Telegram WhatsApp
Previous ArticleWordPress Reset Password Via Database
Next Article WordPress Reduce total blocking time

Related Posts

woocommerce user registration form without email

June 9, 2024

Edit Author Slug in WordPress

January 30, 2024

install 2 wordpress on same domain

January 23, 2024

Leave A Reply Cancel Reply

Trending
seo

SEO Services in texas

By NinaMay 30, 20250

Do You need to grow your business in Texas? Whether you own a company website or an online shop, you can count on us. We will get in touch as soon as possible. Please fill in the box with your name and email address.

WordPress Show Data From Database

December 12, 2023

woocommerce user registration form without email

June 9, 2024

WordPress database update required loop

April 6, 2024

Edit Author Slug in WordPress

January 30, 2024
Social
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
Facebook X (Twitter) Instagram Pinterest
©2025 WPexpelor. All Rights Reserved.

Type above and press Enter to search. Press Esc to cancel.