Best way to load WordPress Bootstrap

Requiring a file using relative path is never a good idea, that because wp content folder can be easily changed and that make your code fail. You should convert your file into a MU plugin (you only need to save it wp-content/mu-plugins folder) and change it to something like: add_action( ‘wp_ajax_super_custom_stuff’, ‘my_super_custom_stuff’ ); add_action( ‘wp_ajax_nopriv_super_custom_stuff’, … Read more

How to avoid wp-load.php within a PHP/CSS file?

Here is a basic setup that I use for AJAX with WordPress. instead of loading wp-load.php; just use WordPress default method for AJAX calls. This allows you to also filter function calls from Javascript through a switch. I also added a quick example for wp_localize_script. <?php add_action(‘wp_enqueue_scripts’, ‘YOUR_NAME_scripts’); //back end function YOUR_NAME_scripts( $hook_suffix ) { … Read more

WordPress function gives different results outside vs inside PHPUnit test

The reason this is happening is actually not because of anything in the code you’ve posted, per se, but because of how you have PHPUnit configured. The shortcode callbacks are stored in a global ($shortcode_tags). When you load WP, the shortcode is registered and added to the global. You call shortcode_exists(), it checks $shortcode_tags, and … Read more

How to load wordpress environment without loading the template?

If you place the file in the WP root directory, e.g. http://mysite.com/myscript.php require( dirname(__FILE__) . ‘../blog/wp-load.php’ ); if (function_exists(‘wp_create_user’)) { echo “wp_create_user() found”; } If you are in a different directory, just make sure you are loading wp-load.php from the proper location.

Initialize WordPress environment to use in command line script

I came up with following solution. The script have to start with the following code. <?php if( php_sapi_name() !== ‘cli’ ) { die(“Meant to be run from command line”); } function find_wordpress_base_path() { $dir = dirname(__FILE__); do { //it is possible to check for other files here if( file_exists($dir.”/wp-config.php”) ) { return $dir; } } … Read more