-1

When I try to send mail... it showing -

Fatal error: Call to undefined function get_option() in /home/junkie/public_html/sub_demo/wp-content/themes/scroller/includes/contact-form.php on line 9

My code=> contact-form.php

<?php
if($_POST)
{
    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
    } 

    $to_Email       = "tanvir.focus@gmail.com"; //Replace with recipient email address
    $subject        = 'Ah!! My email from Somebody out there...'; //Subject line for emails

    //check $_POST vars are set, exit if any missing
    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"]))
    {
        die();
    }

    //Sanitize input data using PHP filter_var().
    $user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_Phone       = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
    $user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

    //additional php validation
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
    {
        header('HTTP/1.1 500 Name is too short or empty!');
        exit();
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
    {
        header('HTTP/1.1 500 Please enter a valid email!');
        exit();
    }
    if(!is_numeric($user_Phone)) //check entered data is numbers
    {
        header('HTTP/1.1 500 Only numbers allowed in phone field');
        exit();
    }
    if(strlen($user_Message)<5) //check emtpy message
    {
        header('HTTP/1.1 500 Too short message! Please enter something.');
        exit();
    }

    //proceed with PHP email.
    $headers = 'From: '.$user_Email.'' . "rn" .
    'Reply-To: '.$user_Email.'' . "rn" .
    'X-Mailer: PHP/' . phpversion();

    @$sentMail = mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

    if(!$sentMail)
    {
        header('HTTP/1.1 500 Could not send mail! Sorry..');
        exit();
    }else{
        echo 'Hi '.$user_Name .', Thank you for your email! ';
        echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
    }
}
?>
  • Tanvir, please show us where you call that code. Hint: There is wp_mail() which should be used instead of the native PHP mail() function. – kaiser May 5 '14 at 14:23
  • Based on this-- //check if its an ajax request, exit if not-- and other clues in the question and the code, I am pretty sure this is a duplicate: wordpress.stackexchange.com/q/108143/21376 – s_ha_dum May 5 '14 at 14:40
  • downvoted as the code do not match the error – Mark Kaplun Sep 15 '17 at 7:54
1

I don't see any get_option() function on line 9 or anywhere else in your code.

After quick googling, my guess would be that your problem is the same as this question on stackoverflow

EDIT

As @Ray proposed at the linked question, paste this in your code to check if wp-includes/option.php is included:

$includedStuff = get_included_files();
print_r($includedStuff);

If it is not, include it yourself:

include_once('wp-includes\option.php');

If this works for you, don't forget to upvote Ray's answer.

  • 2
    Please post the relevant code from that question/answer that will solve the problem of the OP. Just remember, please give credit to the person who post the solution, which will be Ray I suppose. The reason for this, if this question gets removed from SO, your answer won't be just a dead link – Pieter Goosen May 5 '14 at 14:13
  • @PieterGoosen, thanks for the correction! I hope my edit resolves this. – Petr Cibulka May 5 '14 at 15:09
  • if you need to "manually" include such a basic core file it is an indication you are doing it wrong. In addition, you should never assume to know where those files are relative to the executing script. – Mark Kaplun Sep 15 '17 at 7:57
-1

If your contact-form.php exists outside of the Wordpress installation, you will need to ensure that you include the wp-load.php file above your code like this:

<?php include '../../../wp-load.php'; ?>
/* The rest of your code here */

The above works on a file in the root of a theme folder, but if you are unsure of where your .php file may end up in the scheme of directories, you can always use the below script made by Frankie Jarret:

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
/* The rest of your code here */

EDIT:

Please note that the structure of the WP installation can be adjusted so that the wp-content file falls outside of the install tree, in which case the secondary code block would also not work. This is a rare occurence, but the functionality is there, and therefore must be considered in a possible scenario.

There are other possibilities to consider which would require you overhauling your code to make use of actions, filters and Wordpress's built in functions, that would stop you from having to try and the right load dependancies inside your file. This would be the best way to go about it, the above solutions are merely quick/cheap work-around.

  • you should never try to guess where wordpress core files are installed, just don't write code that depends on such knowledge – Mark Kaplun Sep 15 '17 at 7:48
  • @MarkKaplun my apologies if I am misunderstanding you, but that's what the second code snippet addresses? The first code block shows what I suggest we use to achieve the required goal, the second code block addresses the fact that the core file may not always be 3 files back (../../../wp-load.php) as that would be dependant on where your custom .php file is located, and then gives an extended solution that fixes that concern. Or am I misunderstanding your apparent grievance? – Frits Sep 15 '17 at 7:54
  • in general wp-content might be located outside of the wordpress install tree. Except for maybe wp-admin everything else can be in any random place – Mark Kaplun Sep 15 '17 at 7:59
  • .... and while you might be able to assume such things about a specific site, it is a general a bad coding habit – Mark Kaplun Sep 15 '17 at 8:02
  • @MarkKaplun while that is a valid point, I am going to leave this answer here as it has helped me and will hopefully help someone in the future. Might not be the best solution, but it's still a valid solution. I've addressed your concern with an 'edit', thank you for pointing it out, but I must admit that I have never worked with a WP website that had the structure that severely adjusted that the above solution would not work. That said, I know that it is indeed possible, and would love to see what your workaround is in this specific instance. – Frits Sep 15 '17 at 8:17

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.