WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Feb 18 2011

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]

Written by Travis Smith · Categorized: Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. clyde says

    January 25, 2012 at 8:16 pm

    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

    Reply
    • Travis Smith says

      January 30, 2012 at 11:11 am

      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

      Reply
  2. Akin Williams says

    May 3, 2012 at 3:22 pm

    A useful function. Thanks for sharing it!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in