<?php
/*
* @author Daan Vos de Wael
* @copyright Copyright (c) 2013, Daan Vos de Wael, http://www.daanvosdewael.com
* @license http://en.wikipedia.org/wiki/MIT_License The MIT License
*/
function gallery_metabox_enqueue($hook) {
if ( 'post.php' == $hook || 'post-new.php' == $hook ) {
wp_enqueue_script('jquery') ;
wp_enqueue_script('gallery-metabox', PLUGIN_EMME_GALLERY_URL. '/gallery-metabox/js/gallery-metabox.js', array('jquery', 'jquery-ui-sortable'));
wp_enqueue_style('gallery-metabox', PLUGIN_EMME_GALLERY_URL. '/gallery-metabox/css/gallery-metabox.css');
}
}
add_action('admin_enqueue_scripts', 'gallery_metabox_enqueue');
function add_gallery_metabox($post_type) {
$types = array('emme_gallery', 'page');
if (in_array($post_type, $types)) {
add_meta_box(
'gallery-metabox',
'Gallery',
'gallery_meta_callback',
$post_type,
'normal',
'high'
);
}
}
add_action('add_meta_boxes', 'add_gallery_metabox');
function gallery_meta_callback($post) {
wp_nonce_field( basename(__FILE__), 'gallery_meta_nonce' );
$ids = get_post_meta($post->ID, 'vdw_gallery_id', true);
?>
<div class="center switcher"><span class="txt">Visualizzazione:</span> <span class="option">Grid</span>
<label class="switch">
<input type="checkbox" >
<span class="slider round"></span>
</label>
<span class="option">List</span></div>
<table class="form-table viewGrid">
<tr><td>
<a class="gallery-add button" href="#" data-uploader-title="Aggiungi immagini alla Galleria" data-uploader-button-text="Aggiungi immagini">Aggiungi immagini</a>
<ul id="gallery-metabox-list">
<?php if ($ids) : foreach ($ids as $key => $value) : $image = wp_get_attachment_image_src($value); ?>
<li>
<div class="image">
<input type="hidden" name="vdw_gallery_id[<?php echo $key; ?>]" value="<?php echo $value; ?>">
<img class="image-preview" src="<?php echo $image[0]; ?>">
</div>
<div class="caption">
<label>Titolo</label>
<input type="text" name="titol<?=$value?>" value="<?= get_the_title($value); ?>" />
<label>Didascalia</label>
<textarea name="caption<?= $value; ?>" ><?= wp_get_attachment_caption($value); ?></textarea>
<input type="hidden" name="idArt[]" value="<?= $value; ?>" />
</div>
<div class="option">
<a class="change-image button button-small" href="#" data-uploader-title="Cambia immagine" data-uploader-button-text="Cambia immagine">Cambia immagine</a><br>
<small><a class="remove-image" href="#">Rimuovi immagine</a></small>
</div>
</li>
<?php endforeach; endif; ?>
</ul>
</td></tr>
</table>
<?php }
function gallery_meta_save($post_id) {
if (!isset($_POST['gallery_meta_nonce']) || !wp_verify_nonce($_POST['gallery_meta_nonce'], basename(__FILE__))) return;
if (!current_user_can('edit_post', $post_id)) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if(isset($_POST['idArt'])){
$idImmg=$_POST['idArt'];
if(is_array($idImmg)){
for($i=0;$i<count($idImmg);$i++)
{
$capt='caption'.$idImmg[$i];
$captVal=$_POST[$capt];
$capt2='titol'.$idImmg[$i];
$captVal2=$_POST[$capt2];
$the_post = array(
'ID' => $idImmg[$i],//the ID of the Post
'post_excerpt' => $captVal,
'post_title' => $captVal2,
);
wp_update_post($the_post);
}
}
}
if(isset($_POST['vdw_gallery_id'])) {
update_post_meta($post_id, 'vdw_gallery_id', $_POST['vdw_gallery_id']);
} else {
delete_post_meta($post_id, 'vdw_gallery_id');
}
}
add_action('save_post', 'gallery_meta_save');
?>
All system for education purposes only. For more tools: Telegram @jackleet