如何在自定义 PHP 脚本中调用 WordPress 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2091521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call WordPress functions in a custom PHP script
提问by sid.sri
I have a PHP script I want to use for creating a new blog in WPMU. I am having trouble calling WordPress functions like wpmu_create_userand wpmu_create_blog.
我有一个 PHP 脚本,我想用它在 WPMU 中创建一个新博客。我在调用wpmu_create_user和wpmu_create_blog等 WordPress 函数时遇到问题。
My hope is to get this script running as a cronjob from the command line and pick up new blog creation requests from an external db, create a new blog using the WordPress functions and update the database with the new blog information.
我希望让这个脚本从命令行作为cron作业运行,并从外部数据库中获取新的博客创建请求,使用 WordPress 功能创建一个新博客,并使用新博客信息更新数据库。
回答by Pragati Sureka
include wp-load.php file (in the root of your wordpress installation) in your php script file like so,
像这样在你的 php 脚本文件中包含 wp-load.php 文件(在你的 wordpress 安装的根目录中),
require_once("/path/to/wordpress/wp-load.php");
you will have to provide the abspath of the wp-load file, now you can use all the functions of wordpress in your php script
您必须提供 wp-load 文件的 abspath,现在您可以在 php 脚本中使用 wordpress 的所有功能
回答by pie6k
I've got a universal solution that will work in any PHP fileinside the wp-contentfolder without any adjustmentsor needing to know what is mysterious 'path/to/wordpress'
我有一个通用的解决方案,可以在文件wp-content夹内的任何 PHP 文件中工作,无需任何调整或需要知道什么是神秘的'path/to/wordpress'
require_once(explode("wp-content", __FILE__)[0] . "wp-load.php");
It just automatically goes up to root of WordPress and loads wp-load.php.
它会自动上升到 WordPress 的根目录并加载wp-load.php。
You can just paste it anywhere, no matter if it's a plugin or theme file, and it will work.
您可以将它粘贴到任何地方,无论是插件还是主题文件,它都会起作用。
I think stuff like ../../../..looks extremely bad and when you modify the structure of your folders of a theme or plugin you can get crazy.
我认为像这样的东西../../../..看起来非常糟糕,当您修改主题或插件的文件夹结构时,您可能会发疯。
Note: This solution assumes you didn'trename your wp-contentfolder.
注意:此解决方案假定您没有重命名wp-content文件夹。
回答by Pablo
For wordpress 3.1, I had to specify the host/domain because wp-includes/ms-settings.php:100 needs it or it dies. So my script looks something like (note I am using it for a network/multiblog site):
对于 wordpress 3.1,我必须指定主机/域,因为 wp-includes/ms-settings.php:100 需要它,否则它就会死掉。所以我的脚本看起来像(注意我将它用于网络/多博客站点):
#!/usr/bin/php -q
<?php
#for multi-blog only
$blog_id = 1;
#specify host or domain (needed for wp-includes/ms-settings.php:100)
$_SERVER[ 'HTTP_HOST' ] = 'localhost';
#location of wp-load.php so we have access to database and $wpdb object
$wp_load_loc = "/path/to/wordpress/wp-load.php";
require_once($wp_load_loc);
#for multi-blog only
switch_to_blog($blog_id);
#test to make sure we can access database
echo $wpdb->prefix;
?>
回答by Arun Basil Lal
This should work:
这应该有效:
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
i.e. When the php script is on the same server and WP is installed on the root. As most cases are.
即当 php 脚本在同一台服务器上并且 WP 安装在 root 上时。就像大多数情况一样。
回答by Jigar Navadiya
require_once('../../../wp-load.php');
I think you have to add this line before any usage of wordpress function in your custom file. and make sure i have add ../ 3 times as per my wordpress installation structure.It's depends on your structure checks manually. ex. if your custom file is inside your themes/yourtheme/custom.php then above code will works perfectly and if not then add ../ or remove one or more ../ as per your path.
我认为您必须在自定义文件中使用 wordpress 函数之前添加这一行。并确保我按照我的 wordpress 安装结构添加了 ../ 3 次。这取决于您手动检查结构。前任。如果您的自定义文件在您的 themes/yourtheme/custom.php 中,那么上面的代码将完美运行,如果不是,则根据您的路径添加 ../ 或删除一个或多个 ../ 。
回答by sid.sri
Following is the code I am using:
以下是我正在使用的代码:
<?PHP
require_once ('/path/to/wordpress/wp-load.php');
require_once ('/path/to/wordpress/wp-blog-header.php');
require_once ('/path/to/wordpress/wp-includes/registration.php');
do_action('wpmuadminedit', '');
//Code to Connect and Select the external database
//Code to Connect to the external DB and get the new order details:
NewBlogName=$name and AdminEmail=$email
if ( !email_exists($email) )
{
// email does exist, create a new user
$name = create_name_from_email($email);
$password = "use a default password";
$user_id=wpmu_create_user($name, $password, $email);
create_blog($email, $title, $user_id, $password);
}
else
{
// user exists, create new blog
$user_id=email_exists($email);
$password = "use existing wordpress password";
create_blog($email, $title, $user_id, $password);
}
function create_name_from_email ($email) {
preg_match('/[^@]+)@/',$email,$matches);
$name = $matches[1];
return $name;
}
//Creates a new blog, expects user to already exist.
function create_blog($email, $title, $user_id, $password)
{
//Code to Update external DB that the order is in process
$public = array("public" => 1);
if (constant('VHOST') == 'yes')
{
$newdomain = $domain . "." . $current_site->domain;
$path = $base;
}
else
{
$newdomain = $current_site->domain; $path = $base . $domain . '/';
}
$domain = strtolower($domain);
$newdomain = strtolower($newdomain);
$path = strtolower($path);
$meta = apply_filters('signup_create_blog_meta', array('lang_id' => 1, $public));
$meta = apply_filters("add_singup_meta", $meta);
wpmu_create_blog($newdomain, $path, $title, $user_id , $meta, $current_site->id);
do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $title, $meta);
// Update external DB with BlogUrl, NewBlogName, AdminPassword,
OrderStatus=Complete.
mysql_close($con);
?>
回答by Fstarocka Burns
WordPress uses a phpassfunction.
WordPress 使用phpass函数。
This worked for me as I had a password, and the hash in a table (migrated WordPress users) and had to find a way to check login details.
这对我有用,因为我有密码和表中的哈希值(迁移的 WordPress 用户),并且必须找到一种方法来检查登录详细信息。
Grab this download here - https://github.com/sunnysingh/phpass-starter
在这里下载 - https://github.com/sunnysingh/phpass-starter
All you need is this basic function to check a text password to a WordPress hash:
您只需要这个基本功能来检查 WordPress 哈希的文本密码:
<?php
require_once("PasswordHash.php");
$hasher = new PasswordHash(8, false);
// Check that the password is correct
$check = $hasher->CheckPassword($password, $stored_hash);
if ($check) {
// Password good
} else {
// Password wrong
}
?>
All credits to Sunny Singh!
所有学分都归功于 Sunny Singh!

