Skip to main content
  1. All Posts/

wordpress-rewrite-content-urls-plugin

Tools Open Source Sync WordPress


Rewrite Content URLs

This plugin allows you to rewrite URLs in content.
This plugin was primarily designed for developers, useful for development/staging environments after syncing data from a MySQL dump.
DO NOT USE IN PRODUCTION: This plugin should only be used for development/staging sites with frequently-synced data, as it creates a performance penalty.
📌 Use in conjunction with: Network Sub-domain Updater for WordPress multisite

Requirements

  • WordPress 4.5 or higher
  • PHP 7.0 or higher
  • output-buffering.php must be copied to your WordPress mu-plugins directory.

This plugin will not work on versions of PHP below 7.0.

Installation

  1. Download and unzip the plugin to your WordPress plugins directory.
  2. Open wp-config.php for your site and define the REWRITE_URL_PATTERNS constant.
  3. Activate the plugin.

There is no settings page in WP Admin for this plugin. All options are configured through wp-config.php constants.

Configuration

In order for this plugin to work, you must add a REWRITE_URL_PATTERNS constant to your wp-config.php.
Example of simply replacing the page links’ domain:

define( 'REWRITE_URL_PATTERNS', [ '/mydomain.com/' => 'mydomain.local' ] );

Example of simply replacing the page links’ domain AND stripping ‘www’ (useful if your local development stack doesn’t alias it):

define( 'REWRITE_URL_PATTERNS', [ '/(www.)?mydomain.com/' => 'mydomain.local' ] );

Since the constant expects an array, you and add multiple match expressions.

Use Case

Let’s say you run a WordPress Network of sub-domains. If you did a MySQL dump from your production server (example.com) and then imported to your staging (staging.example.com) or local development (example.local) instance and updated the domains, the links in your page content will be pointing to the production site.
This plugin replaces URL patterns in content, defined by REWRITE_URL_PATTERNS constant.
For example, I use Local by Flywheel for local WordPress development:

  1. Create a local WordPress multisite instance.
  2. Copy necessary file assets (plugins, themes, uploads, etc) from the remote site.
  3. Install this plugin on your staging or development instance (not on production!)
  4. Define the REWRITE_URL_PATTERNS in wp-config.php.

Now when you view pages on your instance, the links will be rewritten to your new domain.

Frequently Asked Questions

Q: Why did you use a constant to define the replacement URLs and not a settings page in WP Admin?
A: This plugin was designed for developers who sync data via a MySQL dump from a production site to staging/local development. Because of this, any settings set in WP Admin would be blown away.

Future Ideas

  • Ability to rewrite image URLs
  • Ability to set filter exceptions
  • Possibility of moving configuration to file

Change Log

Release changes are noted on the Releases page.

Branch: master

  • None since release