最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Change the slug of a particular page every x hours

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Feb 24, 2022 at 12:57 JoaMika asked Feb 20, 2022 at 16:41 JoaMikaJoaMika 6986 gold badges27 silver badges58 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论