-1

I made my first WP plugin, it is a contact form - here is the code:

<?php
/*
Plugin Name: Example Contact Form Plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/

function html_form_code() {
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<p>';
    echo 'Your Name (required) <br />';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Your Email (required) <br />';
    echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Subject (required) <br />';
    echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Your Message (required) <br />';
    echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
    echo '</p>';
    echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
    echo '</form>';
}

function deliver_mail() {

    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $subject = sanitize_text_field( $_POST["cf-subject"] );
        $message = esc_textarea( $_POST["cf-message"] );

        // get the blog administrator's email address
        $to = get_option( 'admin_email' );

        $headers = "From: $name <$email>" . "\r\n";

        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for contacting me, expect a response soon.</p>';
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
        }
    }
}

function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();

    return ob_get_clean();
}

add_shortcode( 'contact_form', 'cf_shortcode' );
?>

This is the tutorial I used to make it.

When I put [contact_form] in a post or on a page, nothing appears...no contact form.

Why won't the form appear?

  • have you tried turning off your router err i mean plugin and turning it on again? – majick Mar 23 '16 at 8:07
  • yes...i found a workaround..I think maybe something is wrong with my wordpress install because someone on stack tried my code and it worked on their install. I'll post my workaround as an answer – ewizard Mar 23 '16 at 14:23
  • yes couldn't see anything wrong myself, so wondered if the plugin was activated, hence the humourous comment. :-) – majick Mar 23 '16 at 14:33
  • haha ahhh so that is the joke...nice – ewizard Mar 23 '16 at 21:15
0

Instead of using the shortcode in the editor, there is a function that executes the shortcode which you can use directly in php of the template for any page.

<?php echo do_shortcode('[contact_form]'); ?>

0

Instead of using output buffering, you can just set a string and return it instead of using echo, eg.

    $result = '';
    if ( wp_mail( $to, $subject, $message, $headers ) ) {
        $result .= '<div>';
        $result .= '<p>Thanks for contacting me, expect a response soon.</p>';
        $result .= '</div>';
    } else {
        $result .= 'An unexpected error occurred';
    }
    return $result;

...and do the same for html_form_code function... then just do:

function cf_shortcode() {
    $deliver = deliver_mail();
    $form = html_form_code();
    return $deliver.$form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.