简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM