4

I have been writing a function using the Shortcode Exec PHP plugin and the function works great when I run it inside the editor.

When I move it to a plugin I begin to see errors in the log such as this:

PHP Fatal error: Call to undefined function wp_create_category()

I realize that this is because of lack of includes, etc.

What is the correct way to include the built-in wordpress functions for a plugin?

My plugin uses the following wordpress functions

wp_create_category
username_exists
wp_generate_password
wp_create_user
wp_insert_post
update_post_meta
add_post_meta
3
  • try including this to your file and let me know then -- require_once(WORDPRESS_HOME. 'wp-config.php'); require_once(WORDPRESS_HOME. 'wp-includes/wp-db.php'); require_once(WORDPRESS_HOME. 'wp-admin/includes/taxonomy.php'); Commented Aug 29, 2012 at 3:54
  • This works. Why don't you submit it as an answer so I can credit you? Commented Aug 29, 2012 at 4:19
  • done..added as an answer check now :) and thx for showing your concern :) Commented Aug 29, 2012 at 4:52

3 Answers 3

16

Use below code, working fine , i have tested

   require_once(ABSPATH . 'wp-config.php'); 
   require_once(ABSPATH . 'wp-includes/wp-db.php'); 
   require_once(ABSPATH . 'wp-admin/includes/taxonomy.php'); 
Sign up to request clarification or add additional context in comments.

2 Comments

voting this up because of the reference to ABSPATH.
For me only require_once(ABSPATH . 'wp-admin/includes/taxonomy.php'); was nessecary (along with require_once( "../wp-load.php" );)
3

try including this to your file and let me know then --

require_once(WORDPRESS_HOME. 'wp-config.php'); 
require_once(WORDPRESS_HOME. 'wp-includes/wp-db.php'); 
require_once(WORDPRESS_HOME. 'wp-admin/includes/taxonomy.php'); 

Comments

0

I know this is very old question, but somehow noone mentioned solution I have in mind.

I think the correct way of doing this is by using correct init hook in your plugin. Adding require_once() seems like hack to me.

So some functions are being loaded only for admin area and some are only loaded for frontend. So depending on those use correct hook in your plugin.

add_action('init', function () {}

or

add_action('admin_init', function () {}

You can have both of them in one plugin of course.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.