{"id":5601,"date":"2026-02-12T20:44:53","date_gmt":"2026-02-13T01:44:53","guid":{"rendered":"https:\/\/chubes.net\/?documentation=text-diff-functions"},"modified":"2026-03-13T03:28:19","modified_gmt":"2026-03-13T07:28:19","slug":"text-diff-functions","status":"publish","type":"documentation","link":"https:\/\/chubes.net\/docs\/wordpress-core\/diff\/text-diff-functions\/","title":{"rendered":"Text Diff Functions"},"content":{"rendered":"<p>Core function for generating human-readable diff output.<\/p><p><strong>Source:<\/strong> <code>wp-includes\/pluggable.php<\/code><\/p><hr class=\"wp-block-separator\"\/><h2 class=\"wp-block-heading\">wp_text_diff()<\/h2><p>Displays a human-readable HTML representation of the difference between two strings.<\/p><p><strong>Since:<\/strong> 2.6.0<br \/>\n<strong>Pluggable:<\/strong> Yes \u2014 can be overridden in a plugin<\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">wp_text_diff( string $left_string, string $right_string, string|array $args = null ): string<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Parameters<\/h3><figure class=\"wp-block-table\"><table><thead><tr><th>Parameter<\/th><th>Type<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$left_string<\/code><\/td><td>string<\/td><td>&quot;Old&quot; (left) version of string<\/td><\/tr><tr><td><code>$right_string<\/code><\/td><td>string<\/td><td>&quot;New&quot; (right) version of string<\/td><\/tr><tr><td><code>$args<\/code><\/td><td>string|array<\/td><td>Optional configuration<\/td><\/tr><\/tbody><\/table><\/figure><h3 class=\"wp-block-heading\">Args<\/h3><figure class=\"wp-block-table\"><table><thead><tr><th>Key<\/th><th>Type<\/th><th>Default<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>title<\/code><\/td><td>string<\/td><td><code>''<\/code><\/td><td>Title displayed in table caption<\/td><\/tr><tr><td><code>title_left<\/code><\/td><td>string<\/td><td><code>''<\/code><\/td><td>Header for left\/original column<\/td><\/tr><tr><td><code>title_right<\/code><\/td><td>string<\/td><td><code>''<\/code><\/td><td>Header for right\/modified column<\/td><\/tr><tr><td><code>show_split_view<\/code><\/td><td>bool<\/td><td><code>true<\/code><\/td><td>Two columns (true) or single column (false)<\/td><\/tr><\/tbody><\/table><\/figure><h3 class=\"wp-block-heading\">Returns<\/h3><ul class=\"wp-block-list\"><li><code>string<\/code> \u2014 HTML table showing differences<\/li><li><code>string<\/code> (empty) \u2014 If strings are equivalent after whitespace normalization<\/li><\/ul><h3 class=\"wp-block-heading\">Processing Flow<\/h3><ol class=\"wp-block-list\"><li>Parse arguments with <code>wp_parse_args()<\/code><\/li><li>Load <code>wp-diff.php<\/code> if needed<\/li><li>Normalize whitespace on both strings<\/li><li>Split strings into lines<\/li><li>Create <code>Text_Diff<\/code> object<\/li><li>Render with <code>WP_Text_Diff_Renderer_Table<\/code><\/li><li>Wrap output in accessible HTML table<\/li><\/ol><h3 class=\"wp-block-heading\">Example: Basic Usage<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">$old_content = &quot;Hello world.nThis is a test.&quot;;\n$new_content = &quot;Hello world.nThis is a modified test.&quot;;\n\n$diff = wp_text_diff( $old_content, $new_content );\necho $diff;<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Example: With Column Headers<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">$diff = wp_text_diff( $old_content, $new_content, array(\n    &#039;title&#039;       =&gt; __( &#039;Content Changes&#039; ),\n    &#039;title_left&#039;  =&gt; __( &#039;Revision 42&#039; ),\n    &#039;title_right&#039; =&gt; __( &#039;Current Version&#039; ),\n) );<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Example: Single Column View<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">\/\/ Useful for narrow layouts or mobile\n$diff = wp_text_diff( $old_content, $new_content, array(\n    &#039;show_split_view&#039; =&gt; false,\n) );<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Example: Check for Changes<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">$diff = wp_text_diff( $old_content, $new_content );\n\nif ( empty( $diff ) ) {\n    echo &#039;No changes detected.&#039;;\n} else {\n    echo $diff;\n}<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Overriding (Pluggable)<\/h3><p>This function is pluggable and can be replaced entirely:<\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">\/\/ In a must-use plugin (loaded before pluggable.php)\nfunction wp_text_diff( $left_string, $right_string, $args = null ) {\n    \/\/ Custom implementation\n    \/\/ Perhaps using a different diff library\n    return my_custom_diff( $left_string, $right_string, $args );\n}<\/code><\/pre><\/div><h3 class=\"wp-block-heading\">Internal Notes<\/h3><ul class=\"wp-block-list\"><li>Whitespace is normalized before comparison via <code>normalize_whitespace()<\/code><\/li><li>The function loads <code>wp-diff.php<\/code> lazily only when needed<\/li><li>Empty diff result (no changes) returns empty string, not empty table<\/li><li>The <code>is-split-view<\/code> class is added to table when split view is enabled<\/li><li>Column headers use <code>&lt;th&gt;<\/code> when content is provided, <code>&lt;td&gt;<\/code> when empty<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>Core function for generating human-readable diff output. Source: wp-includes\/pluggable.php wp_text_diff() Displays a human-readable HTML representation of the difference between two strings. Since: 2.6.0 Pluggable: Yes \u2014 can be overridden in&#8230;<\/p>\n","protected":false},"featured_media":0,"template":"","meta":{"footnotes":""},"tags":[],"project":[615],"project_type":[749],"class_list":["post-5601","documentation","type-documentation","status-publish","hentry","project-diff","project_type-wordpress-reference"],"project_info":{"id":589,"name":"WordPress Core","slug":"wordpress-core"},"project_type_info":{"id":749,"name":"WordPress Reference","slug":"wordpress-reference"},"_links":{"self":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation"}],"about":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/types\/documentation"}],"version-history":[{"count":18,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5601\/revisions"}],"predecessor-version":[{"id":10893,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5601\/revisions\/10893"}],"wp:attachment":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/media?parent=5601"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/tags?post=5601"},{"taxonomy":"project","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/project?post=5601"},{"taxonomy":"project_type","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/project_type?post=5601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}