admin管理员组

文章数量:1122832

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 months ago.

Improve this question

Would it be possible through our modern WordPress system to have a "specific" page permalink change often (.e.g. every day, or every week, or every month) dynamically through code?

If my initial permalink structure for my WordPress "Page" looks like this...

...can we craft some code to have that "Page" only accessible through...

Then after certain period of time, my code tells that same "Page" to now have its permalink become...

@38

Is there anything that comes close to achieving this level of dynamic URL change today?

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 months ago.

Improve this question

Would it be possible through our modern WordPress system to have a "specific" page permalink change often (.e.g. every day, or every week, or every month) dynamically through code?

If my initial permalink structure for my WordPress "Page" looks like this...

https://www.mysite.org/newsletter-subscription-center/manage

...can we craft some code to have that "Page" only accessible through...

https://www.mysite.org/newsletter-subscription-center/manage12345

Then after certain period of time, my code tells that same "Page" to now have its permalink become...

https://www.mysite.org/newsletter-subscription-center/mange837@38

Is there anything that comes close to achieving this level of dynamic URL change today?

Share Improve this question edited Sep 12, 2024 at 23:02 klewis asked Sep 12, 2024 at 22:53 klewisklewis 8991 gold badge14 silver badges29 bronze badges 4
  • what is the aim of this ? you want a secret url that is shared only for a few people ? – mmm Commented Sep 13, 2024 at 6:09
  • I’m voting to close this question because it is a discussion topic – jdm2112 Commented Sep 15, 2024 at 14:10
  • I vote for you jdm2112 or anyone that wants to randomly give your opinion on closing a question that you also provide a link to help the rest of us now what we do is illegal on stack. – klewis Commented Sep 16, 2024 at 20:36
  • This question is likely to be answered with opinions rather than facts and citations. It should be updated so it will lead to fact-based answers. | But this question did lead to coding facts (e.g. WordPress hooks that help contribute the solution), on how to solve a solution. should not be considered a post that does not lead to facts... – klewis Commented Sep 16, 2024 at 20:39
Add a comment  | 

1 Answer 1

Reset to default 2

You can dynamically change the permalink of a specific page programmatically. We can achieve this with the help of WordPress hooks and a custom function these two will update the page slug at a specified interval.

Step 1 : We need to schedule a cron event that will update the page slug as per our desired interval (daily, weekly, or monthly).

if ( ! wp_next_scheduled( 'update_page_slug_event' ) ) {
    wp_schedule_event( time(), 'daily', 'update_page_slug_event' ); // Here you need to change this 'daily' to 'weekly' or 'monthly' as per your need.
}

Step 2 : Once the page slug is updated, we need to flush the rewrite rules in order to ensure the new URL works correctly. Here we have hook the function to the scheduled event.

add_action( 'update_page_slug_event', 'update_page_slug' );

function update_page_slug() {

    // ID of the page you want to update
    $page_id  = 123; // You can replace this with your actual page ID.

    // Here we are generating a new slug (you can customize this part as per your need).
    $new_slug = 'manage' . wp_generate_password( 6, false, false );

    // Here we are updating the page slug.
    wp_update_post( array(
        'ID'        => $page_id,
        'post_name' => $new_slug
    ) );

    // This is to flush rewrite rules to ensure the new URL works properly.
    flush_rewrite_rules();
}

We need to add the code given in step 1 and step 2 to the functions.php file of the theme.

本文标签: Dynamically change page permalink name often