How to call wordpress functions in custom php scri

2019-01-16 10:20发布

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.

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.

标签: php blogs wpmu
7条回答
聊天终结者
2楼-- · 2019-01-16 10:39

wordpress uses a phpass function -

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 -

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

all u need is this basic function to check a text password to a WordPress hash:

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-16 10:40

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

?>

查看更多
劫难
4楼-- · 2019-01-16 10:49

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'

require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );

It just automatically goes up to root of wordpress and loads 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.

查看更多
forever°为你锁心
5楼-- · 2019-01-16 10:55

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

#!/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; 
?>
查看更多
Evening l夕情丶
6楼-- · 2019-01-16 10:56

include wp-load.php file (in the root of your wordpress installation) in your php script file like so,

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

查看更多
乱世女痞
7楼-- · 2019-01-16 10:56

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.

查看更多
登录 后发表回答