Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: PDF Carousel
- * Description: A lightweight plugin to display PDFs in a carousel (uses Slick). Includes admin settings for URLs, size, and autoplay.
- * Version: 1.3
- * Author: Nikolay Djemerenov
- */
- // Exit if accessed directly
- if ( ! defined( 'ABSPATH' ) ) exit;
- /**
- * Enqueue Slick Carousel assets
- */
- function pdf_carousel_enqueue_scripts() {
- // Slick CSS
- wp_enqueue_style( 'slick-theme-css', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css' );
- // Slick JS
- wp_enqueue_script( 'slick-js', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js', array('jquery'), null, true );
- // Get options
- $autoplay = get_option( 'pdf_carousel_autoplay', '1' );
- $autoplay_speed = get_option( 'pdf_carousel_autoplay_speed', '4000' );
- // Init script
- $autoplay_js = ( $autoplay === '1' ) ? 'true' : 'false';
- $speed_js = intval( $autoplay_speed );
- wp_add_inline_script( 'slick-js', "
- jQuery(document).ready(function($){
- $('.pdf-carousel').slick({
- infinite: true,
- slidesToShow: 1,
- autoplay: $autoplay_js,
- autoplaySpeed: $speed_js,
- arrows: true,
- dots: true
- });
- });
- " );
- }
- add_action( 'wp_enqueue_scripts', 'pdf_carousel_enqueue_scripts' );
- /**
- * Shortcode [pdf_carousel]
- */
- function pdf_carousel_shortcode() {
- // Get settings
- $pdfs = get_option( 'pdf_carousel_urls', '' );
- $width = get_option( 'pdf_carousel_width', '300' );
- $height = get_option( 'pdf_carousel_height', '400' );
- if ( empty( $pdfs ) ) return '<p>No PDFs added yet.</p>';
- $pdfs = array_map( 'trim', explode( ',', $pdfs ) );
- $output = '<div class="pdf-carousel">';
- foreach ( $pdfs as $pdf ) {
- $output .= '<div><iframe src="' . esc_url( $pdf ) . '" width="' . intval( $width ) . '" height="' . intval( $height ) . '" style="border:none;border-radius:8px;"></iframe></div>';
- }
- $output .= '</div>';
- return $output;
- }
- add_shortcode( 'pdf_carousel', 'pdf_carousel_shortcode' );
- /**
- * Admin Menu
- */
- function pdf_carousel_add_admin_menu() {
- add_options_page(
- 'PDF Carousel',
- 'PDF Carousel',
- 'manage_options',
- 'pdf-carousel',
- 'pdf_carousel_options_page'
- );
- }
- add_action( 'admin_menu', 'pdf_carousel_add_admin_menu' );
- /**
- * Register Settings
- */
- function pdf_carousel_settings_init() {
- register_setting( 'pdfCarousel', 'pdf_carousel_urls' );
- register_setting( 'pdfCarousel', 'pdf_carousel_width' );
- register_setting( 'pdfCarousel', 'pdf_carousel_height' );
- register_setting( 'pdfCarousel', 'pdf_carousel_autoplay' );
- register_setting( 'pdfCarousel', 'pdf_carousel_autoplay_speed' );
- add_settings_section(
- 'pdf_carousel_section',
- __( 'PDF Carousel Settings', 'pdf-carousel' ),
- function() {
- echo '<p>Enter PDF URLs, size, and carousel behavior.</p>';
- },
- 'pdfCarousel'
- );
- add_settings_field(
- 'pdf_carousel_urls',
- __( 'PDF URLs', 'pdf-carousel' ),
- 'pdf_carousel_urls_render',
- 'pdfCarousel',
- 'pdf_carousel_section'
- );
- add_settings_field(
- 'pdf_carousel_width',
- __( 'PDF Width (px)', 'pdf-carousel' ),
- 'pdf_carousel_width_render',
- 'pdfCarousel',
- 'pdf_carousel_section'
- );
- add_settings_field(
- 'pdf_carousel_height',
- __( 'PDF Height (px)', 'pdf-carousel' ),
- 'pdf_carousel_height_render',
- 'pdfCarousel',
- 'pdf_carousel_section'
- );
- add_settings_field(
- 'pdf_carousel_autoplay',
- __( 'Autoplay', 'pdf-carousel' ),
- 'pdf_carousel_autoplay_render',
- 'pdfCarousel',
- 'pdf_carousel_section'
- );
- add_settings_field(
- 'pdf_carousel_autoplay_speed',
- __( 'Autoplay Speed (ms)', 'pdf-carousel' ),
- 'pdf_carousel_autoplay_speed_render',
- 'pdfCarousel',
- 'pdf_carousel_section'
- );
- }
- add_action( 'admin_init', 'pdf_carousel_settings_init' );
- /**
- * Render Inputs
- */
- function pdf_carousel_urls_render() {
- $urls = get_option( 'pdf_carousel_urls', '' );
- ?>
- <textarea name="pdf_carousel_urls" rows="5" cols="60"><?php echo esc_textarea( $urls ); ?></textarea>
- <p class="description">Example: /wp-content/uploads/file1.pdf, /wp-content/uploads/file2.pdf</p>
- <?php
- }
- function pdf_carousel_width_render() {
- $width = get_option( 'pdf_carousel_width', '300' );
- ?>
- <input type="number" name="pdf_carousel_width" value="<?php echo esc_attr( $width ); ?>" min="100" max="1200"> px
- <?php
- }
- function pdf_carousel_height_render() {
- $height = get_option( 'pdf_carousel_height', '400' );
- ?>
- <input type="number" name="pdf_carousel_height" value="<?php echo esc_attr( $height ); ?>" min="100" max="2000"> px
- <?php
- }
- function pdf_carousel_autoplay_render() {
- $autoplay = get_option( 'pdf_carousel_autoplay', '1' );
- ?>
- <select name="pdf_carousel_autoplay">
- <option value="1" <?php selected( $autoplay, '1' ); ?>>Enabled</option>
- <option value="0" <?php selected( $autoplay, '0' ); ?>>Disabled</option>
- </select>
- <?php
- }
- function pdf_carousel_autoplay_speed_render() {
- $speed = get_option( 'pdf_carousel_autoplay_speed', '4000' );
- ?>
- <input type="number" name="pdf_carousel_autoplay_speed" value="<?php echo esc_attr( $speed ); ?>" min="1000" step="500"> ms
- <p class="description">Default: 4000 ms (4 seconds)</p>
- <?php
- }
- /**
- * Options Page
- */
- function pdf_carousel_options_page() {
- ?>
- <div class="wrap">
- <h1>PDF Carousel</h1>
- <form action="options.php" method="post">
- <?php
- settings_fields( 'pdfCarousel' );
- do_settings_sections( 'pdfCarousel' );
- submit_button();
- ?>
- </form>
- </div>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment