admin管理员组文章数量:1405570
hello I am trying to add new tab to the media library uploader:
My plugin currently only shows on the old wordpress media library:
How do I add the tab to the select/upload media dialog? Here is the code of the full plugin below:
<?php
/*
Plugin Name: Sologet Upload Media From Url
Plugin URI:
Author: Solomon (Sologet)
Author URI:
Version: 1.0.0
Description: This plugin allows you to fetch the remote audio and save to your local wordpress without stress.
*/
class SologetUploadMediaFromUrl {
var $audioName;
function SologetUploadMediaFromUrl(){$this->__construct();}
function __construct(){
global $wp_version;
if ($wp_version < 3.5) {
if ( basename($_SERVER['PHP_SELF']) != "media-upload.php" ) return;
} else {
if ( basename($_SERVER['PHP_SELF']) != "media-upload.php" && basename($_SERVER['PHP_SELF']) != "post.php" && basename($_SERVER['PHP_SELF']) != "post-new.php") return;
}
add_filter("media_upload_tabs",array(&$this,"build_tab"));
add_action("media_upload_SologetUploadMediaFromUrl", array(&$this, "menu_handle"));
}
/*
* Merge an array into middle of another array
*
* @param array $array the array to insert
* @param array $insert array to be inserted
* @param int $position index of array
*/
function array_insert(&$array, $insert, $position) {
settype($array, "array");
settype($insert, "array");
settype($position, "int");
//if pos is start, just merge them
if($position==0) {
$array = array_merge($insert, $array);
} else {
//if pos is end just merge them
if($position >= (count($array)-1)) {
$array = array_merge($array, $insert);
} else {
//split into head and tail, then merge head+inserted bit+tail
$head = array_slice($array, 0, $position);
$tail = array_slice($array, $position);
$array = array_merge($head, $insert, $tail);
}
}
return $array;
}
function build_tab($tabs) {
$newtab = array('SologetUploadMediaFromUrl' => __('Sologet Upload Media From Url', 'SologetUploadMediaFromUrl'));
return $this->array_insert($tabs, $newtab, 2);
//return array_merge($tabs,$newtab);
}
function menu_handle() {
return wp_iframe(array($this,"media_process"));
}
function fetch_audio($url) {
if ( function_exists("curl_init") ) {
return $this->curl_fetch_audio($url);
} elseif ( ini_get("allow_url_fopen") ) {
return $this->fopen_fetch_audio($url);
}
}
function curl_fetch_audio($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$audio = curl_exec($ch);
curl_close($ch);
return $audio;
}
function fopen_fetch_audio($url) {
$audio = file_get_contents($url, false, $context);
return $audio;
}
function media_process() {
if ( $_POST['audiourl'] ) {
$audiourl = $_POST['audiourl'];
$audiourl = stripslashes($audiourl);
$uploads = wp_upload_dir();
$post_id = isset($_GET['post_id'])? (int) $_GET['post_id'] : 0;
$ext = pathinfo( basename($audiourl) , PATHINFO_EXTENSION);
$newfilename = $_POST['newfilename'] ? $_POST['newfilename'] . "." . $ext : basename($audiourl);
$filename = wp_unique_filename( $uploads['path'], $newfilename, $unique_filename_callback = null );
$wp_filetype = wp_check_filetype($filename, null );
$fullpathfilename = $uploads['path'] . "/" . $filename;
try {
if ( !substr_count($wp_filetype['type'], "image") && !substr_count($wp_filetype['type'], "audio")) {
throw new Exception( basename($audiourl) . ' is not a valid ' . $wp_filetype['type'] . '' );
}
$audio_string = $this->fetch_audio($audiourl);
$fileSaved = file_put_contents($uploads['path'] . "/" . $filename, $audio_string);
if ( !$fileSaved ) {
throw new Exception("The file cannot be saved.");
}
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $uploads['url'] . "/" . $filename
);
$attach_id = wp_insert_attachment( $attachment, $fullpathfilename, $post_id );
if ( !$attach_id ) {
throw new Exception("Failed to save record into database.");
}
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullpathfilename );
wp_update_attachment_metadata( $attach_id, $attach_data );
} catch (Exception $e) {
$error = '<div id="message" class="error"><p>' . $e->getMessage() . '</p></div>';
}
}
media_upload_header();
if ( !function_exists("curl_init") && !ini_get("allow_url_fopen") ) {
echo '<div id="message" class="error"><p><b>cURL</b> or <b>allow_url_fopen</b> needs to be enabled. Please consult your server Administrator.</p></div>';
} elseif ( $error ) {
echo $error;
} else {
if ( $fileSaved && $attach_id ) {
echo '<div id="message" class="updated"><p>File saved.</p></div>';
}
}
?>
<form action="" method="post" id="audio-form" class="media-upload-form type-form">
<h3 clas s="media-title">Grab Audio Or Image from URL</h3>
<div class="describe">Audio Or Image URL:
<input id="src" type="text" name="audiourl"><br/>
Save as (optional) <input type="text" name="newfilename" style="width:200px">
<input type="submit" class="button" value="Grab">
</div>
</form>
<?php
if ( $attach_id ) {
$this->media_upload_type_form("audio", $errors, $attach_id);
}
?>
<div style="padding:10px;"><a href="/" target="_blank" rel="noopener"><strong>Sologet</strong></a> © <a href=""><strong>Solomon Alozie</strong></a>
<a href=""><strong>Hiding it difficulty to use this plugin or find any error </strong><strong>refer here for fix or help</strong></a>
</div>
<?php
}
/*
* modification from media.php function
*
* @param unknown_type $type
* @param unknown_type $errors
* @param unknown_type $id
*/
function media_upload_type_form($type = 'file', $errors = null, $id = null) {
$post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
$form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
$form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
?>
<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
<input type="submit" class="hidden" name="save" value="" />
<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
<?php wp_nonce_field('media-form'); ?>
<script type="text/javascript">
//<![CDATA[
jQuery(function($){
var preloaded = $(".media-item.preloaded");
if ( preloaded.length > 0 ) {
preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
}
updateMediaForm();
});
//]]>
</script>
<div id="media-items">
<?php
if ( $id ) {
if ( !is_wp_error($id) ) {
add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
echo get_media_items( $id, $errors );
} else {
echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
exit;
}
}
?>
</div>
<p class="savebutton ml-submit">
<input type="submit" class="button" name="save" value="<?php esc_attr_e( 'Save all changes' ); ?>" />
</p>
</form>
<?php
}
}
new SologetUploadMediaFromUrl();
?>
本文标签: Add tab on new media library
版权声明:本文标题:Add tab on new media library 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744889321a2630675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论