Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm using a function to execute a code that sends an email with an attachment. This function is hooked on to a filter from woocommerce.

I'd like to rebuild this function to execute the code from an external php file. Is this possible in Wordpress?

The reason I want to execute it from an external script is because I want some more functionality, the sender of the email and the attachment are generated from sessions.

In my child theme's function.php-

function send_invoice () 
{
    $attachments = array( WP_CONTENT_DIR . '/themes/mytheme/invoices/invoice.pdf' );
    wp_mail( '[email protected]', 'Attachment test', 'The message', 'header', $attachments);
}
add_filter( 'woocommerce_email_attachments', 'send_invoice' );

Is there a way to execute a php file from my functions.php?

Further elaboration: I'm trying to convert the previous script to something like: Functions.php

function execute_send_invoice () {
    send_invoice();
}   
add_filter( 'woocommerce_email_attachments', 'execute_send_invoice' );

And then the code for send_invoice(); somewhere outside my functions.php, for instance, in myscript.php which is located in my template directory.

share|improve this question
    
If the file is included, wordpress or not, you can just call the function like normal – ಠ_ಠ Aug 26 '13 at 14:39
    
So I don't have to link the new php file, if I just write my function there I can call it in my functions.php? BTW can you define, "included"? Do I have to link it manually, or is a file included when it is in my theme directory? – user25312 Aug 26 '13 at 14:40
    
see my answer for an explanation. With wordpress, functions.php is automatically included when the theme is active. – ಠ_ಠ Aug 26 '13 at 14:49
up vote 1 down vote accepted

Needless to say, you should be very careful about running external scripts in PHP.

If there isn't some kind of preparsing done by wordpress before executing your PHP files, there are two main ways to run external code. Using the first, you must further enclose the PHP in <?php ?> tags, but it's my personal suggestion.

include('full_or_relative_php_path_to_file.php');

or

eval(file_get_contents('full_or_relative_php_path_to_file.php'));
share|improve this answer
    
I am facing same problem here which file you have included and how. – Sanjay Nakate Nov 9 '13 at 13:18

If the functions.php file is inside the theme that is currently active, it is included by default. Therefore, you can access and call the function like you normally would (as if it was defined on the same page that you will be calling the function from).

see here

share|improve this answer
    
That is the functionality I'm trying to achieve, only backwards. My goal is to have the function like this: function execute_send_invoice() { send_invoice(); } add_filter( 'woocommerce_email_attachments', 'execute_send_invoice' ); And have the code for send_invoice(); outside of my functions.php file. – user25312 Aug 26 '13 at 14:51
1  
In that case, use include or require within functions.php to add the PHP file that contains send_invoice(). – ಠ_ಠ Aug 26 '13 at 14:56

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.