Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

As @toscho already answered herehere, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best PracticesBest Practices

As @toscho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

As @toscho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

added 1 character in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109

As @tosho@toscho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

As @tosho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

As @toscho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

added 209 characters in body
Source Link
fischi
  • 7.6k
  • 2
  • 29
  • 44

As @tosho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

As @tosho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

As @tosho already answered here, yes this is possible.

You just need to copy this code into the functions.php of your theme, and there you go.

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    
    // use this line if you have multiple posts with the same title
    $new_slug = wp_unique_post_slug( $new_slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent );

    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

You can also create a plugin for that, if you create a yourplugin.php file in your plugins folder, with a valid plugin header and the above code:

<?php
    /*
    Plugin Name: Your Plugin
    Plugin URI: http://www.example.com
    Description: description
    Version: 0.1
    Author: thatsyou
    Author URI: http://www.yourdomain.com
    License: MIT
    */
    
    //yourcode
?>  

Please be aware that if you copy this code into your functions.php or your activate the plugin, it is executed all the time. You can get some ideas on how to execute code only once here: Best Practices

Source Link
fischi
  • 7.6k
  • 2
  • 29
  • 44
Loading