The MU forums have moved to WordPress.org

managing blogs and users via batch scripts (8 posts)

  1. jte
    Member
    Posted 14 years ago #

    I'd like to set up some processes to synchronize our wpmu installation with our account management system. This would ideally involve processing an input file once a day or so and doing blog/user adds/deletes and role/security setup based on the contents of this file.

    Are there any hooks that would allow me to get at the wordpress functions for adding blogs/users outside of the context of a web browser/plugin? Has anyone attempted something like this? I'm not familiar with plugin creation, but it doesn't seem like a good fit here?

  2. tim.moore
    Member
    Posted 14 years ago #

    Is your account management system LDAP-based? There are several LDAP plugins floating around that might help.

  3. jte
    Member
    Posted 14 years ago #

    Sadly, the information I need isn't in our ldap directory. The account management system is custom. I can get data sent to me in whatever format that I want (more-or-less), but I need a way to get at the wpmu functions from some sort of scripting language in order to create the blogs/entities that I need from that.

    Ideally, I could work in perl, or similar so I could easily parse some sort of input file, and then call wpmu functions to do the add/deletes/permission-setting/etc... I'm not sure how one would approach this...

  4. tim.moore
    Member
    Posted 14 years ago #

    Flip through the Codex. You'll find the WordPress functions there. You'll also want to write in PHP.

  5. jte
    Member
    Posted 14 years ago #

    can I get the wordpress functions to work outside of a webserver context?

    this test script:

    <?php
    require( '/<wpmu path>/wp-blog-header.php' );
    require( '/<wpmu path>/wp-includes/registration.php' );
    $username="admin";
    if (username_exists($username))
    echo "exists";
    else
    echo "doesn't exist";
    ?>

    works fine if I dump it in my document root, but if I try and run it from the command line ($ php test.php) I get no output.

    What I'm trying to do would require that I run a job periodically - it doesn't seem appropriate to run within a browser... Can this be done?

  6. SteveAtty
    Member
    Posted 14 years ago #

    You can't run WPMU scripts from the command line without changing a core file.

    If you want to do it :

    Copy wp-config.php to wp-config2.php
    Edit wp-config2.php and change require_once(ABSPATH.'wp-settings.php'); to require_once(ABSPATH.'wp-settings2.php');

    Copy wp-settings.php to wp-settings2.php
    Edit wp-settings2.php and change require_once ( ABSPATH . 'wpmu-settings.php' );to require_once ( ABSPATH . 'wpmu-settings2.php' );

    Copy wpmu-settings.php to wpmu-settings2.php
    Edit wpmu-settings2.php

    Find

    if( $current_site && $current_blog == null ) {
    
    		if( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
    
    			header( "Location: http://&quot; . $current_site->domain . $current_site->path );
    
    			exit;
    
    		}
    
    		$current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path) );
    
    	}

    which is round line 166

    Change it to:

    if( $current_site && $current_blog == null ) {
    
    		#if( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
    
    		#	header( "Location: http://&quot; . $current_site->domain . $current_site->path );
    
    		#	exit;
    
    		#}
    
    		$current_blog = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path) );
    
    	}

    Then in the script you want to run you need to change the include line that says wp-config.php to wp-config2.php

    Its a pain but it works.

    I'm wondering if its worth working out a neater way to do it and putting something into trac

  7. jte
    Member
    Posted 14 years ago #

    I'm still getting no output with the command-line, and expected functionality if I call from within a browser. Test script is now:

    <?php
    require_once( '/<wpmu dir>/wp-config2.php' );
    require( '/<wpmu dir>/wp-includes/registration.php' );
    $username="admin";
    if (username_exists($username))
            echo "exists";
    else
            echo "doesn't exist";
    ?>

    Any thoughts? Many thanks for the help.

  8. jte
    Member
    Posted 14 years ago #

    Is there a good way to figure out why this fails? It seems to just die without any output whatsoever.

    Even with:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    in my files, I get almost nothing indicating why it dies on the command line. Exit value is 0. A simple echo after the require of wp-config2.php never displays, so I assume that there's something in there that's dying, but I've got no leads to go on.

About this Topic