Files
super-simple-slideshow/super-simple-slideshow.php
T
2026-10-13 08:49:12 +00:00

123 lines
3.5 KiB
PHP

<?php
/*
Plugin Name: Super Simple Slideshow
Description: Super simple slideshow plugin for Wordpress.
Version: 1.0
Author: ai-bot
*/
if (!defined('ABSPATH')) exit;
add_action('admin_menu', function(){
add_menu_page('Super Simple Slideshow','Super Simple Slideshow','manage_options','super-simple-slideshow','super_simple_slideshow_admin');
});
add_action('admin_enqueue_scripts', function($hook){
if($hook !== 'toplevel_page_super-simple-slideshow') return;
wp_enqueue_media();
wp_enqueue_script('jquery');
});
function super_simple_slideshow_admin(){
if(isset($_POST['super_simple_ids'])){
update_option('super_simple_slideshow_ids', sanitize_text_field($_POST['super_simple_ids']));
echo '<div class="notice notice-success"><p>Lagra.</p></div>';
}
$ids = get_option('super_simple_slideshow_ids','');
?>
<div class="wrap">
<h1>Super Simple Slideshow</h1>
<form method="post">
<input type="hidden" id="super_simple_ids" name="super_simple_ids" value="<?php echo esc_attr($ids); ?>">
<p><button type="button" class="button button-primary" id="super_simple_select">Vel bilete</button></p>
<div id="preview">
<?php
if(!empty($ids)){
foreach(explode(',', $ids) as $id){
echo wp_get_attachment_image((int)$id,'thumbnail',false,array(
'style'=>'margin:5px;width:120px;height:120px;object-fit:cover;'
));
}
}
?>
</div>
<p><input type="submit" class="button button-primary" value="Lagre"></p>
</form>
</div>
<script>
jQuery(function($){
$('#super_simple_select').on('click', function(e){
e.preventDefault();
var frame = wp.media({
title:'Vel bilete',
multiple:true,
library:{type:'image'}
});
frame.on('select', function(){
var ids=[];
var html='';
frame.state().get('selection').each(function(item){
item=item.toJSON();
ids.push(item.id);
if(item.sizes && item.sizes.thumbnail){
html += '<img src="'+item.sizes.thumbnail.url+'" style="margin:5px;width:120px;height:120px;object-fit:cover;">';
}
});
$('#super_simple_ids').val(ids.join(','));
$('#preview').html(html);
});
frame.open();
});
});
</script>
<?php
}
add_shortcode('super_simple_slideshow', function(){
$ids = get_option('super_simple_slideshow_ids','');
if(empty($ids)) return '';
$ids = explode(',', $ids);
ob_start();
?>
<div class="super-simple-slideshow">
<?php foreach($ids as $i => $id): ?>
<?php echo wp_get_attachment_image((int)$id,'full',false,array(
'class' => $i === 0 ? 'super-simple-slide active' : 'super-simple-slide'
)); ?>
<?php endforeach; ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function(){
const slides=document.querySelectorAll('.super-simple-slideshow .super-simple-slide');
if(slides.length < 2) return;
let current=0;
setInterval(function(){
slides[current].classList.remove('active');
current=(current+1)%slides.length;
slides[current].classList.add('active');
},5000);
});
</script>
<?php
return ob_get_clean();
});