wp_xmlrpc_server::add_enclosure_if_new() WordPress Method

The add_enclosure_if_new() method is a convenience method for adding an enclosure to a post if it does not already exist. This is useful for ensuring that an enclosure is always included with a post, even if the post is edited later.

wp_xmlrpc_server::add_enclosure_if_new( int $post_ID, array $enclosure ) #

Adds an enclosure to a post if it’s new.


Parameters

$post_ID

(int)(Required)Post ID.

$enclosure

(array)(Required)Enclosure data.


Top ↑

Source

File: wp-includes/class-wp-xmlrpc-server.php

	public function add_enclosure_if_new( $post_ID, $enclosure ) {
		if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
			$encstring  = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
			$found      = false;
			$enclosures = get_post_meta( $post_ID, 'enclosure' );
			if ( $enclosures ) {
				foreach ( $enclosures as $enc ) {
					// This method used to omit the trailing new line. #23219
					if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
						$found = true;
						break;
					}
				}
			}
			if ( ! $found ) {
				add_post_meta( $post_ID, 'enclosure', $encstring );
			}
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More