Universal PHP solution for managing multiple 301 redirects without plugins - works with any post type
Guide, Tools, Websites, WordPress customization

The Simple WordPress Redirect Method That Works for Posts and Pages

Universal PHP solution for managing multiple 301 redirects without plugins – works with any post type

When you need to redirect multiple WordPress posts or pages, most solutions force you to choose between complex plugins or writing separate code for posts versus pages. Here’s a simple, universal method that handles everything with just a few lines of code.

Why This Method is Superior

Traditional redirect approaches have limitations:

  • Plugin dependency: Most redirect plugins add bloat and potential security vulnerabilities
  • Type-specific code: Using is_single() only works for posts, is_page() only for pages
  • Maintenance nightmare: Adding new redirects requires duplicating similar code blocks

This universal method solves all these problems with a clean, maintainable solution.

The Universal Redirect Code

Add this code to your theme’s functions.php file or use a code snippets plugin like WPCode:

				
					function multiple_post_redirects() {
    // Array of redirects: source_ID => destination_ID
    $redirects = array(
        6687 => 43914,  // post or page
        3455 => 43918,  // post or page
        1786 => 44393,  // post or page
        1916 => 20601,  // post or page
        31   => 5856,   // post or page
    );
    
    // Check if we're on a singular page (post, page, or custom post type)
    if (is_singular()) {
        $current_id = get_the_ID();
        
        if (isset($redirects[$current_id])) {
            $redirect_url = get_permalink($redirects[$current_id]);
            if ($redirect_url) {
                wp_redirect($redirect_url, 301);
                exit;
            }
        }
    }
}
add_action('template_redirect', 'multiple_post_redirects');
				
			

How It Works

1. Universal Detection: is_singular() catches posts, pages, and custom post types – no need to worry about content types.

2. Array-Based Management: All redirects are stored in a simple array. Just add source_ID => destination_ID pairs.

3. Safe Execution: The code only runs on the frontend using the template_redirect hook, ensuring redirects happen before any content loads.

4. Error Prevention: Built-in checks ensure the destination URL exists before attempting the redirect.

Key Benefits

Simplicity: Add new redirects by simply adding one line to the array.

Performance: No database queries for each redirect check – everything is stored in memory.

Flexibility: Works with posts, pages, custom post types, and any combination.

SEO-Friendly: Proper 301 redirects preserve link equity and search rankings.

Plugin-Free: No external dependencies or potential conflicts.

Adding More Redirects

To add a new redirect, simply add a line to the $redirects array:

				
					$redirects = array(
    6687 => 43914,
    3455 => 43918,
    // Add your new redirect here:
    1234 => 5678,  // redirects post/page 1234 to post/page 5678
);
				
			

When to Use This Method

This approach is perfect when you:

  • Need to redirect multiple posts or pages
  • Want to avoid plugin bloat
  • Have mixed content types (posts and pages)
  • Need a maintainable, long-term solution
  • Want full control over your redirects

Implementation Tips

  1. Test first: Always test redirects in a staging environment
  2. Use 301 redirects: They’re permanent and pass SEO value
  3. Clear cache: Remember to clear any caching after implementing
  4. Monitor: Check that both source and destination URLs are correct

 

This universal method eliminates the guesswork from WordPress redirects. Whether you’re dealing with posts, pages, or custom post types, one simple array manages everything efficiently and effectively.

Need help with this solution or looking for custom development?

Visit my Stay In Touch page to connect and discuss your project. Discover a range of web development services and specialized WordPress solutions tailored to your needs. Let's work together to enhance your digital presence.