wp_text_diff()WP 2.6.0

Displays a human readable HTML representation of the difference between two strings.

The Diff is available for getting the changes between versions. The output is HTML, so the primary use is for displaying the changes. If the two strings are equivalent, then an empty string will be returned.

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.

Return

String. Empty string if strings are equivalent or HTML with differences.

Usage

wp_text_diff( $left_string, $right_string, $args );
$left_string(string) (required)
"old" (left) version of string.
$right_string(string) (required)
"new" (right) version of string.
$args(string|array)

Associative array of options to pass to WP_Text_Diff_Renderer_Table().

Default: null

  • title(string)
    Titles the diff in a manner compatible with the output.
    Default: ''

  • title_left(string)
    Change the HTML to the left of the title.
    Default: ''

  • title_right(string)
    Change the HTML to the right of the title.
    Default: ''

  • show_split_view(true|false)
    True for split view (two columns), false for un-split view (single column).
    Default: true

Examples

0

#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
Old text something more   New text more.

Notes

Changelog

Since 2.6.0 Introduced.

wp_text_diff() code WP 6.4.3

function wp_text_diff( $left_string, $right_string, $args = null ) {
	$defaults = array(
		'title'           => '',
		'title_left'      => '',
		'title_right'     => '',
		'show_split_view' => true,
	);
	$args     = wp_parse_args( $args, $defaults );

	if ( ! class_exists( 'WP_Text_Diff_Renderer_Table', false ) ) {
		require ABSPATH . WPINC . '/wp-diff.php';
	}

	$left_string  = normalize_whitespace( $left_string );
	$right_string = normalize_whitespace( $right_string );

	$left_lines  = explode( "\n", $left_string );
	$right_lines = explode( "\n", $right_string );
	$text_diff   = new Text_Diff( $left_lines, $right_lines );
	$renderer    = new WP_Text_Diff_Renderer_Table( $args );
	$diff        = $renderer->render( $text_diff );

	if ( ! $diff ) {
		return '';
	}

	$is_split_view       = ! empty( $args['show_split_view'] );
	$is_split_view_class = $is_split_view ? ' is-split-view' : '';

	$r = "<table class='diff$is_split_view_class'>\n";

	if ( $args['title'] ) {
		$r .= "<caption class='diff-title'>$args[title]</caption>\n";
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$r .= '<thead>';
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$th_or_td_left  = empty( $args['title_left'] ) ? 'td' : 'th';
		$th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th';

		$r .= "<tr class='diff-sub-title'>\n";
		$r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n";
		if ( $is_split_view ) {
			$r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n";
		}
		$r .= "</tr>\n";
	}

	if ( $args['title_left'] || $args['title_right'] ) {
		$r .= "</thead>\n";
	}

	$r .= "<tbody>\n$diff\n</tbody>\n";
	$r .= '</table>';

	return $r;
}