Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active December 14, 2015 16:59
Show Gist options
  • Save tcelestino/5118915 to your computer and use it in GitHub Desktop.
Save tcelestino/5118915 to your computer and use it in GitHub Desktop.
show metabox in wordpress save data

Criando um metabox no WordPress

Um exemplo de como criar um metabox para upload no WordPress

<?php
/*
cria os meta boxes
@use cria uma nova linha com o metodo add_meta_box();
*/
add_action('add_meta_boxes', 'addCustomBoxes');
function addCustomBoxes() {
add_meta_box("arquivo_upload", "Upload do Arquivo", "metaBoxUpload", "aqui-seu-post-type-ou-page-ou-post", "normal", "default");
}
/*
inclue o html dentro do meta box upload do arquivo
*/
function metaBoxUpload($post, $metabox) {
include('metabox_upload.php'); // html do metabox a ser incluido no admin.
}
/*
salva os valores nos campos
*/
add_action('save_post', 'saveData');
function saveData($postID) {
if ($_POST && $_POST["action"] != "autosave"){
update_post_meta($postID, '_arquivo', $_POST['_arquivo'], false);
}
}
/*
printa os valores na tela de um campo especifico
*/
function printArrayField($field, $print=true){
global $post;
$val = get_post_meta($post->ID, $field, true);
if($print){
echo $val;
} else {
return $val;
}
}
<script>
jQuery(document).ready(function() {
var formfield;
var header_clicked = false;
jQuery('#btn-upload-arquivo' ).click( function() {
formfield = jQuery( '#_arquivo' ).attr( 'name' );
tb_show( '', 'media-upload.php?TB_iframe=true' );
header_clicked = true;
return false;
});
// Guarda o uploader original
window.original_send_to_editor = window.send_to_editor;
// Sobrescreve a funcao nativa e preenche o campo com a URL
window.send_to_editor = function( html ) {
if ( header_clicked ) {
fileurl = jQuery( html ).attr( 'href' );
jQuery( '#_arquivo' ).val( fileurl );
header_clicked = false;
tb_remove();
}
else
{
window.original_send_to_editor( html );
}
}
});
</script>
<style>
<!--
#metabox-side label {display:block; margin-bottom:10px}
-->
</style>
<div id="metabox-side">
<label for="_coordinatorName">Arquivo aqui</label>
<input type="text" value="<?php printArrayField ('_arquivo'); ?>" name="_arquivo" id="_arquivo" class="large-text" />
<button type="button" class="button" id="btn-upload-arquivo">Fazer upload</button>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment