Skip to content

BuzzCrafted/media-redirect

Repository files navigation

Media Redirect

A WordPress plugin that redirects media file URLs to an external URL, perfect for local development environments.

Description

Media Redirect allows you to redirect all media file URLs (images, videos, documents) from your local WordPress installation to an external URL. This is particularly useful when developing locally but need to serve media files from a production or staging server.

The plugin intercepts WordPress media URL generation hooks and automatically replaces local upload URLs with your configured redirect URL.

Features

  • 🚀 Easy Configuration - Simple admin settings page
  • 🔄 Automatic Redirect - Hooks into WordPress media URL generation
  • 📸 Full Media Support - Works with images, srcsets, and attachment metadata
  • 🌍 Multilingual - Includes Russian translation (ru_RU)
  • ⚙️ Developer Friendly - Clean codebase with modern PHP practices

Requirements

  • WordPress 5.6 or higher
  • PHP 8.2 or higher

Installation

Manual Installation

  1. Download the plugin files
  2. Upload the media-redirect folder to /wp-content/plugins/ directory
  3. Activate the plugin through the 'Plugins' menu in WordPress
  4. Navigate to Settings → Media Redirect to configure the redirect URL

Via Composer

composer require dmitriko/media-redirect

Usage

  1. Activate the Plugin

    • Go to Plugins → Installed Plugins
    • Find "Media Redirect" and click "Activate"
  2. Configure Redirect URL

    • Navigate to Settings → Media Redirect
    • Enter the base URL where your media files are hosted (e.g., https://example.com)
    • Click "Save Changes"
  3. How It Works

    • The plugin automatically replaces local upload URLs with your configured redirect URL
    • Works with:
      • Attachment URLs (wp_get_attachment_url)
      • Image srcsets (wp_calculate_image_srcset)
      • Attachment metadata (wp_prepare_attachment_for_js)

Example

If you have a local URL like:

http://localhost/wp-content/uploads/2025/01/image.jpg

And configure redirect URL as:

https://production.example.com

The plugin will automatically redirect to:

https://production.example.com/wp-content/uploads/2025/01/image.jpg

Development

Prerequisites

  • Node.js and npm
  • Composer
  • PHP 8.2+

Setup

# Install PHP dependencies
composer install

# Install Node dependencies
npm install

Build

# Build assets
npm run build

# Create plugin bundle
npm run bundle

Available Scripts

  • npm run build - Build production assets
  • npm run start - Start development mode with watch
  • npm run bundle - Create plugin zip file
  • npm run lint:js - Lint JavaScript files
  • npm run lint:css - Lint CSS files

Translation

The plugin uses WordPress i18n standards. To update translations:

# Generate POT file
wp i18n make-pot . languages/media-redirect.pot --domain=media-redirect

# Update existing translation
msgmerge --update languages/media-redirect-ru_RU.po languages/media-redirect.pot

# Compile MO file
msgfmt -o languages/media-redirect-ru_RU.mo languages/media-redirect-ru_RU.po

File Structure

media-redirect/
├── assets/              # Compiled assets (CSS, JS)
├── bundle/              # Plugin bundle files
├── inc/                 # PHP source files
│   ├── Admin/           # Admin interface classes
│   ├── EventManagement/ # Event subscriber interfaces
│   ├── Generator/       # Template generator
│   ├── Service/         # Service classes
│   ├── Subscriber/      # Event subscribers
│   └── ...
├── languages/           # Translation files
├── src/                 # Source assets (SCSS, JS)
├── templates/           # PHP/Twig templates
├── vendor/              # Composer dependencies
├── media-redirect.php   # Main plugin file
└── README.md           # This file

Architecture

The plugin follows a clean architecture pattern:

  • Event-Driven: Uses WordPress hooks and filters
  • Service Layer: Business logic in service classes
  • Template System: Twig templating for admin pages
  • Dependency Injection: Constructor injection for dependencies

Support

License

GPL v2 or later

Copyright (c) 2025 Dmitry Kokorin

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Changelog

1.0.0

  • Initial release
  • Basic media redirect functionality
  • Admin settings page
  • Russian translation support

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published