Bon après quelques tests et un certain nombres d'heures de prise de tête j'ai la solution.
On crée une Metaboxe
function media_gallery_table_metabox($post){
global $post;
$id = $post->ID ;
//esc les attributs html, de facon a lire correctement la valeur enregistrée.
$val = esc_html(get_post_meta($id,'_ma_valeur',true));
// on envoi l'url du post.
$modal_update_href = esc_url( add_query_arg( array(
'post' => $id,
'action' => 'edit',
), admin_url('post.php') ) );
// Champ pour enregistrer le shortcode récupéré de l'éditeur.
echo '<input id="mon_champ" type="hidden" name="mon_champ" value="'.$val.'" />';
?>
<a id="upload-and-attach-link" class="button" data-update-link="<?php echo esc_attr( $modal_update_href ); ?>">
<?php _e( 'Ajouter ou joindre un média' );?>
</a>
<?php
}
On enregistre la Metaboxe
function save_media_gallery($post_ID){
// si la metabox est définie, on sauvegarde sa valeur
if(isset($_POST'mon_champ'])){
update_post_meta($post_ID,'_ma_valeur',$_POST'mon_champ']);
//Pour insérer la valeur du champ dans le contenu du post sans passer par l'éditeur.
$my_post = array();
$my_post'id'] = $post_ID;
$my_post'post_content'] = $_POST'mon_champ'];
//Evite de faire une boulce infini avec le wp_update_post. On enleve l'action save_post, pour la remettre plus loin.
remove_action('save_post', 'save_media_gallery');
wp_update_post($my_post);
add_action('save_post', 'save_media_gallery');
}
}
add_action('save_post','save_media_gallery');
La fonction pour envoyer au wp.media si un shortcode a déja été posté.
function media_view_settings($settings) {
global $post;
// Create our own shortcode string here
$shortcode = get_post_meta($post->ID,'_ma_valeur',true);
$settings'gallery_shortcode'] = array('shortcode' => $shortcode);
return $settings;
}
add_filter( 'media_view_settings', 'media_view_settings');
la fonction JS complète pour utiliser l'uploader de média directement sur la galerie.
wp.media.delageWebIdWorkflow = {
frame: function() {
if ( this._frame )
return this._frame;
var selection = this.select();
this._frame = wp.media({
id: 'my-frame',
frame: 'post',
state: 'gallery-edit',
title: wp.media.view.l10n.editGalleryTitle,
editing: true,
multiple: true,
selection: selection
});
// On envoi les données sélectionnée dans le champ.
this._frame.on( 'update', function( selection ) {
var gallery_shortcode = wp.media.gallery.shortcode(selection).string();
$('#mon_champ').val(gallery_shortcode);
console.log(wp.media.view.settings.dj.shortcode);
});
return this._frame;
},
select: function(){
var shortcode = wp.shortcode.next( 'gallery', wp.media.view.settings.dj.shortcode ),
defaultPostId = wp.media.gallery.defaults.id,
attachments, selection;
// Bail if we didn't match the shortcode or all of the content.
if ( ! shortcode )
return;
// Ignore the rest of the match object.
shortcode = shortcode.shortcode;
if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) )
shortcode.set( 'id', defaultPostId );
attachments = wp.media.gallery.attachments( shortcode );
selection = new wp.media.model.Selection( attachments.models, {
props: attachments.props.toJSON(),
multiple: true
});
selection.gallery = attachments.gallery;
// Fetch the query's attachments, and then break ties from the
// query to allow for sorting.
selection.more().done( function() {
// Break ties with the query.
selection.props.set({ query: false });
selection.unmirror();
selection.props.unset('orderby');
});
return selection;
},
init: function() {
$('#upload-and-attach-link').click( function( event ) {
event.preventDefault();
wp.media.delageWebIdWorkflow.frame().open();
});
}
};
$(document).ready(function(){
$( wp.media.delageWebIdWorkflow.init );
});
Voila, en gros reste plus qu'a améliorer en incluant une sauvegarde ajax comme shibashake, mais c'est déjàpas mal.