35

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 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.

2
  • What's the code you have right now? We really need to see something in order to diagnose it. Commented Jan 19, 2010 at 6:28
  • Brian, See code below, everything between the horizontal lines is my code. I don't know how to format it so it shows all code in the special format. Commented Jan 19, 2010 at 18:14

7 Answers 7

42

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

Sign up to request clarification or add additional context in comments.

3 Comments

yes it will load wordpress too... but will make all the functions avaliable for use
i tried the above solution. Works in one file but not in the consequent files.
well ofcourse it wont... if you are not including that fle in your main script(in which you included wp-load.php) it wont work... then you will have to include it in all the files.
34

I've got a universal solution that will work in any PHP file inside the wp-content folder without any adjustments or needing to know what is mysterious '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 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't rename your wp-content folder.

5 Comments

This does assume that you haven't renamed your wp-content folder. Great answer
Thats true, I'll add note about this to my answer.
Just add this instead: include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
What if your wordpress is not inside root folder? eg. mywebsite.com/my/wordpress/dir?
Helpful answer :)
16

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

1 Comment

Thank you! switch_to_blog() was exactly what I was looking for here!
5

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.

2 Comments

Not in a cron job (as requested by OP) they're not.
It will fail if wordpress is not inside root of server, but in some folder like www.mysite.com/some/path/wordpress
2

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

?>

Comments

1
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.

Comments

1

WordPress uses a phpass function.

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.

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

All you 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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.