media_single_attachment_fields_to_edit

Definition:
function media_single_attachment_fields_to_edit( $form_fields, $post ) {}

Parameters

  • unknown_type $form_fields
  • unknown_type $post

Source code

function media_single_attachment_fields_to_edit( $form_fields, $post ) {

	unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);

	return $form_fields;

}

2359

media_sideload_image

Definition:
function media_sideload_image($file, $post_id, $desc = null) {}

Download an image from the specified URL and attach it to a post.

Parameters

  • string $file: The URL of the image to download
  • int $post_id: The post ID the media is to be associated with
  • string $desc: Optional. Description of the image

Return values

returns:Populated HTML img tag on success

Source code

function media_sideload_image($file, $post_id, $desc = null) {

	if ( ! empty($file) ) {

		// Download file to temp location

		$tmp = download_url( $file );



		// Set variables for storage

		// fix file filename for query strings

		preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);

		$file_array['name'] = basename($matches[0]);

		$file_array['tmp_name'] = $tmp;



		// If error storing temporarily, unlink

		if ( is_wp_error( $tmp ) ) {

			@unlink($file_array['tmp_name']);

			$file_array['tmp_name'] = '';

		}



		// do the validation and storage stuff

		$id = media_handle_sideload( $file_array, $post_id, $desc );

		// If error storing permanently, unlink

		if ( is_wp_error($id) ) {

			@unlink($file_array['tmp_name']);

			return $id;

		}



		$src = wp_get_attachment_url( $id );

	}



	// Finally check to make sure the file has been saved, then return the html

	if ( ! empty($src) ) {

		$alt = isset($desc) ? esc_attr($desc) : '';

		$html = "<img src='$src' alt='$alt' />";

		return $html;

	}

}

2357

media_send_to_editor

Definition:
function media_send_to_editor($html) {}

Parameters

  • unknown_type $html

Source code

function media_send_to_editor($html) {

?>

<script type="text/javascript">

/* <![CDATA[ */

var win = window.dialogArguments || opener || parent || top;

win.send_to_editor('<?php echo addslashes($html); ?>');

/* ]]> */

</script>

<?php

	exit;

}

2355

media_post_single_attachment_fields_to_edit

Definition:
function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {}

Parameters

  • unknown_type $form_fields
  • unknown_type $post

Source code

function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {

	unset($form_fields['image_url']);

	return $form_fields;

}

2353

media_handle_upload

Definition:
function media_handle_upload($file_id, $post_id, $post_data = array() {}

This handles the file upload POST itself, creating the attachment post.

Parameters

  • string $file_id: Index into the $_FILES array of the upload
  • int $post_id: The post ID the media is associated with
  • array $post_data: allows you to overwrite some of the attachment
  • array $overrides: allows you to override the wp_handle_upload() behavior

Return values

returns:the ID of the attachment

Source code

function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {



	$time = current_time('mysql');

	if ( $post = get_post($post_id) ) {

		if ( substr( $post->post_date, 0, 4 ) > 0 )

			$time = $post->post_date;

	}



	$name = $_FILES[$file_id]['name'];

	$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);



	if ( isset($file['error']) )

		return new WP_Error( 'upload_error', $file['error'] );



	$name_parts = pathinfo($name);

	$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );



	$url = $file['url'];

	$type = $file['type'];

	$file = $file['file'];

	$title = $name;

	$content = '';



	// use image exif/iptc data for title and caption defaults if possible

	if ( $image_meta = @wp_read_image_metadata($file) ) {

		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )

			$title = $image_meta['title'];

		if ( trim( $image_meta['caption'] ) )

			$content = $image_meta['caption'];

	}



	// Construct the attachment array

	$attachment = array_merge( array(

		'post_mime_type' => $type,

		'guid' => $url,

		'post_parent' => $post_id,

		'post_title' => $title,

		'post_content' => $content,

	), $post_data );



	// This should never be set as it would then overwrite an existing attachment.

	if ( isset( $attachment['ID'] ) )

		unset( $attachment['ID'] );



	// Save the data

	$id = wp_insert_attachment($attachment, $file, $post_id);

	if ( !is_wp_error($id) ) {

		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

	}



	return $id;



}

2351