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.

Revisions

  1. khleomix revised this gist Mar 6, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions function.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function replace_nbsp_in_posts() {
    global $wpdb;

  2. khleomix created this gist Mar 6, 2025.
    24 changes: 24 additions & 0 deletions function.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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>';
    }
    });