WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Feb 22 2011

How to Get an Array of Options Tables from WordPress Multisite, get_ms_options_tables()

This simple function will return an array of the various WordPress multisite options table names that exist in the WordPress database. For example, this function will return an array that looks like:

Array
(
[0] => wp_options
[1] => wp_2_options
[2] => wp_3_options
[3] => wp_6_options
[4] => wp_9_options
[5] => wp_11_options
[6] => wp_12_options
[7] => wp_14_options
[8] => wp_17_options
[9] => wp_19_options
)

[php]function get_ms_options_tables() {
global $wpdb;
$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
$query .= " ORDER BY {$wpdb->blogs}.blog_id ";
$order = "ASC";
$query .= $order;
$sql = $wpdb->prepare($query);
$blogs = $wpdb->get_results( $sql, ARRAY_A );
//$blogs = $this->get_ms_sites();
foreach ($blogs as $blog) {
$tables[] = $wpdb->get_blog_prefix($blog['blog_id']).'options';
}
return $tables;
}[/php]

Written by Travis Smith · Categorized: Tutorials

Feb 21 2011

How to Write a Simple update_ms_option() Function for WordPress Multisite

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

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

$wpdb->query( $wpdb->prepare( "
INSERT INTO `".DB_NAME."`.`".$wpdb->get_blog_prefix($blogID)."options`
(
`option_id` ,
`blog_id` ,
`option_name` ,
`option_value` ,
`autoload`
)
VALUES (NULL, '0', '%s', '%s', 'yes')
ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = '%s', `autoload` = VALUES(`autoload`)",
array($option_name, $option_value, $option_value) ) );
[/php]

Written by Travis Smith · Categorized: Tutorials

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

Feb 17 2011

How to Write a Simple get_ms_sites() Function

This will return an array of sites from WordPress Multisite.

Simple Version

[php]function get_ms_sites() {
global $wpdb;

$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
$query .= " ORDER BY {$wpdb->blogs}.blog_id ";
$order = "ASC";
$query .= $order;
$sql = $wpdb->prepare($query);
$blog_list = $wpdb->get_results( $sql, ARRAY_A );

return $blog_list;
}
[/php]

"Complicated Version"

[php]function get_ms_sites() {
global $wpdb;
$include_public_only = true; // 1 is public; 0 is private
$include_archived = false; // 1 is archived;0 is not
$include_mature = false; // 1 is mature;0 is not
$include_spam = false; // 1 is spam;0 is not
$include_deleted = false; // 1 is deleted;0 is not

$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
//Options
$query .= " AND {$wpdb->blogs}.public = ". (($include_public_only) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.archived = ". (($include_archived) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.mature = ". (($include_mature) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.spam = ". (($include_spam) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.deleted = ". (($include_deleted) ? "'1'" : "'0'");

//Query Order
$qorder = " ORDER BY {$wpdb->blogs}.blog_id "; //Next lines contain other options
//$qorder = " ORDER BY last_updated ";
//$qorder = " ORDER BY domain ";
$query .= $qorder;

//Order ASC = ascending; DESC = descending;
$order.= "ASC"; //can also be "DESC"
$query .= $order;
$sql = $wpdb->prepare($query);
$blog_list = $wpdb->get_results( $sql, ARRAY_A );

return $blog_list;
}[/php]

Written by Travis Smith · Categorized: Tutorials

Feb 08 2011

How to Add a Custom Login/Logout Message in WordPress

In a previous post, How to Add a Custom Message on the Login or Register Screen, I talked about how to add a custom message to either the login screen or the register screen. Recently, I was asked how to create a custom message for logging out. Since WordPress uses the wp-login.php file to logout as well, then it isn't as simple as the last post.

So, to add a custom message to your logout screen, you need to add the following to your functions.php file.

[php]function custom_login_head() //outputs the CSS needed to blend custom-message with the normal message
{?>
<style type="text/css">
#login_error, .message { display:none; }
.custom-message {
-moz-border-radius:3px 3px 3px 3px;
border-style:solid;
border-width:1px;
margin:0 0 16px 8px;
padding:12px;
}
.login .custom-message {
background-color:#FFFFE0;
border-color:#E6DB55;
}
</STYLE><?php
}

add_action('login_head','custom_login_head');

function custom_logout_message() {
if ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] ) //check to see if it's the logout screen
{
$message = "<p class='custom-message'>Custom logged out Message.</p><br />";
}
else //they are logged in
{
$message = "<p class='custom-message'>Custom Login Message.</p><br />";
}

return $message;
}
add_filter('login_message', 'custom_logout_message');[/php]

Written by Travis Smith · Categorized: Tutorials

  • « Previous Page
  • 1
  • …
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • Next Page »
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

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