WordPressに簡易的なメディアアップローダーを追加する

//
//add media uploading sysytem
function custom_media_menu()
{
	add_media_page('Custom Media Upload', 'Custom Media Upload', 'manage_options', 'custom-media-upload', 'custom_media_upload_callback');
}
add_action('admin_menu', 'custom_media_menu');

function custom_media_upload_callback()
{
?>
	<div class="wrap">
		<h1>Custom Media Upload</h1>
		<form method="post" enctype="multipart/form-data">
			<input type="file" name="custom_media_file" />
			<input type="submit" value="Upload" />
		</form>

		<?php

		// Handle the file upload
		if (isset($_FILES['custom_media_file']) && !empty($_FILES['custom_media_file']['name'])) {
			// Upload the file to the custom directory
			$uploaded_file = wp_handle_upload($_FILES['custom_media_file'], array('test_form' => false));
			ob_start();
			if (!isset($uploaded_file['error'])) {
				// Create an attachment record for the uploaded file
				$attachment = array(
					'post_mime_type' => $uploaded_file['type'],
					'post_title' => sanitize_file_name($uploaded_file['file']),
					'post_content' => '',
					'post_status' => 'inherit'
				);
				// Insert the attachment record into the database
				$attachment_id = wp_insert_attachment($attachment, $uploaded_file['file']);

				if (!is_wp_error($attachment_id)) {
					// Success!
					echo '<p>Image uploaded successfully!</p>';
					echo '<img class="uploaded_img" src="' . wp_get_attachment_url($attachment_id) . '" alt="Uploaded Image" />';
				} else {
					// Error
					echo '<p>Image upload failed. Please try again.</p>';
				}
			} else {
				// Error
				echo '<p>Image upload failed. Please try again.</p>';
			}
			$ret = ob_get_clean();
			echo $ret;
		}
		// Handle the file deletion
		if (isset($_GET['delete']) && !empty($_GET['delete'])) {
			// Get the attachment ID
			$attachment_id = (int) $_GET['delete'];

			// Check if the attachment exists
			$attachment = get_post($attachment_id);
			if ($attachment) {
				// Delete the attachment
				wp_delete_attachment($attachment_id, true);

				// Redirect back to the current page
				ob_start();
				wp_redirect(remove_query_arg('delete'));
				ob_get_clean();
			} else {
				// Attachment not found
				echo '<p>Attachment not found.</p>';
			}
		}

		// Display list of uploaded files
		$args = array(
			'post_type' => 'attachment',
			'post_status' => 'inherit',
			'posts_per_page' => -1,
		);
		$attachments = get_posts($args);
		if ($attachments) {
			ob_start();
			echo '<h2>List of Uploaded Files</h2>';
			echo '<ul>';
			foreach ($attachments as $attachment) {
				echo '<li><a href="' . wp_get_attachment_url($attachment->ID) . '">' . get_the_title($attachment->ID) . '</a> <a href="' . add_query_arg('delete', $attachment->ID) . '">Delete</a></li>';
			}
			echo '</ul>';
		} else {
			echo '<p>No files uploaded yet.</p>';
		}
		$ret = ob_get_clean();
		echo $ret;
		?>
	</div>
<?php
}



//
// change uploading directory when the custom upload
function edd_set_upload_dir($upload)
{
	$upload['subdir'] = '/gallery';
	$upload['path'] = $upload['basedir'] . '/gallery';
	$upload['url']  = $upload['baseurl'] . '/gallery';
	return $upload;
}

function edd_change_downloads_upload_dir()
{
	if (isset($_FILES['custom_media_file']) && !empty($_FILES['custom_media_file']['name'])) {
		add_filter('upload_dir', 'edd_set_upload_dir');
	}
}
add_action('admin_init', 'edd_change_downloads_upload_dir', 999);

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です