Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

up vote 5 down vote accepted

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

Judging from your comment, I would advise you use the WP AJAX APIs instead, see here:

http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

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 )
add_action('wp_ajax_mymail', 'mymail');

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);
   }
);
share|improve this answer
    
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
1  
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
1  
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.

So, basically, I think you've just answered your own question:

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?

share|improve this answer
    
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
2  
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 );
require( './wp-load.php' );

Use the defined constant to load faster and not all, waht WP have. You will use it on a custom mail function, but its not necessary, that you load all the functions for themes and the themes.

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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