You are currently viewing 5 steps to create word press plugin?
WordPress Plugin

5 steps to create word press plugin?

To create a WordPress plugin that collects a user’s name, email, and mobile number, and displays the information in the admin panel with an option to download the data as a CSV file, follow these steps:

Create Plugin Step by Step

  1. Create a new folder in your /wp-content/plugins/ directory and name it subscription-form.
  2. Inside the subscription-form folder, create a new file named subscription-form.php. This file will contain the main plugin code.
  3. Open subscription-form.php and add the following code: ( you can get the php codes from chat gpt)
<?php
/**
 * Plugin Name: Subscription Form
 * Description: A simple subscription form that collects name, email, and mobile number.
 * Version: 1.0
 * Author: Oneto99.com
 */

// Create database table on plugin activation
function subscription_form_install() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'subscriptions';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name tinytext NOT NULL,
        email varchar(100) NOT NULL,
        mobile varchar(15) NOT NULL,
        created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";

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

register_activation_hook(__FILE__, 'subscription_form_install');

// Handle subscription form submission
function subscription_form_submit() {
    global $wpdb;
    $name = sanitize_text_field($_POST['name']);
    $email = sanitize_email($_POST['email']);
    $mobile = sanitize_text_field($_POST['mobile']);
    $table_name = $wpdb->prefix . 'subscriptions';

    $wpdb->insert($table_name, array(
        'name' => $name,
        'email' => $email,
        'mobile' => $mobile,
    ));

    wp_send_json_success(array('message' => 'Thank you for subscribing!'));
}

add_action('wp_ajax_subscription_form_submit', 'subscription_form_submit');
add_action('wp_ajax_nopriv_subscription_form_submit', 'subscription_form_submit');

// Display subscription form
function subscription_form_shortcode() {
    ob_start();
    ?>
    <form id="subscription-form">
        <p>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" required>
        </p>
        <p>
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" required>
        </p>
        <p>
            <label for="mobile">Mobile:</label>
            <input type="text" id="mobile" name="mobile" required>
        </p>
        <p>
            <input type="submit" value="Subscribe">
        </p>
        <p id="subscription-response"></p>
    </form>
    <script>
        document.getElementById("subscription-form").addEventListener("submit", function(event) {
            event.preventDefault();
            let formData = new FormData(event.target);
            formData.append('action', 'subscription_form_submit');
            fetch('<?php echo admin_url("admin-ajax.php"); ?>', {
                method: "POST",
                body: new URLSearchParams(formData),
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById("subscription-response").innerHTML = data.data.message;
            })
            .catch(error => {
                document.getElementById("subscription-response").innerHTML = 'An error occurred. Please try again later.';
            });
        });
    </script>
    <?php
    return ob_get_clean();
}

add_shortcode('subscription_form', 'subscription_form_shortcode');

add_action('admin_menu', 'subscription_form_menu');

function subscription_form_menu() {
    add_menu_page('Subscriptions', 'Subscriptions', 'manage_options', 'subscription-form-list', 'subscription_form_list', 'dashicons-list-view', 6);
}

// Display subscription list
function subscription_form_list() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'subscriptions';
    $results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY id DESC");

    // Export as CSV
    echo '<div class="wrap">';
    echo '<h1>Subscriptions</h1>';
    echo '<a href="javascript:void(0);" onclick="downloadCSV();" class="button button-primary">Export as CSV</a>';

    echo '<table class="wp-list-table widefat fixed striped">';
    echo '<thead>';
    echo '<tr>';
    echo '<th>ID</th>';
    echo '<th>Name</th>';
    echo '<th>Email</th>';
    echo '<th>Mobile</th>';
    echo '<th>Created At</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    foreach ($results as $row) {
        echo '<tr>';
        echo '<td>' . $row->id . '</td>';
        echo '<td>' . $row->name . '</td>';
        echo '<td>' . $row->email . '</td>';
        echo '<td>' . $row->mobile . '</td>';
        echo '<td>' . $row->created_at . '</td>';
        echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
    echo '</div>';

    // JavaScript code for exporting as CSV
    ?>
    <script>
        function downloadCSV() {
            let data = <?php echo json_encode($results); ?>;
            let csvContent = "data:text/csv;charset=utf-8,\uFEFF";
            let header = 'ID,Name,Email,Mobile,Created At\r\n';
            csvContent += header;

            data.forEach(function(row) {
                let rowArray = [row.id, row.name, row.email, row.mobile, row.created_at];
                let rowString = rowArray.join(",");
                csvContent += rowString + "\r\n";
            });

            let encodedUri = encodeURI(csvContent);
            let link = document.createElement("a");
            link.setAttribute("href", encodedUri);
            link.setAttribute("download", "subscriptions.csv");
            link.style.display = "none";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    </script>
    <?php
}

4. Now, activate the “Subscription Form” plugin in your WordPress admin panel.

5. To display the subscription form on a page or post, use the

shortcode.

Now the plugin should work as intended. When a user submits their information, it is stored in the database. In the WordPress admin panel, you can view the list of subscriptions under the “Subscriptions” menu item. You can also download the subscription data as a CSV file by clicking the “Export as CSV” button.

To use the subscription form shortcode in Elementor, follow these steps:

  1. Create or edit the page or post where you want to display the subscription form using Elementor.
  2. In the Elementor editor, search for the “Shortcode” widget in the widget panel on the left.
  3. Drag and drop the “Shortcode” widget to the desired location on the page.
  4. In the “Shortcode” widget settings on the left side, enter the subscription form shortcode:

  5. Click the “Update” button at the bottom of the Elementor editor to save your changes.

Now, the subscription form should be displayed on your page when you view it on the frontend.

If you want to style the form using Elementor, you can create a new form using the Elementor Form widget and customize it according to your needs. Then, you will need to update the AJAX submission code in the subscription-form.php file to handle the new form data structure.

pictory

More Blogs

WordPress Plugin Creation by your own

Leave a Reply