Created
March 5, 2025 14:47
-
-
Save jrevillini/edf08952b2edb906f38644649099b33e to your computer and use it in GitHub Desktop.
leverage the Redirection plugin database to dynamically replace links within Wordpress content with target URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
////////////////////////////////////////////////////////////////////////////////// | |
//////////////////// 🚀 Schwifty-𝓪𝓼𝓼 Link Fixer 3000™ ///////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
// | |
// The purpose of this snippet is to leverage the Redirection plugin database to | |
// dynamically replace links within Wordpress content so the site itself has as few | |
// pointers to URLs that redirect as possible. | |
// | |
function schwifty_replace_old_links_in_content($content) { | |
// Ensure Redirection plugin is active; if not, BAIL EARLY | |
if (!is_plugin_active('redirection/redirection.php')) { | |
return $content; | |
} | |
// Define allowed post types and specific post IDs (set to [] for all) | |
$allowed_post_types = ['post']; // Modify for your use case; [] = all post types; ['post','page'] = just posts and pages etc | |
$allowed_post_ids = []; // Set specific post IDs for testing or [] for all; [1,2,3] = just post ID 1, 2 and 3 | |
// Get the current post object | |
global $post; | |
if (!$post) { | |
return $content; | |
} | |
// Check if the current post matches the post types or the allowed post ID | |
if (!empty($allowed_post_types) && !in_array($post->post_type, $allowed_post_types)) { | |
return $content; | |
} | |
if (!empty($allowed_post_ids) && !in_array($post->ID, (array) $allowed_post_ids)) { | |
return $content; | |
} | |
// Extract all <a> tags from content | |
if (preg_match_all('/<a\s[^>]*href=["\']([^"\']+)["\']/i', $content, $matches)) { | |
$urls_to_check = array_unique($matches[1]); // Unique URLs only | |
if (!$urls_to_check) { | |
return $content; // No links to change, bail early | |
} | |
foreach ($urls_to_check as $old_url) { | |
$redirects = Red_Item::get_for_url($old_url); // Returns an array of Red_Item objects | |
if (empty($redirects) || ! is_array($redirects) || count($redirects) < 1 ) continue; | |
foreach ($redirects as $redirect) { | |
$action_data = $redirect->get_action_data(); | |
if ( $action_data === '' || $redirect->is_regex() ) continue; // @TODO needs to be fixed to handle regex matching | |
$new_url = esc_url( $action_data ); | |
$replacements[$old_url] = $new_url; | |
break; // Use the first valid redirect and move on | |
} | |
} | |
// Only do a replacement if we found redirects | |
if (!empty($replacements)) { | |
foreach ($replacements as $old => $new) { | |
$content = str_replace( | |
'href="' . $old . '"', | |
'href="' . $new . '" data-schwifty-oldhref="' . esc_attr($old) . '"', // keep old URL in data attribute cuz that's fun | |
$content | |
); | |
} | |
} | |
} | |
return $content; | |
} | |
add_filter('the_content', 'schwifty_replace_old_links_in_content', 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
live-replace URLs in Wordpress content using Redirection database
Requires: Redirection
This is a dirty code snippet to live-replace URLs in Wordpress content before a page is served. Please take shots at it and help this become a snippet that works for all situations. Obviously, the best thing would be to actually find/replace strings in the database, however this opens us up to corruption risk and other time-consuming cleanup if it screws up.
This lets the Redirection plugin continue handling uncaught (i.e. external) redirects while live-fixing internal links at render-time*
*this is a horrible if you’re not caching, but then again, what kind of animal are you if you’re not caching to some degree? LOL)
Known Flaws:
Benefits of this add-on/snippet