Wie erstelle ich ein WordPress Plugin ?

6 April 2023

Hülle anlegen

unter plugins ein neues Verzeichnis anlegen und darin ein gleich lautendes php Script

Beispielcode

<?php
/*
Plugin Name: Mein Plugin
Description: Beschreibung des Plugins
Version: 1.0
Author: Dein Name
*/

// Füge das Plugin dem WordPress-Menü hinzu
add_action('admin_menu', 'mein_plugin_add_admin_page');

function mein_plugin_add_admin_page() {
    add_menu_page(
        'Mein Plugin',
        'Mein Plugin',
        'manage_options',
        'mein-plugin',
        'mein_plugin_admin_page',
        'dashicons-admin-plugins',
        110
    );
}

// Erstelle die Einstellungen für das Plugin
add_action('admin_init', 'mein_plugin_settings');

function mein_plugin_settings() {
    register_setting('mein_plugin_options', 'mein_plugin_email');
}

// Definiere die Funktion, die die Plugin-Seite im Dashboard anzeigt
function mein_plugin_admin_page() {
    ?>
    <div class="wrap">
        <h1>Mein Plugin</h1>
        <form method="post" action="options.php">
            <?php settings_fields('mein_plugin_options'); ?>
            <label for="mein_plugin_email">E-Mail-Adresse:</label>
            <input type="text" name="mein_plugin_email" value="<?php echo esc_attr(get_option('mein_plugin_email')); ?>" />
            <?php submit_button(); ?>
        </form>
        <hr>
        <h2><?php _e('Gespeicherte Eingaben', 'mein-plugin'); ?></h2>
        <?php mein_plugin_display_data(); ?>
    </div>
    <?php
}


// Funktion zum Anzeigen der gespeicherten Eingaben in den Plugin-Einstellungen
function mein_plugin_display_data() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'mein_plugin_data';
    $rows = $wpdb->get_results("SELECT * FROM $table_name ORDER BY time DESC");
    ?>
    <table class="widefat">
        <thead>
            <tr>
                <th><?php _e('ID', 'mein-plugin'); ?></th>
                <th><?php _e('Eingabe', 'mein-plugin'); ?></th>
                <th><?php _e('Zeit', 'mein-plugin'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row) : ?>
                <tr>
                    <td><?php echo $row->id; ?></td>
                    <td><?php echo $row->input_value; ?></td>
                    <td><?php echo $row->time; ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php
}



// Definiere die Funktion, die das Formular auf der WordPress-Seite ausgibt
function mein_plugin_shortcode() {
    ob_start();
    ?>
    <form method="post">
        <label for="mein_plugin_input">Überschrift:</label>
        <input type="text" name="mein_plugin_input" />
        <input type="submit" name="mein_plugin_submit" value="Senden" />
    </form>
    <?php
    return ob_get_clean();
}

add_shortcode('mein-plugin-form', 'mein_plugin_shortcode');

// Definiere die Funktion, die das Formular verarbeitet
add_action('init', 'mein_plugin_process_form');

function mein_plugin_process_form() {
    if (isset($_POST['mein_plugin_submit'])) {
        $input_value = sanitize_text_field($_POST['mein_plugin_input']);
        $email = get_option('mein_plugin_email');

        // Speichere den Wert in der Datenbank
        global $wpdb;
        $table_name = $wpdb->prefix . 'mein_plugin_data';
        $wpdb->insert(
            $table_name,
            array(
                'input_value' => $input_value,
                'time' => current_time('mysql')
            )
        );

        // Versende eine E-Mail mit dem Wert
        $subject = 'Neue Eingabe von Mein Plugin';
        $body = 'Neue Eingabe: ' . $input_value;
        $headers = array('Content-Type: text/html; charset=UTF-8');
        #print "Daten sind da und werden gesendet an $email";
        wp_mail($email, $subject, $body, $headers);
    }
}
// Erstelle die Tabelle in der Datenbank, wenn das Plugin aktiviert wird
function mein_plugin_create_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'mein_plugin_data';

    // Prüfe, ob die Tabelle bereits existiert
    if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
        // Wenn nicht, erstelle sie
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE $table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            input_value text NOT NULL,
            time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
}

// Deaktiviere das Plugin und lösche die Tabelle aus der Datenbank
register_deactivation_hook(__FILE__, 'mein_plugin_delete_table');

function mein_plugin_delete_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'mein_plugin_data';
    $sql = "DROP TABLE IF EXISTS $table_name;";
    $wpdb->query($sql);
}

und auf einer Testseite dann

[mein-plugin-form]

einfügen