Add super-simple-slideshow.php

This commit is contained in:
ai-bot committed 2026-10-12 19:41:37 +00:00
commit ed3def6e48
1 file changed
+122
+122
View File
@@ -0,0 +1,122 @@
<?php
/*
Plugin Name: Super Simple Slideshow
Description: Super simple slideshow plugin for Wordpress.
Version: 1.0
Author: Sondre Olav
*/
if (!defined('ABSPATH')) exit;
add_action('admin_menu', function(){
add_menu_page('NTKF Slideshow','NTKF Slideshow','manage_options','ntkf-slideshow','ntkf_slideshow_admin');
});
add_action('admin_enqueue_scripts', function($hook){
if($hook !== 'toplevel_page_ntkf-slideshow') return;
wp_enqueue_media();
wp_enqueue_script('jquery');
});
function ntkf_slideshow_admin(){
if(isset($_POST['ntkf_ids'])){
update_option('ntkf_slideshow_ids', sanitize_text_field($_POST['ntkf_ids']));
echo '<div class="notice notice-success"><p>Lagra.</p></div>';
}
$ids = get_option('ntkf_slideshow_ids','');
?>
<div class="wrap">
<h1>NTKF Slideshow</h1>
<form method="post">
<input type="hidden" id="ntkf_ids" name="ntkf_ids" value="<?php echo esc_attr($ids); ?>">
<p><button type="button" class="button button-primary" id="ntkf_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($){
$('#ntkf_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;">';
}
});
$('#ntkf_ids').val(ids.join(','));
$('#preview').html(html);
});
frame.open();
});
});
</script>
<?php
}
add_shortcode('ntkf_slideshow', function(){
$ids = get_option('ntkf_slideshow_ids','');
if(empty($ids)) return '';
$ids = explode(',', $ids);
ob_start();
?>
<div class="ntkf-slideshow">
<?php foreach($ids as $i => $id): ?>
<?php echo wp_get_attachment_image((int)$id,'full',false,array(
'class' => $i === 0 ? 'ntkf-slide active' : 'ntkf-slide'
)); ?>
<?php endforeach; ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function(){
const slides=document.querySelectorAll('.ntkf-slideshow .ntkf-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();
});