Site icon WP Smith

How to Write a Simple get_ms_option() That Reflects get_option() for WordPress Multisite

For WordPress Multisite, I needed a function that worked like get_option(). So here's a simple one I wrote.

[php]function get_ms_option($blogID, $option_name) {
global $wpdb;

$select_statement = "SELECT *
FROM `".DB_NAME."`.`".$wpdb->get_blog_prefix($blogID)."options`
WHERE `option_name` LIKE '".$option_name."'";
$sql = $wpdb->prepare($select_statement);
$option_value = $wpdb->get_results( $sql, ARRAY_A );
return $option_value[0]['option_value'];
}
[/php]

Another option is to use $wpdb->get_var (thanks to Kawauso from #WordPress IRC):
[php]function get_ms_option($blogID, $option_name) {
global $wpdb;

$select_statement = "SELECT *
FROM `".DB_NAME."`.`".$wpdb->get_blog_prefix($blogID)."options`
WHERE `option_name` LIKE '".$option_name."'";
$sql = $wpdb->prepare($select_statement);
$option_value = $wpdb->get_var( $sql, 3 );
return $option_value;
}
[/php]