wp_text_diff()
Gets a human-readable HTML table with differences between two provided strings (texts). Used for comparing changes in post revisions.
If there are no differences between the strings, wp_text_diff() will return an empty string. The returned table consists of two columns: on the left is the first text, and on the right is the second. Deleted and inserted data are marked with the tags <del> and <ins>.
Since the function returns HTML, it is primarily used for visually displaying differences between the old and new text (for example, in post revisions).
Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.
Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.
No Hooks.
Returns
String. String: HTML code of the table if there is a difference in the strings. If there is no difference, it will return an empty string.
Usage
wp_text_diff( $left_string, $right_string, $args );
- $left_string(string) (required)
- "Old text" (will be on the left).
- $right_string(string) (required)
- "New text" (will be on the right).
- $args(string/array)
Function arguments. You can set the headers in the table:
title- header of the overall table. Default - empty.
title_left- header of the left column of the table. Default - empty.
title_right- header of the right column of the table. Default - empty.Default: null
Examples
#1 Text comparison demonstration
$string = 'Old text something more'; $string2 = 'New text more.'; echo wp_text_diff( $string , $string2, array( 'title' => 'Differences', 'title_left' => 'Old', 'title_right' => 'New', ) );
This code will output the following:
<table class='diff'> <col class='content' /> <thead> <tr class='diff-title'> <th colspan='4'>Differences</th> </tr> <tr class='diff-sub-title'> <td></td> <th>Old</th> <td></td> <th>New</th> </tr> </thead> <tbody> <tr> <td class='diff-deletedline'><del>Old</del> text<del> something</del> more </td> <td> </td> <td class='diff-addedline'><ins>New</ins> text more.<ins>.</ins> </td> </tr> </tbody> </table>
And it looks like this (depends on the css styles of your theme):
| Differences | |||
|---|---|---|---|
| Old | .New | .||
| New text more. | |||
Notes
- See: wp_parse_args() Used to change defaults to user defined settings.
Changelog
| Since 2.6.0 | Introduced. |