Quantcast
Channel: Productivity Archives | My Make Money Online
Viewing all articles
Browse latest Browse all 5

Affiliate Links Transformation with WP-Affiliate-Link-Beautifier

$
0
0

Hello fellow affiliate marketers, it’s Donald here. I’ve been exactly where you are, struggling with those long and unsightly affiliate links provided by merchants. Honestly, if I were on the receiving end of a link that looked like a messy jumble of characters, I’d hesitate to click too. It’s natural to feel unsure when faced with such links, fearing the unknown.

As marketers, we’ve tried workarounds like Bitly or other URL shortening methods to make links appear more “normal” and trackable. However, these solutions often still leave room for doubt. But, imagine stumbling upon a link that follows the pattern: https://YourDomainName.com/ProductName. As a fan of the website, recognizing the domain name instantly puts you at ease and encourages that click. Isn’t that remarkable?

That’s why I took it upon myself to create an easy-to-use WordPress plugin, WP-Affiliate-Link-Beautifier, which I’m sharing with you all for free today. I’m also sharing the plugin code. So, for those of you who are tech-savvy or familiar with AI, you can modify this plugin as you see fit. And for those who might not be as comfortable with programming or AI just yet, don’t worry, I’ve got you covered with a download link below. (Please note that this plugin is only for the WordPress platform.)

*This article is dedicated to those of you working hard to promote affiliate products online. If you’re new to affiliate marketing and wondering what it’s all about, consider reading “What is Affiliate Marketing and How Does it Work?” and check our Affiliate Tips.

Demonstration

Let’s use the article “Website Speed Optimization : NitroPack – Simplified with One Click” as an example. (It’s a product I genuinely recommend for its one-click solution to boosting website speed, thereby aiding SEO rankings. While it’s free, purchasing a paid version through my link supports me with a small commission. Thank you for your support! )

Before Change

1. The affiliate link looks like this before change.

Install the Plugin

2. Go to your WordPress dashboard, install the WP-Affiliate-Link-Beautifier v2.1.zip (download the zip file), then follow the steps:

  • Navigate to the left menu
  • Select “Plugins” > “Add New Plugin” > “Upload”

Upload the Plugin

3. Drag and drop the WP-Affiliate-Link-Beautifier.zip file.

Access the Plugin Panel

4. After installation, the “Affiliate Links” admin panel will be available in the left menu.

5. Fill in the details and click “Add”

  • Pretty Link: The display name you prefer
  • Target Affiliate Link: The original or shortened affiliate link
  • Remarks: Any notes about the link (optional)

6. Once added, the table will display your new active link, and you can track the “Clicks” on your Pretty Link.

7. Replace the original affiliate links in your content or articles with the pretty links you’ve just crafted. There you go, all done! (Remember to run a quick test to ensure everything is looking good.)

Here’s a very straightforward code for the WP-Affiliate-Link-Beautifier plugin that I’ve made. (I personally prefer to keep it as simple as possible.) I’m sharing it with everyone here, so if you need to, please modify it as needed for personalization. Or, if you have any suggestions for improvements, let me know in the comments.

<?php
/*
Plugin Name: WP-Affiliate-Link-Beautifier
Description: A plugin to beautify affiliate links.
Version: 2.1
Author: MyMakeMoneyOnline
Author URI: https://mymakemoneyonline.com/
*/

// Create custom table on plugin activation
function als_create_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'als_links';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        pretty_link varchar(50) NOT NULL,
        target_link varchar(255) NOT NULL,
        clicks int(11) DEFAULT 0 NOT NULL,
        created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        remarks text DEFAULT '' NOT NULL,
        PRIMARY KEY  (id),
        UNIQUE KEY pretty_link (pretty_link),
        UNIQUE KEY target_link (target_link)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}
register_activation_hook(__FILE__, 'als_create_table');

// Add admin menu
function als_admin_menu() {
    add_menu_page(
        'Affiliate Link Beautifier', 
        'Affiliate Links', 
        'manage_options', 
        'affiliate-link-beautifier', 
        'als_admin_page', 
        'dashicons-admin-links', 
        6
    );
}
add_action('admin_menu', 'als_admin_menu');

// Export links
function als_export_links() {
    if (isset($_POST['als_export'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'als_links';
        $links = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);

        header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename=affiliate_links.csv');
        $output = fopen('php://output', 'w');
        fputcsv($output, array('ID', 'Pretty Link', 'Target Link', 'Clicks', 'Created At', 'Remarks'));
        
        foreach ($links as $link) {
            fputcsv($output, $link);
        }
        fclose($output);
        exit;
    }
}
add_action('admin_init', 'als_export_links');

// Import links
function als_import_links() {
    if (isset($_POST['als_import'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'als_links';
        if (!empty($_FILES['als_import_file']['tmp_name'])) {
            $csv_file = fopen($_FILES['als_import_file']['tmp_name'], 'r');
            fgetcsv($csv_file); // Skip header row
            while (($data = fgetcsv($csv_file)) !== FALSE) {
                $wpdb->insert($table_name, array(
                    'pretty_link' => sanitize_text_field($data[1]),
                    'target_link' => esc_url_raw($data[2]),
                    'clicks' => intval($data[3]),
                    'created_at' => sanitize_text_field($data[4]),
                    'remarks' => sanitize_textarea_field($data[5])
                ));
            }
            fclose($csv_file);
        }
    }
}
add_action('admin_init', 'als_import_links');

// Admin page
function als_admin_page() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'als_links';

    // Handle form submissions
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['pretty_link']) && !empty($_POST['target_link'])) {
        $pretty_link = sanitize_text_field($_POST['pretty_link']);
        $target_link = esc_url_raw($_POST['target_link']);
        $remarks = sanitize_textarea_field($_POST['remarks']);

        // Check for duplicates
        $existing_pretty_link = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE pretty_link = %s", $pretty_link));
        $existing_target_link = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE target_link = %s", $target_link));

        if ($existing_pretty_link == 0 && $existing_target_link == 0) {
            $wpdb->insert($table_name, [
                'pretty_link' => $pretty_link,
                'target_link' => $target_link,
                'remarks' => $remarks
            ]);
        } else {
            echo '<div class="error"><p>Pretty Link or Target Link already exists.</p></div>';
        }
    }

    // Handle deletions
    if (isset($_GET['delete'])) {
        $id = intval($_GET['delete']);
        $wpdb->delete($table_name, ['id' => $id]);
    }

    // Handle reset clicks
    if (isset($_GET['reset'])) {
        $id = intval($_GET['reset']);
        $wpdb->update($table_name, ['clicks' => 0], ['id' => $id]);
    }

    // Handle edit remarks
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit_id'])) {
        $id = intval($_POST['edit_id']);
        $pretty_link = sanitize_text_field($_POST['edit_pretty_link']);
        $target_link = esc_url_raw($_POST['edit_target_link']);
        $remarks = sanitize_textarea_field($_POST['edit_remarks']);

        // Check for duplicates
        $existing_pretty_link = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE pretty_link = %s AND id != %d", $pretty_link, $id));
        $existing_target_link = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE target_link = %s AND id != %d", $target_link, $id));

        if ($existing_pretty_link == 0 && $existing_target_link == 0) {
            $wpdb->update($table_name, [
                'pretty_link' => $pretty_link,
                'target_link' => $target_link,
                'remarks' => $remarks
            ], ['id' => $id]);
        } else {
            echo '<div class="error"><p>Pretty Link or Target Link already exists.</p></div>';
        }
    }

    $links = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at ASC");

    ?>
    <div class="wrap">
        <p>Do you like this plugin? Feel free to continue following our website at <a href="https://mymakemoneyonline.com/" target="_blank">https://mymakemoneyonline.com/</a></p>
        <form method="post" enctype="multipart/form-data" action="">
            <input type="file" name="als_import_file" />
            <input type="submit" name="als_import" class="button" value="Import" />
            <input type="submit" name="als_export" class="button" value="Export" />
        </form>
        <h1>Affiliate Link Beautifier</h1>
        <form method="post" action="">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Pretty Link:</th>
                    <td><input type="text" name="pretty_link" placeholder="Example: mylink (will be used as https://yourdomain.com/mylink)" class="large-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Target Affiliate Link:</th>
                    <td><input type="url" name="target_link" placeholder="https://example.com/1234" class="large-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Remarks:</th>
                    <td><textarea name="remarks" placeholder="Enter any remarks here..." class="large-text"></textarea></td>
                </tr>
                <tr valign="top">
                    <td colspan="2" align="right">
                        <input type="submit" class="button-primary" value="Add" />
                    </td>
                </tr>
            </table>
        </form>

        <table class="wp-list-table widefat fixed striped" style="width: 100%; max-width: 100%;">
            <thead>
                <tr>
                    <th style="width: 15%;">Created Date</th>
                    <th style="width: 20%;">Pretty Link</th>
                    <th style="width: 20%;">Target Link</th>
                    <th style="width: 5%;">Clicks</th>
                    <th style="width: 20%;">Remarks</th>
                    <th style="width: 20%;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($links as $link): ?>
                    <tr>
                        <td><?php echo esc_html($link->created_at); ?></td>
                        <td>
                            <a href="<?php echo home_url('/' . esc_html($link->pretty_link)); ?>" target="_blank"><?php echo home_url('/' . esc_html($link->pretty_link)); ?></a>
                            <button class="button copy-button" data-url="<?php echo home_url('/' . esc_html($link->pretty_link)); ?>">Copy</button>
                        </td>
                        <td><a href="<?php echo esc_url($link->target_link); ?>" target="_blank"><?php echo esc_html($link->target_link); ?></a></td>
                        <td><?php echo intval($link->clicks); ?></td>
                        <td><?php echo esc_html($link->remarks); ?></td>
                        <td>
                            <a href="#" class="button button-secondary edit-link" data-id="<?php echo $link->id; ?>" data-pretty="<?php echo esc_html($link->pretty_link); ?>" data-target="<?php echo esc_html($link->target_link); ?>" data-remarks="<?php echo esc_html($link->remarks); ?>">Edit</a>
                            <a href="<?php echo admin_url('admin.php?page=affiliate-link-beautifier&reset=' . $link->id); ?>" class="button button-secondary reset-clicks">Reset</a>
                            <a href="<?php echo admin_url('admin.php?page=affiliate-link-beautifier&delete=' . $link->id); ?>" class="button button-secondary delete-link">Delete</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div id="edit-link-modal" style="display:none;">
        <form method="post" action="">
            <input type="hidden" name="edit_id" id="edit_id" />
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Pretty Link:</th>
                    <td><input type="text" name="edit_pretty_link" id="edit_pretty_link" class="large-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Target Affiliate Link:</th>
                    <td><input type="url" name="edit_target_link" id="edit_target_link" class="large-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Remarks:</th>
                    <td><textarea name="edit_remarks" id="edit_remarks" class="large-text"></textarea></td>
                </tr>
                <tr valign="top">
                    <td colspan="2" align="right">
                        <input type="submit" class="button-primary" value="Save" />
                        <button type="button" class="button" id="cancel-edit">Cancel</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <?php
}

// Handle redirect and click counting
function als_redirect() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'als_links';

    $request = trim($_SERVER['REQUEST_URI'], '/');
    if (strpos($request, '/') === false) {
        $link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE pretty_link = %s", $request));
        if ($link) {
            $wpdb->update($table_name, ['clicks' => $link->clicks + 1], ['id' => $link->id]);
            wp_redirect($link->target_link);
            exit;
        }
    }
}
add_action('template_redirect', 'als_redirect');

// Enqueue custom styles and scripts for admin
function als_admin_styles() {
    echo '<style>
        .form-table input.large-text, .form-table textarea.large-text {
            width: 100% !important;
        }
        .form-table input.large-text::placeholder, .form-table textarea.large-text::placeholder {
            color: #ccc;
        }
        .wrap h1 {
            font-size: 2em;
            margin-bottom: 1em;
        }
        .wp-list-table th, .wp-list-table td {
            padding: 8px 10px;
            vertical-align: middle;
        }
        .wp-list-table th {
            background-color: #f1f1f1;
            font-weight: bold;
        }
        .wp-list-table tbody tr:nth-child(odd) {
            background-color: #f9f9f9;
        }
        .wp-list-table tbody tr:hover {
            background-color: #f1f1f1;
        }
        .button-primary {
            background: #0073aa;
            border-color: #0073aa;
            box-shadow: 0 1px 0 #0073aa;
            text-decoration: none;
            text-shadow: none;
        }
        .button {
            margin-top: 10px;
        }
        .copy-button {
            margin-left: 10px;
        }
        #edit-link-modal {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border: 1px solid #ccc;
            box-shadow: 0 0 10px rgba(0,0,0,0.5);
            z-index: 1000;
            width: 80%;
            max-width: 600px;
        }
        #edit-link-modal form {
            display: flex;
            flex-direction: column;
        }
        #edit-link-modal textarea {
            margin-bottom: 10px;
        }
    </style>';
}
add_action('admin_head', 'als_admin_styles');

function als_admin_scripts() {
    echo '<script>
        document.addEventListener("DOMContentLoaded", function() {
            const resetButtons = document.querySelectorAll(".reset-clicks");
            const deleteButtons = document.querySelectorAll(".delete-link");
            const editButtons = document.querySelectorAll(".edit-link");
            const copyButtons = document.querySelectorAll(".copy-button");
            const modal = document.getElementById("edit-link-modal");
            const cancelEdit = document.getElementById("cancel-edit");

            resetButtons.forEach(button => {
                button.addEventListener("click", function(event) {
                    if (!confirm("Are you sure you want to reset the clicks?")) {
                        event.preventDefault();
                    }
                });
            });

            deleteButtons.forEach(button => {
                button.addEventListener("click", function(event) {
                    if (!confirm("Are you sure you want to delete this link?")) {
                        event.preventDefault();
                    }
                });
            });

            editButtons.forEach(button => {
                button.addEventListener("click", function(event) {
                    event.preventDefault();
                    const id = button.getAttribute("data-id");
                    const prettyLink = button.getAttribute("data-pretty");
                    const targetLink = button.getAttribute("data-target");
                    const remarks = button.getAttribute("data-remarks");
                    document.getElementById("edit_id").value = id;
                    document.getElementById("edit_pretty_link").value = prettyLink;
                    document.getElementById("edit_target_link").value = targetLink;
                    document.getElementById("edit_remarks").value = remarks;
                    modal.style.display = "block";
                });
            });

            copyButtons.forEach(button => {
                button.addEventListener("click", function(event) {
                    event.preventDefault();
                    const url = button.getAttribute("data-url");
                    navigator.clipboard.writeText(url).then(() => {
                        alert("Pretty Link URL copied to clipboard");
                    });
                });
            });

            cancelEdit.addEventListener("click", function() {
                modal.style.display = "none";
            });
        });
    </script>';
}
add_action('admin_footer', 'als_admin_scripts');

WP-Affiliate-Link-Beautifier.php

Conclusion

With WP-Affiliate-Link-Beautifier, you have a powerful tool at your disposal to make your affiliate links more attractive and trustworthy. By integrating this plugin into your WordPress site, you can improve the user experience, increase click-through rates, and ultimately boost your affiliate marketing success.

In closing, I must remind everyone successful affiliate marketing is about more than just the links you use. It’s about providing valuable content, being transparent with your audience, and continuously optimizing your strategy. Keep these principles in mind, and you’ll be well on your way to achieving your marketing goals.

Thank you for reading, and I hope this plugin helps you in your affiliate marketing journey. If this plugin has been helpful to you, please like and share and leave a comment to tell me, it gives me more motivation to provide more exciting and useful content for everyone (or buy me a coffee), thank you!


Continue Reading:

The post Affiliate Links Transformation with WP-Affiliate-Link-Beautifier appeared first on My Make Money Online.


Viewing all articles
Browse latest Browse all 5

Trending Articles