submit form

Home » Snippets » submit form
0

Created with:

Visibility: 

public

Creator: ali

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: My WP Plugin
 * Description: This plugin displays the latest posts from the 'recipes' post type and provides a submit form.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * Text Domain: wpturbo
 */

// Enqueue plugin stylesheets and scripts
function wpturbo_enqueue_scripts() {
    // Enqueue CSS stylesheet
    wp_enqueue_style( 'wpturbo-styles', plugins_url( 'css/styles.css', __FILE__ ) );

    // Enqueue JS script
    wp_enqueue_script( 'wpturbo-script', plugins_url( 'js/script.js', __FILE__ ), array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpturbo_enqueue_scripts' );

// Display latest posts from 'recipes' post type
function wpturbo_display_latest_recipes() {
    $args = array(
        'post_type'      => 'recipes',
        'posts_per_page' => 5,
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '<ul>';

        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }

        echo '</ul>';

        // Restore original post data
        wp_reset_postdata();
    } else {
        echo 'No recipes found.';
    }
}
add_shortcode( 'latest_recipes', 'wpturbo_display_latest_recipes' );

// Display submit form
function wpturbo_display_submit_form() {
    if ( isset( $_POST['submit_form'] ) ) {
        $name         = sanitize_text_field( $_POST['name'] );
        $email        = sanitize_email( $_POST['email'] );
        $company_name = sanitize_text_field( $_POST['company_name'] );

        // Send email using WP Mail SMTP plugin
        $to      = 'admin@example.com'; // Change this to your email address
        $subject = 'New form submission';
        $message = "Name: $namenEmail: $emailnCompany Name: $company_name";
        $headers = array( 'Content-Type: text/html; charset=UTF-8' );

        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<p class="success">Form submitted successfully!</p>';
        } else {
            echo '<p class="error">Failed to submit the form. Please try again later.</p>';
        }
    }

    echo '
        <form method="post" class="submit-form">
            <label for="name">Name:</label>
            <input type="text" name="name" id="name" required>
            
            <label for="email">Email:</label>
            <input type="email" name="email" id="email" required>
            
            <label for="company_name">Company Name:</label>
            <input type="text" name="company_name" id="company_name" required>
            
            <button type="submit" name="submit_form">Submit</button>
        </form>
    ';
}
add_shortcode( 'submit_form', 'wpturbo_display_submit_form' );
				

Explanation:

Remember to replace 'admin@example.com' with your own email address in the wpturbo_display_submit_form function.

Make sure you have the necessary CSS and JS files in the specified directories, and update the file paths accordingly in the wp_enqueue_style and wp_enqueue_script functions.

You can use the shortcode [submit_form] in your posts or pages to display the submit form.

Register an account to save your snippets or go Pro to get more features.