詳細検索

I want to access the WordPress DB with a script

Avatar
by すがピー
1 min read

I want to access the WordPress DB with a script
Translated from 日本語 • View original

If you want to access the WordPress DB from a script, you can simply access MySQL and process it, but if you want to use a WordPress function for some reason (e.g., scripting an image), you can use the

require_once('wp-blog-header.php');

OK, but some methods require you to include the source in which the method was written directly.

For example, if you want to use wp_insert_category() to register a category,

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

It is necessary to do so. /wp-admin/includes/ is a class that finds what you need and includes it.

Including wp-blog-header.php is also affected if you put web restrictions such as HTTPS access or only visible to logged-in users in the function.php and restrict the entire site.

Therefore, if such a restriction is added, the restriction processing is

if ( ! preg_match( '/^(script.php|hogehoge.*.php)/', basename( $_SERVER['SCRIPT_NAME'] ) ) ){
 Restriction Processing
}

(in the above case, a script that matches "script.php" or "hogehoge.*.php") must be specified to exclude from the process.

If you don't do this, when you run the script, you get stuck on the limit and the process stops.

Related Articles