I am trying to trigger an add_action hook (add_action("wpcf7_before_send_mail", "add_data_to_custom_table");) from outside the wordpress loop. The add_action hook is defined in theme's functions-custom.php.

in domain.com/test.php i have

define('WP_USE_THEMES', false);
define('WP_CUSTOM_TABLE', 'test_table');

/** Loads the WordPress Environment and Template */
require( '../wp/wp-blog-header.php' );


<?php echo do_shortcode('[contact-form-7 id="417" title="Test Contact Form"]'); ?>

In this setup the add_action hook is not working. If however i move the add_action hook definition to the theme's functions.php it does work.

My question: how can i include a functions-custom.php file outside the wordpress loop with working add_actions without including it from the theme's functions.php???

  • 1
    The fact that you are requireing a Core file means you are doing this wrong (about 99.9% certainty). Please explain the problem and perhaps someone can offer a proper solution.
    – s_ha_dum
    Nov 20, 2015 at 1:08

1 Answer 1


Just simply put your add_action hook in a simple plugin. Activate the plugin and watch it run with the rest of the plugins.

If you insist on putting the add_action in the theme's functions.php then simply define a constant in your point of entry script, like this: define('SCRIPTONOMY', true);

Finally run a conditional in the functions.php testing for the constant.

  • your define('SCRIPTONOMY', true); seems to be a solution for my testing files.
    – alex
    Nov 20, 2015 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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