# How to load WP functions?

I have a mymail.php script which sends a email (I managed to do it using require_once( \$_SERVER['DOCUMENT_ROOT'] . '/wp-includes/class-phpmailer.php' );.) and outputs a plain text string (Such as "Email is sent successfully.")

I want to use the functions defined in WP in this my script. The particular function I want to call is get_option() in order to retrieve the email user of the site owner. Which file to import for the particular get_option() function and which to import the entire WP core (like the context available for theme's .php files?

-
Where does this mymail.php file live, and how is it executed? Is it part of a Plugin or Theme, or is it a stand-alone file executed outside of WordPress? –  Chip Bennett Apr 5 '12 at 16:38
@Chip Bennett: <code>mymail.php</code> is located in the same dir where <code>style.css</code> and <code>functions.php</code> of the custom theme I am developing is located. It is executed from a .js file when a user clicks a submit button in a form of our theme. <code>mymail.php</code> is executed outside of WordPress. –  porton Apr 5 '12 at 16:41

Put this near the top of your PHP file:

require_once('wp-blog-header.php');


You may also want to reduce wordpress core load by defining SHORT_INIT, though this depends on which functions you are using ( and wether they get loaded when this is defined ).

But this is completely the wrong way of going about it. Because its:

• more effort
• wasteful
• slower
• less secure
• likely to incurr a higher maintenance and support cost

Using these APIs will save you time and effort, and it will make things simpler/easier in the longrun ( and more secure )

e.g.

// PHP ( functions.php )

function mymail() {
// send email
echo "Success email sent";
return;
}


and

// js
jQuery.post(
ajaxurl,
{
'action':'mymail',
'data':'the form submission data'
},
function(response){
alert('The server responded: ' + response);
}
);

-
I'm not going to downvote, but I want to reiterate that I think this is the wrong approach, even though it will work. –  Chip Bennett Apr 5 '12 at 16:58
Indeed, I agree entirely, so I answered the question and provided a much better alternative instead using the WP AJAX apis (see edited answer) –  Tom J Nowell Apr 5 '12 at 17:00
Well, strike my previous comment; I left it while you were editing your question. :) +1 for the wp_jax_ suggestion! –  Chip Bennett Apr 5 '12 at 17:00

mymail.php is located in the same dir where style.css and functions.php of the custom theme I am developing is located. It is executed from a .js file when a user clicks a submit button in a form of our theme. mymail.php is executed outside of WordPress.

mymail.php is executed outside of WordPress.

Since the functionality is part of your Theme, there's no reason you can't/shouldn't execute mymail.php from within WordPress.

Have you considered using the wp_mail() function for your form submission?

-
It seems you haven't understood my question. My question: How to get WP's functions for a script which is not a part of a WP theme or plugin? –  porton Apr 5 '12 at 16:50
No, to the contrary: I understand your question completely. I am implying that taking the functionality outside of WordPress is the wrong approach, and that you should instead execute your form mailer from within WordPress. –  Chip Bennett Apr 5 '12 at 16:57
define( 'WP_USE_THEMES', FALSE );

Alternative to wp-load.php you can also load the wp-blog-header.php, but the wp-load.php is the default file to load WP environment.
Also an hint, set the const define( 'SHORTINIT', TRUE ); to load much more less of files and WP faster. But check it; with this const load WP much smaller and only the important files. (see more information about this on my answer on other question)