post

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.

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'];
}

Another option is to use $wpdb->get_var (thanks to Kawauso from #WordPress IRC):

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;
}
About Travis Smith

As a WordPress Enthusiast, Travis writes about his journey in WordPress trying to help other WordPress travelers and enthusiasts with tutorials, explanations, & demonstrations of the things he learns.

Comments

  1. clyde says:

    Hi there, two little questions.

    First does the get_ms_option() function loops through all blogs in a MS installation?

    Second is :( I simple cant get it to work, i need to pull out a field from every blog option table and store it in a variable…
    but all i get is nothing.
    Is there a way you can send me a query that pulls something out of the option table?

    Thank you best wishes …. clyde

    • Travis Smith says:

      Hello Clyde,

      This function pulls one option from one site. One approach you could take with this code is to cycle through all available sites/blogs (helpful code) and then use this to build the array. However, you’d be better off talking with Andrea or Ron on the StudioPress forums as they live and breath WordPress Multisite and Genesis.

      Thanks,

      Travis

  2. A useful function. Thanks for sharing it!

Speak Your Mind

*