如何在自定义PHP脚本中调用wordpress函数

时间:2022-06-05 01:23:04

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_user and wpmu_create_blog.

我有一个Php脚本,我想用它在WPMU中创建一个新的博客。我无法调用像wpmu_create_user和wpmu_create_blog这样的wordpress函数。

My hope is to get this script running as a cron job from command line and pick up new blog creation requests from an external db, create a new blog using the wordpress functions and update the db with new blog info.

我希望从命令行将此脚本作为cron作业运行,从外部数据库中获取新的博客创建请求,使用wordpress函数创建新博客,并使用新的博客信息更新数据库。

7 个解决方案

#1


41  

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的所有功能了

#2


24  

I've got universal solution that will work in any php file inside wp-content folder without any adjustments or needing to know what is misterious '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 anywehre no matter if its plugin or theme file and it will work.

你可以随便粘贴它,无论它的插件或主题文件是否有效。

I think stuff like ../../../.. looks extremely bad and when you modify structure of your folders of theme/plugin you can get crazy.

我觉得像../../../ ..这样的东西看起来非常糟糕,当你修改你的主题/插件文件夹的结构时,你会发疯的。


Note: This solution assumes you didn't rename your wp-content folder.

注意:此解决方案假定您未重命名wp-content文件夹。

#3


14  

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; 
?>

#4


4  

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安装在根目录上时。大多数情况都是如此。

#5


2  

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次。这取决于您的结构检查手动。恩。如果您的自定义文件位于您的主题/ yourtheme / custom.php中,那么上面的代码将完美运行,如果没有,则根据您的路径添加../或删除一个或多个../。

#6


1  

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

?>

#7


0  

wordpress uses a phpass function -

wordpress使用phpass功能 -

This worked for me as I had a password and the hash in a table (migrated wp users) and had to find a way to check login details -

这对我有用,因为我有一个密码和表中的哈希值(迁移的wp用户),并且必须找到一种检查登录详细信息的方法 -

Grab this download here - https://github.com/sunnysingh/phpass-starter

抓住这个下载 - https://github.com/sunnysingh/phpass-starter

all u 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!

#1


41  

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的所有功能了

#2


24  

I've got universal solution that will work in any php file inside wp-content folder without any adjustments or needing to know what is misterious '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 anywehre no matter if its plugin or theme file and it will work.

你可以随便粘贴它,无论它的插件或主题文件是否有效。

I think stuff like ../../../.. looks extremely bad and when you modify structure of your folders of theme/plugin you can get crazy.

我觉得像../../../ ..这样的东西看起来非常糟糕,当你修改你的主题/插件文件夹的结构时,你会发疯的。


Note: This solution assumes you didn't rename your wp-content folder.

注意:此解决方案假定您未重命名wp-content文件夹。

#3


14  

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; 
?>

#4


4  

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安装在根目录上时。大多数情况都是如此。

#5


2  

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次。这取决于您的结构检查手动。恩。如果您的自定义文件位于您的主题/ yourtheme / custom.php中,那么上面的代码将完美运行,如果没有,则根据您的路径添加../或删除一个或多个../。

#6


1  

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

?>

#7


0  

wordpress uses a phpass function -

wordpress使用phpass功能 -

This worked for me as I had a password and the hash in a table (migrated wp users) and had to find a way to check login details -

这对我有用,因为我有一个密码和表中的哈希值(迁移的wp用户),并且必须找到一种检查登录详细信息的方法 -

Grab this download here - https://github.com/sunnysingh/phpass-starter

抓住这个下载 - https://github.com/sunnysingh/phpass-starter

all u 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!