1

Is it possible to rewrite the slug of a particular page (e.g. Page-ID 123) with a wordpress function?

I found this somewhere but It doesnt work and I am also looking to simplify it as I just want to change the permalink of a single page

function rudr_post_permalink( $url, $post ){
    if( !is_object( $post ) )
        $post = get_post( $post_id );
        
    $replace = $post->post_name;
        
    /* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */
        
    if( $post->ID == 1 ) 
        $replace = 'hello-planet';
    if( $post->ID == 12 ) 
        $replace = 'Contacts';
        
    $url = str_replace($post->post_name, $replace, $url );
    return $url;
}

add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );

The reason I want to do this is because I want to change the slug of a particular page every x hours. I do not want to redirect the old slug to the new slug.

0

1 Answer 1

4
+100

You'll need a two step process for that.

  1. Create a function that actually changes the slug (not rewrite it)
  2. Schedule an event in WordPress to run that function in every X hours.

Here are something to start with

function wpse402903_schedule_event() {

    add_action( 'wpse402903_cron', 'wpse402903_cron_callback' );
    
    if ( !wp_next_scheduled('wpse402903_cron') ) {
        //Change 12 to your interval
        wp_schedule_event( time(), 12 * HOUR_IN_SECONDS, 'wpse402903_cron' );
    }
}
add_action( 'init', 'wpse402903_schedule_event' );

function wpse402903_cron_callback() {
    
    $post_id = 123; //The ID of the Post
    
    $postID = wp_insert_post( array(
        'ID' => $post_id,
        'post_name' => 'your-new-slug', //always use sanitize_title() if this generates dynamically.
    ));
}  

The only drawback is the WP Cron. It acivates only if someone visits your site. For a site with regular traffic, this is not a problem though.

6
  • Just to add to @Abhik answer, you can create a REST Endpoint that instead of WP Cron and use your Server's Cron scheduler which will trigger it automatically without need for site visitors. Commented Feb 26, 2022 at 18:28
  • 1
    Please make sure to also check for that slug availability first, or you might run into issues. You could also use wp_unique_post_slug()
    – Bysander
    Commented Feb 28, 2022 at 17:23
  • yes I will be using a random slug each time so I need to incorporate that to the answer as well
    – JoaMika
    Commented Mar 3, 2022 at 18:16
  • hello, I am going to need this to work every 10 minutes without relying on site visitors... any help would be much appreciated - a random unique url slug should be created every 10 minutes
    – JoaMika
    Commented Oct 6, 2022 at 12:46
  • @JoaMika You can create a php file in your theme/plugin with the wpse402903_cron_callback function in the answer and include the wp-load.php at top of that file to use your custom function to be recognized by WordPress. After that, use your server's cron system to to call that file every 10 minutes.
    – Abhik
    Commented Oct 6, 2022 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.