wp_text_diff( string $left_string, string $right_string, string|array $args = null ): string

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

Description

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.

See also

Parameters

$left_stringstringrequired
"old" (left) version of string.
$right_stringstringrequired
"new" (right) version of string.
$argsstring|arrayoptional
Associative array of options to pass to WP_Text_Diff_Renderer_Table().
  • title string
    Titles the diff in a manner compatible with the output. Default empty.
  • title_left string
    Change the HTML to the left of the title.
    Default empty.
  • title_right string
    Change the HTML to the right of the title.
    Default empty.
  • show_split_view bool
    True for split view (two columns), false for un-split view (single column). Default true.

Default:null

Return

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

Source


$args = array_merge( $args, $loading_optimization_attr );

if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
	$id_or_email = get_comment( $id_or_email );
}

/**
 * Allows the HTML for a user's avatar to be returned early.
 *
 * Returning a non-null value will effectively short-circuit get_avatar(), passing
 * the value through the 'get_avatar' filter and returning early.
 *
 * @since 4.2.0
 *
 * @param string|null $avatar      HTML for the user's avatar. Default null.
 * @param mixed       $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
 *                                 user email, WP_User object, WP_Post object, or WP_Comment object.
 * @param array       $args        Arguments passed to get_avatar_url(), after processing.
 */
$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );

if ( ! is_null( $avatar ) ) {
	/** This filter is documented in wp-includes/pluggable.php */
	return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
}

if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {
	return false;
}

$url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) );

$args = get_avatar_data( $id_or_email, $args );

$url = $args['url'];

if ( ! $url || is_wp_error( $url ) ) {
	return false;
}

$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );

if ( ! $args['found_avatar'] || $args['force_default'] ) {
	$class[] = 'avatar-default';
}

if ( $args['class'] ) {
	if ( is_array( $args['class'] ) ) {
		$class = array_merge( $class, $args['class'] );
	} else {
		$class[] = $args['class'];
	}
}

// Add `loading`, `fetchpriority`, and `decoding` attributes.
$extra_attr = $args['extra_attr'];

if ( in_array( $args['loading'], array( 'lazy', 'eager' ), true )
	&& ! preg_match( '/\bloading\s*=/', $extra_attr )

Changelog

VersionDescription
2.6.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example

    $left_string = 'This is the original string';
    
    $right_string = 'This is the revised string';
    
    $args = array(
    	'title'       => 'Differences',
    	'title_left'  => 'Old Version',
    	'title_right' => 'New Version'
    );
    
    $diff_table = wp_text_diff( $left_string,$right_string, $args );
    
    echo $diff_table;

    This will output the following html:

    [html]
    <table class="diff">
    <colgroup>
    <col class="ltype">
    <col class="content">
    <col class="ltype">
    <col class="content">
    </colgroup>
    <thead>
    <tr class="diff-title">
    <th colspan="4">Differences</th>
    </tr>
    <tr class="diff-sub-title">
    <td></td>
    <th>Old Version</th>
    <td></td>
    <th>New version</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>-</td>
    <td class="diff-deletedline">
    This is the <del>original</del> string
    </td>
    <td>+</td>
    <td class="diff-addedline">
    This is the <ins>revised</ins> string
    </td>
    </tr>

    </tbody>
    </table>
    [/html]

You must log in before being able to contribute a note or feedback.