May 18, 2023

How To Change/Edit Bulk URL Of Posts In WordPress using Plugins or PHP

How To Change/Edit Bulk URL Of Posts In WordPress using Plugins or PHP

To change the URLs of bulk posts in WordPress, you can use a plugin or a combination of plugins and manual methods. Here’s a step-by-step guide using a popular plugin called “Bulk Edit”:

  1. Install and activate the “Bulk Edit” plugin from the WordPress plugin repository.
  2. Go to your WordPress dashboard and navigate to “Posts” or “Pages,” depending on which type of content you want to modify.
  3. Select the posts or pages you want to edit by checking the corresponding checkboxes next to each item.
  4. Once you’ve selected the desired content, click on the “Bulk Actions” dropdown menu, and choose “Edit.”
  5. Click the “Apply” button to proceed to the bulk editing screen.
  6. Locate the “Post Slug” or “Page Slug” field (the URL-friendly version of the post or page title).
  7. Modify the slugs for the selected posts or pages, ensuring that each slug is unique and URL-friendly (consisting of lowercase letters, numbers, and hyphens).
  8. After making the necessary changes, click the “Update” button to save the modifications.

It’s important to note that modifying the URLs of posts or pages may affect their search engine rankings and result in broken links. To mitigate these issues, consider implementing 301 redirects from the old URLs to the new ones. You can achieve this using a plugin like “Redirection” or by editing your website’s .htaccess file manually.

Remember to backup your website before performing bulk edits or making significant changes to ensure you have a restore point in case any issues arise.

If you want to add a number to every URL every day using PHP, you can achieve it by creating a custom function and implementing it in your WordPress theme’s functions.php file. Here’s an example:

  1. Access your WordPress theme files via FTP or through the WordPress dashboard by going to Appearance > Theme Editor.
  2. Locate the functions.php file for your active theme and open it for editing.
  3. Add the following code snippet at the end of the functions.php file:
function add_daily_number_to_urls($permalink) {
  // Get the current date
  $current_date = date('Ymd');
  // Generate a random number
  $random_number = mt_rand(100, 999);
  // Append the date and number to the URL
  $permalink = $permalink . $current_date . $random_number;
  return $permalink;

add_filter('post_link', 'add_daily_number_to_urls');
  1. Save the changes to the functions.php file.

This code creates a custom function named add_daily_number_to_urls that appends the current date (in the format Ymd) and a random three-digit number to the end of the permalink. The add_filter function hooks this custom function into the post_link filter, which modifies the post permalink before it is displayed.

Please note that adding a dynamic component to the URL like this can affect your website’s SEO and may result in broken links if the URLs are shared or indexed by search engines. Ensure you understand the implications and test thoroughly before implementing this functionality on a live website.

If the modified URLs are resulting in 404 errors when accessed, it indicates that the WordPress rewrite rules need to be updated to recognize the new URL structure. To resolve this issue, you can follow these steps:

  1. Access your WordPress dashboard and go to Settings > Permalinks.
  2. Without making any changes, click the “Save Changes” button. This action refreshes the rewrite rules and ensures that WordPress recognizes the new URL structure.
  3. After saving the changes, try accessing the posts with the modified URLs again. They should now properly redirect to the corresponding content instead of a 404 page.

By saving the permalink settings, WordPress regenerates the rewrite rules and flushes the existing rules, allowing the modified URLs to be correctly interpreted.

If the issue persists even after saving the permalink settings, you can try manually flushing the rewrite rules by adding the following code snippet to your theme’s functions.php file:

function flush_rewrite_rules_on_activation() {
register_activation_hook( __FILE__, 'flush_rewrite_rules_on_activation' );

This code snippet ensures that the rewrite rules are flushed whenever the theme is activated. After adding this code, reactivate your theme, and the rewrite rules should be refreshed.

Remember to remove the code snippet from functions.php after the rewrite rules have been flushed to avoid unnecessary rule regeneration on each theme activation.

If the problem persists or you encounter any other issues, it might be worth checking if any other plugins or custom code are conflicting with the permalink structure or causing the 404 errors.

If the previous solutions didn’t resolve the issue, you can try manually updating the rewrite rules using the generate_rewrite_rules filter. Here’s an alternative approach:

  1. Open your theme’s functions.php file for editing.
  2. Remove the previous code added to the file.
  3. Add the following code snippet instead:
function custom_rewrite_rules() {
add_action('init', 'custom_rewrite_rules');

function custom_rewrite_tags() {
  add_rewrite_tag('%post_number%', '([0-9]{3})');
add_action('init', 'custom_rewrite_tags', 10, 0);

function custom_post_link($permalink, $post) {
  if ($post->post_type === 'post') {
    $post_number = str_pad(mt_rand(0, 999), 3, '0', STR_PAD_LEFT);
    $permalink = str_replace('%post_number%', $post_number, $permalink);
  return $permalink;
add_filter('post_link', 'custom_post_link', 10, 2);
  1. Save the changes to the functions.php file.

In this code snippet, we’re manually adding a custom rewrite rule and rewrite tag. The custom_rewrite_rules function adds a rewrite rule that matches a URL pattern with a date (8 digits) and a three-digit number. It redirects the request to the post based on the provided date. The custom_rewrite_tags function adds a custom rewrite tag for the three-digit number.

The custom_post_link function generates a random three-digit number and replaces the %post_number% placeholder in the permalink structure. This ensures that each post URL will have a unique number.

After adding this code snippet, save the changes and flush the rewrite rules by going to Settings > Permalinks in your WordPress dashboard and clicking the “Save Changes” button.

Now, when you access a post URL with the pattern /post-url-pattern/20230512001, where 20230512 is the date and 001 is the three-digit number, it should properly redirect to the corresponding post without resulting in a 404 error.

Please note that modifying rewrite rules and implementing custom solutions can be complex, and it’s recommended to have a backup of your website before making any changes. If you’re still experiencing issues, you may need to consult with a developer or consider reaching out to WordPress support for further assistance.