Initializing
Back to Projects
Year2024
DomainFullstack
AccessOpen Source
Complexity0 / 10
PHP/JavaScriptWordPressWordPress Snippet3DMedia
FullstackArchived

WordPress Code Snippets Collection

A collection of utility code snippets for WordPress including GLB 3D model viewer, photo sphere viewer, item rearranger, and more.

# WordPress Code Snippets Collection

A collection of specialized code snippets for WordPress that add advanced functionality like 3D model viewing, panorama viewers, and UI enhancements.

1. GLB 3D Model Viewer

Advanced 3D model viewer for displaying .glb/.gltf files in WordPress.

Structure

html
<div class="model-viewer-container">
  <model-viewer
    src="path/to/model.glb"
    alt="3D Model"
    auto-rotate
    camera-controls
    shadow-intensity="1"
    ar
    style="width: 100%; height: 500px;"
  >
  </model-viewer>
  
  <div class="model-controls">
    <button id="rotate-left"></button>
    <button id="zoom-in">+</button>
    <button id="zoom-out">-</button>
    <button id="reset-view">Reset</button>
  </div>
</div>

<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.1.1/model-viewer.min.js"></script>

Features

  • Auto-rotation
  • Touch/mouse controls
  • AR mode support (mobile)
  • Lighting control
  • Background removal option

2. Simple GLB Viewer

Lightweight 3D viewer without advanced features.

html
<canvas id="simple-3d-canvas" width="400" height="300"></canvas>
<script src="three.min.js"></script>
<script>
  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(75, 400/300, 0.1, 1000);
  const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('simple-3d-canvas') });
  
  // Load and display GLB model
  const loader = new THREE.GLTFLoader();
  loader.load('model.glb', (gltf) => {
    scene.add(gltf.scene);
  });
  
  function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
  }
  animate();
</script>

3. PhotoSphere Viewer

360° panorama viewer for immersive photo viewing.

html
<div class="photosphere-viewer">
  <div id="pano" style="width: 100%; height: 500px;"></div>
</div>

<link rel="stylesheet" href="photo-sphere-viewer.css">
<script src="photo-sphere-viewer.js"></script>
<script>
  const viewer = PhotoSphereViewer({
    container: document.getElementById('pano'),
    panorama: 'path/to/panorama.jpg',
    caption: '360° View',
    loadingImg: 'loader.gif',
    defaultZoomLvl: 0,
    minZoomLvl: 0,
    maxZoomLvl: 90
  });
</script>

Use Cases

  • Real estate property tours
  • Event venue showcases
  • Travel destination previews

4. Item Rearranger

Drag-and-drop interface for reordering list items.

Structure

html
<div class="item-rearranger" id="sortable-list">
  <div class="sortable-item" data-id="1">
    <span class="handle">⋮⋮</span>
    <span class="item-content">First Item</span>
  </div>
  <div class="sortable-item" data-id="2">
    <span class="handle">⋮⋮</span>
    <span class="item-content">Second Item</span>
  </div>
  <div class="sortable-item" data-id="3">
    <span class="handle">⋮⋮</span>
    <span class="item-content">Third Item</span>
  </div>
</div>

<button id="save-order">Save Order</button>

JavaScript

javascript
new Sortable(document.getElementById('sortable-list'), {
  handle: '.handle',
  animation: 150,
  onEnd: function() {
    // Update order array
    const order = [];
    document.querySelectorAll('.sortable-item').forEach(item => {
      order.push(item.dataset.id);
    });
    console.log('New order:', order);
  }
});

Features

  • Drag handles for reordering
  • Visual feedback during drag
  • Save order to database
  • Use for priority/order management

Installation

For Custom Themes

Add to functions.php:

php
// Enqueue necessary scripts
function enqueue_snippet_assets() {
    // For specific snippet pages
    if (is_page('3d-models')) {
        wp_enqueue_script('model-viewer', 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.1.1/model-viewer.min.js', [], '3.1.1', true);
    }
}
add_action('wp_enqueue_scripts', 'enqueue_snippet_assets');

For Elementor

  1. Add "Custom HTML" widget
  2. Paste snippet code
  3. Configure as needed

Library Dependencies

SnippetDependencies
GLB Viewer (Advanced)Google Model Viewer Web Component
GLB Viewer (Simple)Three.js
PhotoSpherePhoto Sphere Viewer.js
Item RearrangerSortable.js

Snippet Summary

SnippetTypePurpose
GLB 3D Model (Advanced)3D ViewerInteractive 3D model display with controls
GLB 3D Model (Simple)3D ViewerBasic 3D model display
PhotoSphere Viewer360° ViewerImmersive panorama viewing
Item RearrangerUIDrag-drop ordering interface

Architecture Feedback

Spotted a potential optimization or antipattern? Let me know.

Submit a Technical Suggestion