This specification provides a blueprint for adding a custom Shortlink system to your existing WordPress plugin. It mimics the core functionality of URL Shortify—specifically the ability to create manual redirects for external sites (like an Amazon review page)—while remaining lightweight.
1. Architectural Overview
The system will consist of two parts:
    1. A Custom Post Type (CPT): To store and manage individual shortlinks.
    2. A Redirect Engine: A high-priority hook that intercepts requests, checks for a matching shortlink slug, and redirects the user.

2. Technical Specifications
A. Data Storage (Custom Post Type)
Register a hidden CPT called my_shortlink. This allows you to use WordPress's native database optimization and UI patterns.
    • Slug: The short name (e.g., /review).
    • Target URL: Stored in post_meta (e.g., https://amazon.com/review/ref=...).
    • Redirect Type: 301 (Permanent) or 302 (Temporary).
B. The Redirect Logic
You must hook into template_redirect or init to catch the request before WordPress tries to load a 404 page.
PHP
add_action('template_redirect', 'my_custom_shortlink_redirect');
function my_custom_shortlink_redirect() {
    // 1. Get the current request path (e.g., "review")
    $slug = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');

    // 2. Query the 'my_shortlink' CPT for a matching slug
    $query = new WP_Query([
        'post_type' => 'my_shortlink',
        'name'      => $slug,
        'posts_per_page' => 1
    ]);

    if ($query->have_posts()) {
        $link_id = $query->posts[0]->ID;
        $target_url = get_post_meta($link_id, '_target_url', true);
        
        // 3. Perform the redirect
        wp_redirect($target_url, 301);
        exit;
    }
}

3. Feature Set (Functional Spec)
Feature	Requirement	Use Case
Manual Slugs	Ability to set a custom slug like amazon-review.	Printing on paperback book covers.
Auto-Generator	If no slug is provided, generate a 5-character random string.	Internal page sharing.
Tracking	Increment a _click_count meta field on every redirect.	Measuring marketing success from the book.
External Redirect	Support for any URL outside the site's domain.	Sending readers to Amazon, social media, etc.

4. Admin UI Specification
To keep it "simpler" than Shortify, add a meta box to your plugin’s main settings or a dedicated "Shortlinks" submenu:
    1. Input Field 1: "Target URL" (The long Amazon link).
    2. Input Field 2: "Shortlink Slug" (The word to use after your domain).
    3. Display Table: A list showing Shortlink, Destination, and Total Clicks.
5. Why this works for your "Paperback" use case
Since paperback readers have to type the URL manually, you need a Static Redirect.
    • Shortify approach: You would go to "Add New Link," enter your long Amazon URL, and type review as the slug.
    • Your Plugin approach: You will provide a simple text field in your plugin where the user enters review + the Amazon URL. Your plugin then maps yourdomain.com/review → amazon.com/....
6. Implementation Tip: Prefixing
To avoid conflicts with your actual pages, it is often safer to prefix your shortlinks (e.g., yourdomain.com/go/review). However, if you want the "Shortify" feel of using the root (e.g., yourdomain.com/review), ensure your code checks the database for existing Pages/Posts first to avoid breaking your site's navigation.
