Skip to content

Instantly share code, notes, and snippets.

@khleomix
Last active March 6, 2025 19:13
Show Gist options
  • Save khleomix/b0fc46a6b0d24ddea69e6bc8abca7833 to your computer and use it in GitHub Desktop.
Save khleomix/b0fc46a6b0d24ddea69e6bc8abca7833 to your computer and use it in GitHub Desktop.
Remove nbsp
<?php
function replace_nbsp_in_posts() {
global $wpdb;
$query = "
UPDATE {$wpdb->posts}
SET post_content = REPLACE(post_content, CHAR(160), ' ')
WHERE post_content LIKE '%" . chr(160) . "%'
";
$result = $wpdb->query($query);
if ($result !== false) {
return "Successfully replaced non-breaking spaces in $result posts.";
} else {
return "Failed to update posts.";
}
}
// Run the function only when needed
add_action('admin_init', function() {
if (current_user_can('manage_options') && isset($_GET['replace_nbsp'])) {
echo '<div class="updated"><p>' . replace_nbsp_in_posts() . '</p></div>';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment