Skip to content

Instantly share code, notes, and snippets.

@ara303
Last active September 6, 2024 02:12
Show Gist options
  • Save ara303/f6854100d3a8670e52cc8a38db570c93 to your computer and use it in GitHub Desktop.
Save ara303/f6854100d3a8670e52cc8a38db570c93 to your computer and use it in GitHub Desktop.
Probably broken email editor for WordPress

Here is a basic outline of how you can create a WordPress plugin to achieve this:

Plugin Name: Email Template Editor

Plugin Description: A GUI editor for editing email templates with WYSIWYG editors and text replacements.

Plugin Files:

  • email-template-editor.php (main plugin file)
  • email-template-editor-admin.php (admin interface file)
  • email-template-editor-functions.php (functions file)
  • email-template-editor.js (JavaScript file for WYSIWYG editor)
  • email-template-editor.css (CSS file for styling)

email-template-editor.php:

<?php
/*
Plugin Name: Email Template Editor
Description: A GUI editor for editing email templates with WYSIWYG editors and text replacements.
Version: 1.0
*/

// Include admin interface file
require_once 'email-template-editor-admin.php';

// Include functions file
require_once 'email-template-editor-functions.php';

// Hook to add admin menu
add_action('admin_menu', 'email_template_editor_admin_menu');

function email_template_editor_admin_menu() {
    add_menu_page('Email Template Editor', 'Email Template Editor', 'manage_options', 'email-template-editor', 'email_template_editor_admin_page');
}

// Hook to save email template changes
add_action('admin_init', 'email_template_editor_save_changes');

function email_template_editor_save_changes() {
    if (isset($_POST['email_template_editor_save'])) {
        // Save changes to email template file
        email_template_editor_save_template();
    }
}

// Function to save email template changes
function email_template_editor_save_template() {
    $template_name = $_POST['template_name'];
    $template_content = $_POST['template_content'];
    $template_file = get_template_directory() . '/emails/' . $template_name . '.php';
    file_put_contents($template_file, $template_content);
}

email-template-editor-admin.php:

<?php
// Admin interface file

// Function to display admin page
function email_template_editor_admin_page() {
    ?>
    <div class="wrap">
        <h1>Email Template Editor</h1>
        <form method="post">
            <?php
            // Get email templates
            $templates = email_template_editor_get_templates();
            foreach ($templates as $template) {
                ?>
                <div class="template-editor">
                    <h2><?php echo $template['name']; ?></h2>
                    <div class="wysiwyg-editor">
                        <?php
                        // Display WYSIWYG editor
                        wp_editor($template['content'], 'template_content_' . $template['name'], array(
                            'textarea_name' => 'template_content',
                            'textarea_rows' => 10,
                        ));
                        ?>
                    </div>
                    <div class="text-replacements">
                        <?php
                        // Display text replacements
                        email_template_editor_display_text_replacements($template['name']);
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
            <input type="hidden" name="email_template_editor_save" value="1">
            <button type="submit" class="button button-primary">Save Changes</button>
        </form>
    </div>
    <?php
}

// Function to get email templates
function email_template_editor_get_templates() {
    $templates = array();
    $template_dir = get_template_directory() . '/emails/';
    $files = scandir($template_dir);
    foreach ($files as $file) {
        if (strpos($file, '.php') !== false) {
            $template_name = substr($file, 0, -4);
            $template_content = file_get_contents($template_dir . $file);
            $templates[] = array(
                'name' => $template_name,
                'content' => $template_content,
            );
        }
    }
    return $templates;
}

// Function to display text replacements
function email_template_editor_display_text_replacements($template_name) {
    ?>
    <h3>Text Replacements</h3>
    <ul>
        <?php
        // Get text replacements
        $text_replacements = email_template_editor_get_text_replacements($template_name);
        foreach ($text_replacements as $replacement) {
            ?>
            <li>
                <input type="text" name="text_replacement_<?php echo $replacement['key']; ?>" value="<?php echo $replacement['value']; ?>">
                <span><?php echo $replacement['description']; ?></span>
            </li>
            <?php
        }
        ?>
    </ul>
    <?php
}

// Function to get text replacements
function email_template_editor_get_text_replacements($template_name) {
    $text_replacements = array();
    // Add text replacements here
    // Example:
    // $text_replacements[] = array(
    //     'key' => 'login_link',
    //     'value' => 'https://example.com/login',
    //     'description' => 'Login link',
    // );
    return $text_replacements;
}

email-template-functions.php

<?php
// SOME OF THIS IS ALMOST DEFINITELY DUPLICATED

// Function to save email template changes
function email_template_editor_save_template() {
    $template_name = $_POST['template_name'];
    $template_content = $_POST['template_content'];
    $template_file = get_template_directory() . '/emails/' . $template_name . '.php';
    file_put_contents($template_file, $template_content);
}

// Function to get email templates
function email_template_editor_get_templates() {
    $templates = array();
    $template_dir = get_template_directory() . '/emails/';
    $files = scandir($template_dir);
    foreach ($files as $file) {
        if (strpos($file, '.php') !== false) {
            $template_name = substr($file, 0, -4);
            $template_content = file_get_contents($template_dir . $file);
            $templates[] = array(
                'name' => $template_name,
                'content' => $template_content,
            );
        }
    }
    return $templates;
}

// Function to display text replacements
function email_template_editor_display_text_replacements($template_name) {
    ?>
    <h3>Text Replacements</h3>
    <ul>
        <?php
        // Get text replacements
        $text_replacements = email_template_editor_get_text_replacements($template_name);
        foreach ($text_replacements as $replacement) {
            ?>
            <li>
                <input type="text" name="text_replacement_<?php echo $replacement['key']; ?>" value="<?php echo $replacement['value']; ?>">
                <span><?php echo $replacement['description']; ?></span>
            </li>
            <?php
        }
        ?>
    </ul>
    <?php
}

// Function to get text replacements
function email_template_editor_get_text_replacements($template_name) {
    $text_replacements = array();
    // Add text replacements here
    // Example:
    // $text_replacements[] = array(
    //     'key' => 'login_link',
    //     'value' => 'https://example.com/login',
    //     'description' => 'Login link',
    // );
    return $text_replacements;
}

// Function to replace text in email templates
function email_template_editor_replace_text($template_content, $text_replacements) {
    foreach ($text_replacements as $replacement) {
        $template_content = str_replace('{{ ' . $replacement['key'] . ' }}', $replacement['value'], $template_content);
    }
    return $template_content;
}

email-template-editor.js:

// JavaScript file for WYSIWYG editor

// Initialize WYSIWYG editor
tinymce.init({
    selector: '.wysiwyg-editor',
    height: 300,
    menubar: false,
    plugins: [
        'advlist autolink lists link image charmap print preview anchor',
        'searchreplace visualblocks code fullscreen',
        'insertdatetime media table paste code help wordcount'
    ],
    toolbar: 'undo redo | formatselect | ' +
        'bold italic backcolor | alignleft aligncenter ' +
        'alignright alignjustify | bullist numlist outdent indent | ' +
        'removeformat | help',
    content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }'
});

email-template-editor.css:

/* CSS file for styling */

.template-editor {
    margin-bottom: 20px;
}

.wysiwyg-editor {
    width: 100%;
    height: 300px;
    border: 1px solid #ccc;
    padding: 10px;
}

.text-replacements {
    margin-top: 20px;
}

.text-replacements ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.text-replacements li {
    margin-bottom: 10px;
}

.text-replacements input[type="text"] {
    width: 100%;
    height: 30px;
    padding: 10px;
    border: 1px solid #ccc;
}

.text-replacements span {
    display: block;
    margin-top: 5px;
}

This plugin creates a GUI editor for editing email templates with WYSIWYG editors and text replacements. The plugin uses the TinyMCE editor for the WYSIWYG editor and allows you to add text replacements for each email template.

To use this plugin, simply install and activate it, then go to the Email Template Editor page in the WordPress admin dashboard. From there, you can edit each email template using the WYSIWYG editor and add text replacements as needed.

Note that this plugin is just a starting point, and you will likely need to customize it to fit your specific needs. Additionally, this plugin does not include any error checking or validation, so be sure to test it thoroughly before using it in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment