Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andersonmadeira/5b3dfd30d065a5ca8bd0 to your computer and use it in GitHub Desktop.
Save andersonmadeira/5b3dfd30d065a5ca8bd0 to your computer and use it in GitHub Desktop.
A very simple custom media uploader that enables the uploader to change the author
<?php
/**
* Media Library administration panel.
*
* @package WordPress
* @subpackage Administration
*/
/** WordPress Administration Bootstrap */
require_once( './admin.php' );
// Se não tiver 'capability' de subir arquivos, encerra.
if ( !current_user_can('upload_files') )
wp_die( __( 'You do not have permission to upload files.' ) );
// inclui o header (barra lateral)
require_once( ABSPATH . 'wp-admin/admin-header.php' );
?>
<!-- HTML STUFF GENERATION -->
<h2>Upload por editor</h2>
<?php
// verificar se o form foi enviado.
if (!isset($_POST['formsent'])) {
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file-to-upload" id="file-upload" /><br>
<input type="text" name="texto" value="Digite seu texto"><br>
<?php
// filtros da listagem de usuários
$args = array(
'role' => 'cliente',
'order' => 'ASC');
// pega todos os usuários 'cliente'
$result_users = get_users( $args );
// constrói a dropdown list que vai conter os nomes e ids de clientes, pra selecionar o dono do arquivo.
echo "<select name='selected_author_id'>\n";
// texto que pede a seleção não faz parte do conjunto pra ser selecionado.
echo "<option value='' style='display:none'>Selecione o cliente</option>";
foreach ($result_users as $user_obj) {
echo "<option value='".$user_obj->ID."'>".$user_obj->display_name."</option>\n";
}
echo "</select><br>\n";
?>
<input type="hidden" name="formsent">
<input type="submit" value="Send">
</form>
<?php
} else {
// prepara o array de informações do attachment pra alterar, só vai ter mesmo o novo autor.
$post_data = array('post_author' => $_POST['selected_author_id']);
// delega o upload do arquivo pra função builtin do wp
$attachment_id = media_handle_upload('file-to-upload', $post->ID, $post_data);
// debug
echo "Mensagem enviada: " . $_POST['texto'] . "<br>";
echo "Cliente selecionado: " . $_POST['selected_author_id'] . "<br>";
// debug
echo "File uploaded: " . $attachment_id;
}
?>
<!-- FOOTER -->
<?php
include('./admin-footer.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment