Created
March 29, 2013 03:18
-
-
Save torounit/5268546 to your computer and use it in GitHub Desktop.
WordPressのマルチサイト用のユーティリティ ブログパスからblog_id取得とか。サブディレクトリ用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: MU Blog Util | |
Description: マルチサイト用のユーティリティ | |
Author: Toro-Unit | |
*/ | |
class MU_Blog_Util { | |
public $blogs; | |
public function __construct() { | |
global $wpdb; | |
$this->blogs = array(); | |
$blogs = $wpdb->get_results( "SELECT blog_id FROM ".$wpdb->blogs." ORDER BY blog_id" ); | |
foreach ($blogs as $blog) { | |
$detals = get_blog_details( $blog->blog_id ); | |
$key = trim($detals->path,"/"); | |
if(!$key) { | |
$key = "root"; | |
} | |
$this->blogs[$key] = $detals; | |
} | |
} | |
public function get_blog_by_path( $path ) { | |
$path = trim( $path, "/" ); | |
if(!isset($this->blogs[$path])) { | |
$path = "root"; | |
} | |
return $this->blogs[$path]; | |
} | |
public function switch_to_blog_by_path( $new_blog_path, $validate = false ) { | |
$blog = $this->get_blog_by_path( $new_blog_path ); | |
return switch_to_blog( $blog->blog_id, $validate ); | |
} | |
} | |
$mu_blog_util = new MU_Blog_Util(); | |
function get_blog_by_path( $path ) { | |
return $mu_blog_util->get_blog_by_path( $path ); | |
} | |
function switch_to_blog_by_path( $new_blog_path, $validate = false ) { | |
return $mu_blog_util->switch_to_blog_by_path( $new_blog_path, $validate = false ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment