$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How can I call WordPress core functions in external scripts?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How can I call WordPress core functions in external scripts?

matteradmin10PV0评论

I'm trying to develop a code to call some Codex functions () the problem is simple, I don't find any doc where read about how to start.

I have a PHP file with this line for example:

<?php

 $website = "";
 $userdata = array(
 'user_login'  =>  'login_name',
 'user_url'    =>  $website,
 'user_pass'   =>  NULL  // When creating an user, `user_pass` is expected.
);

 $user_id = wp_insert_user( $userdata ) ;

 //On success
 if ( ! is_wp_error( $user_id ) ) {
 echo "User created : ". $user_id;
 }

?>

Where I need to put this code to check if works? Where I need to call it? Must I create a plugin? Can I call those functions from a script in a specific folder?

For example, the funcion wp_insert_user is in /wp-includes/user.php, can I call the function just including the script?

include('user.php')

Where are the rest of the functions?

Someone knows an specific manual with a simple doc? I'm getting crazy.

This is my first script for a CMS and I don't undertand how it works, but I dont find manuals or simple doc.

I'm trying to develop a code to call some Codex functions (https://codex.wordpress/Function_Reference) the problem is simple, I don't find any doc where read about how to start.

I have a PHP file with this line for example:

<?php

 $website = "http://example";
 $userdata = array(
 'user_login'  =>  'login_name',
 'user_url'    =>  $website,
 'user_pass'   =>  NULL  // When creating an user, `user_pass` is expected.
);

 $user_id = wp_insert_user( $userdata ) ;

 //On success
 if ( ! is_wp_error( $user_id ) ) {
 echo "User created : ". $user_id;
 }

?>

Where I need to put this code to check if works? Where I need to call it? Must I create a plugin? Can I call those functions from a script in a specific folder?

For example, the funcion wp_insert_user is in /wp-includes/user.php, can I call the function just including the script?

include('user.php')

Where are the rest of the functions?

Someone knows an specific manual with a simple doc? I'm getting crazy.

This is my first script for a CMS and I don't undertand how it works, but I dont find manuals or simple doc.

Share Improve this question edited Nov 10, 2016 at 8:53 JHoffmann 1,88014 silver badges18 bronze badges asked Nov 7, 2016 at 14:29 Leonardo CavaniLeonardo Cavani 2631 gold badge2 silver badges6 bronze badges 3
  • 2 To clear up some confusion... the "Codex" is WordPress core documentation. These functions are built-in to WP core and are already available. Codex is not an add-on for WordPress. I would recommend reviewing the documentation on plugin development and find a tutorial or two about writing a basic plugin. This will give you a better understanding of how WP executes and the proper ways to expand the core functionality. codex.wordpress/Writing_a_Plugin – jdm2112 Commented Nov 7, 2016 at 14:31
  • I had tried to develop a plugin, the package was built here: wppb.me. I can see my plugin listed in my admin panel. The problem is when I try to call some of the functions, Wordpress fail, it says that the function doesn't exist. – Leonardo Cavani Commented Nov 7, 2016 at 14:37
  • 1 Context is everything here as WP core follows a very complex flow with each request. Depending on where and where your code is executed, certain classes and globals of the WP environment may or may not be available. Understanding where to hook or trigger your code is a difficult concept for those new to WP. tipsandtricks-hq/… – jdm2112 Commented Nov 7, 2016 at 16:48
Add a comment  | 

2 Answers 2

Reset to default 9

I get the solution.

To call functions from Wordpress from a custom script, you need to import wp-load:

require_once("/path/wp-load.php");

Thats all, I can work fine with those functions. I save my own script in the root of my PHP Wordpress and I didn't need a plugin.

External files can easily access the wordpress functions. You just need to include the file wp-load.php in your external file. The wp-load.php file is located in root of your wordpress installation. Example: Suppose your file is test.php located at root directory of wordpress installation.

<?php
    require_once('wp-load.php');
    // Your custom code
?>

Source: How to access WordPress functions in external file

Post a comment

comment list (0)

  1. No comments so far