wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_edit_root_attachment_id › WordPress Function

Since7.2.0
Deprecatedn/a
› wp_get_edit_root_attachment_id ( $attachment_id )
Parameters:
  • (int) $attachment_id Attachment ID.
    Required: Yes
Returns:
  • (int) ID of the attachment the chain of edits started from, or 0 when none is recorded.
Defined at:
Codex:

Retrieves the edit root of an attachment: the attachment its chain of edits started from.

Editing an image through the wp/v2/media/<id>/edit REST endpoint does not change the image that was edited. It saves the result as a brand new attachment, so a site can end up with a chain of attachments: an upload, a crop of it, a crop of that crop, and so on. Every attachment created that way stores the ID of the attachment at the top of its chain, so this function can find the edit root in one lookup no matter how long the chain is. Attachments that were uploaded rather than created by editing have no chain of their own, and this returns 0 for them.


Source

function wp_get_edit_root_attachment_id( $attachment_id ) {
	$edit_root_id = (int) get_post_meta( $attachment_id, '_wp_attachment_edit_root_id', true );

	// An attachment recorded as its own edit root is a broken record rather than a chain.
	if ( $edit_root_id <= 0 || $edit_root_id === (int) $attachment_id ) {
		return 0;
	}

	return $edit_root_id;
}