nikdjem

Pdf_carousel

Sep 12th, 2025 (edited)
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: PDF Carousel
  4.  * Description: A lightweight plugin to display PDFs in a carousel (uses Slick). Includes admin settings for URLs, size, and autoplay.
  5.  * Version: 1.3
  6.  * Author: Nikolay Djemerenov
  7.  */
  8.  
  9. // Exit if accessed directly
  10. if ( ! defined( 'ABSPATH' ) ) exit;
  11.  
  12. /**
  13.  * Enqueue Slick Carousel assets
  14.  */
  15. function pdf_carousel_enqueue_scripts() {
  16.     // Slick CSS
  17.     wp_enqueue_style( 'slick-css', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.css' );
  18.     wp_enqueue_style( 'slick-theme-css', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css' );
  19.  
  20.     // Slick JS
  21.     wp_enqueue_script( 'slick-js', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js', array('jquery'), null, true );
  22.  
  23.     // Get options
  24.     $autoplay       = get_option( 'pdf_carousel_autoplay', '1' );
  25.     $autoplay_speed = get_option( 'pdf_carousel_autoplay_speed', '4000' );
  26.  
  27.     // Init script
  28.     $autoplay_js = ( $autoplay === '1' ) ? 'true' : 'false';
  29.     $speed_js    = intval( $autoplay_speed );
  30.  
  31.     wp_add_inline_script( 'slick-js', "
  32.        jQuery(document).ready(function($){
  33.            $('.pdf-carousel').slick({
  34.                infinite: true,
  35.                slidesToShow: 1,
  36.                autoplay: $autoplay_js,
  37.                autoplaySpeed: $speed_js,
  38.                arrows: true,
  39.                dots: true
  40.            });
  41.        });
  42.    " );
  43. }
  44. add_action( 'wp_enqueue_scripts', 'pdf_carousel_enqueue_scripts' );
  45.  
  46. /**
  47.  * Shortcode [pdf_carousel]
  48.  */
  49. function pdf_carousel_shortcode() {
  50.     // Get settings
  51.     $pdfs   = get_option( 'pdf_carousel_urls', '' );
  52.     $width  = get_option( 'pdf_carousel_width', '300' );
  53.     $height = get_option( 'pdf_carousel_height', '400' );
  54.  
  55.     if ( empty( $pdfs ) ) return '<p>No PDFs added yet.</p>';
  56.  
  57.     $pdfs = array_map( 'trim', explode( ',', $pdfs ) );
  58.  
  59.     $output = '<div class="pdf-carousel">';
  60.     foreach ( $pdfs as $pdf ) {
  61.         $output .= '<div><iframe src="' . esc_url( $pdf ) . '" width="' . intval( $width ) . '" height="' . intval( $height ) . '" style="border:none;border-radius:8px;"></iframe></div>';
  62.     }
  63.     $output .= '</div>';
  64.  
  65.     return $output;
  66. }
  67. add_shortcode( 'pdf_carousel', 'pdf_carousel_shortcode' );
  68.  
  69. /**
  70.  * Admin Menu
  71.  */
  72. function pdf_carousel_add_admin_menu() {
  73.     add_options_page(
  74.         'PDF Carousel',
  75.         'PDF Carousel',
  76.         'manage_options',
  77.         'pdf-carousel',
  78.         'pdf_carousel_options_page'
  79.     );
  80. }
  81. add_action( 'admin_menu', 'pdf_carousel_add_admin_menu' );
  82.  
  83. /**
  84.  * Register Settings
  85.  */
  86. function pdf_carousel_settings_init() {
  87.     register_setting( 'pdfCarousel', 'pdf_carousel_urls' );
  88.     register_setting( 'pdfCarousel', 'pdf_carousel_width' );
  89.     register_setting( 'pdfCarousel', 'pdf_carousel_height' );
  90.     register_setting( 'pdfCarousel', 'pdf_carousel_autoplay' );
  91.     register_setting( 'pdfCarousel', 'pdf_carousel_autoplay_speed' );
  92.  
  93.     add_settings_section(
  94.         'pdf_carousel_section',
  95.         __( 'PDF Carousel Settings', 'pdf-carousel' ),
  96.         function() {
  97.             echo '<p>Enter PDF URLs, size, and carousel behavior.</p>';
  98.         },
  99.         'pdfCarousel'
  100.     );
  101.  
  102.     add_settings_field(
  103.         'pdf_carousel_urls',
  104.         __( 'PDF URLs', 'pdf-carousel' ),
  105.         'pdf_carousel_urls_render',
  106.         'pdfCarousel',
  107.         'pdf_carousel_section'
  108.     );
  109.  
  110.     add_settings_field(
  111.         'pdf_carousel_width',
  112.         __( 'PDF Width (px)', 'pdf-carousel' ),
  113.         'pdf_carousel_width_render',
  114.         'pdfCarousel',
  115.         'pdf_carousel_section'
  116.     );
  117.  
  118.     add_settings_field(
  119.         'pdf_carousel_height',
  120.         __( 'PDF Height (px)', 'pdf-carousel' ),
  121.         'pdf_carousel_height_render',
  122.         'pdfCarousel',
  123.         'pdf_carousel_section'
  124.     );
  125.  
  126.     add_settings_field(
  127.         'pdf_carousel_autoplay',
  128.         __( 'Autoplay', 'pdf-carousel' ),
  129.         'pdf_carousel_autoplay_render',
  130.         'pdfCarousel',
  131.         'pdf_carousel_section'
  132.     );
  133.  
  134.     add_settings_field(
  135.         'pdf_carousel_autoplay_speed',
  136.         __( 'Autoplay Speed (ms)', 'pdf-carousel' ),
  137.         'pdf_carousel_autoplay_speed_render',
  138.         'pdfCarousel',
  139.         'pdf_carousel_section'
  140.     );
  141. }
  142. add_action( 'admin_init', 'pdf_carousel_settings_init' );
  143.  
  144. /**
  145.  * Render Inputs
  146.  */
  147. function pdf_carousel_urls_render() {
  148.     $urls = get_option( 'pdf_carousel_urls', '' );
  149.     ?>
  150.     <textarea name="pdf_carousel_urls" rows="5" cols="60"><?php echo esc_textarea( $urls ); ?></textarea>
  151.     <p class="description">Example: /wp-content/uploads/file1.pdf, /wp-content/uploads/file2.pdf</p>
  152.     <?php
  153. }
  154.  
  155. function pdf_carousel_width_render() {
  156.     $width = get_option( 'pdf_carousel_width', '300' );
  157.     ?>
  158.     <input type="number" name="pdf_carousel_width" value="<?php echo esc_attr( $width ); ?>" min="100" max="1200"> px
  159.     <?php
  160. }
  161.  
  162. function pdf_carousel_height_render() {
  163.     $height = get_option( 'pdf_carousel_height', '400' );
  164.     ?>
  165.     <input type="number" name="pdf_carousel_height" value="<?php echo esc_attr( $height ); ?>" min="100" max="2000"> px
  166.     <?php
  167. }
  168.  
  169. function pdf_carousel_autoplay_render() {
  170.     $autoplay = get_option( 'pdf_carousel_autoplay', '1' );
  171.     ?>
  172.     <select name="pdf_carousel_autoplay">
  173.         <option value="1" <?php selected( $autoplay, '1' ); ?>>Enabled</option>
  174.         <option value="0" <?php selected( $autoplay, '0' ); ?>>Disabled</option>
  175.     </select>
  176.     <?php
  177. }
  178.  
  179. function pdf_carousel_autoplay_speed_render() {
  180.     $speed = get_option( 'pdf_carousel_autoplay_speed', '4000' );
  181.     ?>
  182.     <input type="number" name="pdf_carousel_autoplay_speed" value="<?php echo esc_attr( $speed ); ?>" min="1000" step="500"> ms
  183.     <p class="description">Default: 4000 ms (4 seconds)</p>
  184.     <?php
  185. }
  186.  
  187. /**
  188.  * Options Page
  189.  */
  190. function pdf_carousel_options_page() {
  191.     ?>
  192.     <div class="wrap">
  193.         <h1>PDF Carousel</h1>
  194.         <form action="options.php" method="post">
  195.             <?php
  196.             settings_fields( 'pdfCarousel' );
  197.             do_settings_sections( 'pdfCarousel' );
  198.             submit_button();
  199.             ?>
  200.         </form>
  201.     </div>
  202.     <?php
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment