0

I am trying to send an email to the user after form submission but I get an error
Call to undefined function wp_mail() in C:\xampp\htdocs\wordpress\wp-content\plugins\contact form\contact-form-plugin.php on line 46

I googled it and found that it has something to do with add_action( 'plugins_loaded', 'functionShowForm' );.I added this line to my code but it shows another form above main form as shown here link
what I am doing wrong here ?
My plugin file's code is

    <?php
    /*
    Plugin Name: Contact Form 
    Plugin URI: http://wpgeeks.net/
    Version: 1.0
    Author: 
    Description: 
    */

    /*Security Note: Consider blocking direct access to your plugin PHP files by adding the following line at the top of each of them, or be sure to refrain from executing sensitive standalone PHP code before calling any WordPress functions.*/
    defined( 'ABSPATH' ) or die( 'No script kiddies please!' );



    add_action( 'plugins_loaded', 'functionShowForm' );
    function functionShowForm($atts){

            $values = shortcode_atts(array(

        'color'=>'white'
    ),esc_html($atts));//updated here
    ?>

<form style="color:<?php echo $values['color'];?>;" action ="#" method="post">
Name:   <input type="text" name="name" placeholder="First Name">
Email:  <input type="email" name="email" placeholder="someone@gmail.com">
Password:<input type="password" name="password">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}//function ends here

function process_wpse_194468(){
if ($_SERVER['REQUEST_METHOD']=="POST" and isset($_POST["submit"])){
    $to = sanitize_email($_POST["email"]);//updated here
    $subject = esc_html($_POST["name"]);//updated here
    $message = $_POST["password"];
    echo "Email " .$to;
    wp_mail( $to, $subject, $message );
}
}
//short code
add_shortcode('showform','functionShowForm');
add_action('init','process_wpse_194468');

?>
  • 1) Are you sure this spelling is correct-- contact form\contact-forn-plugin.php? 2) That is a weird looking plugin. Why are you wrapping functions in HTML like that? – s_ha_dum Jul 14 '15 at 14:03
  • sorry about that , spelling mistake , updated.Is this the wrong approach of mixing html and php ? please guide me :) – Chaudhry Waqas Jul 14 '15 at 14:09
  • just updated the code please take a look now :) – Chaudhry Waqas Jul 14 '15 at 14:13
  • form shows in the admin area too link – Chaudhry Waqas Jul 14 '15 at 14:14
  • Mixing HTML and PHP is fine-- PHP is built for that. What is weird is the way you had HTML exposed in the main plugin file for no apparent reason. – s_ha_dum Jul 14 '15 at 14:14
0

plugins_loaded is going to load this form much too early, even if you weren't getting errors. The form is loading before the opening <html tags. You need to select better hooks for this-- admin_notices maybe, but it is hard to tell exactly what you want.

Second, you need to separate your handler:

function process_wpse_194468() {
  if ($_SERVER['REQUEST_METHOD']=="POST" and isset($_POST["submit"])){
    $to = $_POST["email"];
    $subject = "Apple Computer";
    $message = $_POST["password"];
    echo "Email " .$to;
    wp_mail( $to, $subject, $message );
  }
}
add_action('init','process_wpse_194468');

Third, validate that input!

| improve this answer | |
  • My main problem is Call to undefined function wp_mail() in C:\xampp\htdocs\wordpress\wp-content\plugins\contact form\contact-form-plugin.php on line 46 – Chaudhry Waqas Jul 14 '15 at 14:50
  • And I don't get that error with my changes. That may be the only problem you are concerned with, but the code is flawed in other ways too. – s_ha_dum Jul 14 '15 at 14:54
  • yes , now this error is fixed after I updated the code according to yours. Would you like to explain what 'add_action('init','process_wpse_194468');' line did ? – Chaudhry Waqas Jul 14 '15 at 14:55
  • I moved the form processing to a later hook so that the appropriate files are loaded before you try to use wp_mail – s_ha_dum Jul 14 '15 at 14:57
  • process_wpse_194468 is this built in wp function ? – Chaudhry Waqas Jul 14 '15 at 15:12

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.