← Volver

Crear plugin en WordPress

Publicado el 27 de abril de 2025

Para crear un plugin en WordPress debemos crear una carpeta con el nombre de nuestro plugin. Y dentro de ella un fichero php con el mismo nombre.

El contenido del fichero php debe ser el siguiente

<?php
/*
Plugin Name: Plugin ejemplo
Description: Este plugin es un ejemplo
Version: 1.0.0
Author: ccorreas
Text Domain: plugin_ejemplo
*/

register_activation_hook(__FILE__, function () {
    global $wpdb;
    $sql = "create table if not exists {$wpdb->prefix}plugin_ejemplo(
        id int not null auto_increment,
        nombre varchar(50),
        correo varchar(50),
        primary key(id)
        );
    ";
    $wpdb->query($sql);
});

register_deactivation_hook(__FILE__, function () {
    flush_rewrite_rules();
});

add_action("admin_menu", function () {
    add_menu_page(
        "Plugin ejemplo",
        "Plugin ejemplo",
        "manage_options",
        plugin_dir_path(__FILE__) . "includes/admin.php",
        null,
        "dashicons-menu-alt",
        135
    );
});

add_action("admin_enqueue_scripts", function ($hook) {
    if ($hook == "plugin_ejemplo/includes/admin.php") {
        wp_enqueue_style("stylescss", plugin_dir_url(__FILE__) . "assets/css/styles.css");
        wp_enqueue_script("scriptsjs", plugin_dir_url(__FILE__) . "assets/js/script.js");
    }
});

El código anterior crear una tabla en la base de datos al activar el plugin.

La página del plugin es admin.php

<?php
global  $wpdb;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['eliminar_id'])) {
    $id = intval($_POST['eliminar_id']);
    $table_name = "{$wpdb->prefix}plugin_ejemplo";

    if (current_user_can('manage_options')) {
        $wpdb->delete($table_name, ['id' => $id], ['%d']);
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['crear'])) {
    $nombre = sanitize_text_field($_POST['nombre']);
    $correo = sanitize_text_field($_POST['correo']);
    $table_name = "{$wpdb->prefix}plugin_ejemplo";

    if (!empty($nombre) && is_email($correo)) {
        $wpdb->insert(
            $table_name,
            ['nombre' => $nombre, 'correo' => $correo],
            ['%s', '%s']
        );
    }
}


$table_name = "{$wpdb->prefix}plugin_ejemplo";
$results = $wpdb->get_results("SELECT * from $table_name");
?>
<h1 class="title">Plugin ejemplo</h1>

<main class="container">

    <form action="" method="POST">
        <input id="nombre" name="nombre" type="text" placeholder="Nombre">
        <input id="correo" name="correo" type="text" placeholder="Correo">
        <input type="hidden" name="crear">
        <button class="link__btn" type="submit">Crear</button>
    </form>

    <table id="tabla" class="main__table">
        <thead>
            <th>ID</th>
            <th>Nombre</th>
            <th>Correo</th>
            <th>Acciones</th>
        </thead>
        <tbody>
            <?php foreach ($results as $result) { ?>

                <tr>
                    <td><?php echo $result->id ?></td>
                    <td><?php echo $result->nombre ?></td>
                    <td><?php echo $result->correo ?></td>
                    <td class="table__td__delete">
                        <form method="post" onsubmit="return confirm('¿Estás seguro de que deseas eliminar este registro?');" style="display:inline;">
                            <input type="hidden" name="eliminar_id" value="<?php echo $result->id; ?>">
                            <button type="submit" class="link__btn link__btn-delete">Eliminar
                                <svg class="svg__delete" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                    <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
                                    <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
                                    <g id="SVGRepo_iconCarrier">
                                        <path d="M10 11V17" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M14 11V17" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M4 7H20" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M6 7H12H18V18C18 19.6569 16.6569 21 15 21H9C7.34315 21 6 19.6569 6 18V7Z" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M9 5C9 3.89543 9.89543 3 11 3H13C14.1046 3 15 3.89543 15 5V7H9V5Z" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                    </g>
                                </svg>
                            </button>
                        </form>

                    </td>
                </tr>

            <?php } ?>
        </tbody>
    </table>
</main>