wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_get_edit_root_attachment_id › WordPress Function

Depuis7.2.0
Dépréciéen/a
› wp_get_edit_root_attachment_id ( $attachment_id )
Paramètres:
  • (int) $attachment_id Attachment ID.
    Requis: Oui
Retourne:
  • (int) ID of the attachment the chain of edits started from, or 0 when none is recorded.
Défini(e) dans:
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;
}