Kevin
Kevin

Reputation: 3730

wordpress error : Call to undefined function in plugin

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

Upvotes: 4

Views: 10547

Answers (3)

codelame
codelame

Reputation: 537

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.

Upvotes: 0

thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

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'); 

Upvotes: 16

swapnesh
swapnesh

Reputation: 26722

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'); 

Upvotes: 3

Related Questions