4

I'm running multisite with each subsite using the same custom theme. I'm using NHP Options for logo upload and contact page info changes in that theme.

I'd like to add a site list page to the main site (which is using a different theme) and have each subsites logo be displayed beside its name in the list.

Getting the site list was fine - no problems there, but for the life of me I can't figure out how to get the logos to show.

The data is stored in a serialized array in each sites options table - with the option_name of 'nhp_opts'.

The data looks like this :

    a:5:{s:8:"last_tab";s:1:"0";i:19;s:59:"http://_PATH_TO_LOGO.png";i:20;
    s:34:"Test Street,Testing,Co. Test";i:21;s:16:"(000) 123 456789";s:10:"multi_text";
    a:4:{i:0;s:21:"Mon - Fri 9.30 - 6.00";i:1;s:18:"Sat - 10.00 - 6.00";
    i:2;s:18:"Sun - 12.00 - 5.00";i:3;s:22:"Bank Holidays - Closed";}}

and the code I cobbled together is this:

    <?php global $wpdb;
            $logos = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->options WHERE option_name = nhp_opts  "));
                foreach($logos as $logo){
                $thedata = $logos;
                $thedata = unserialize($thedata);
                echo $thedata;

                }?>

All I get is Array() - repeated a bunch of times.

I'm not very familiar with working directly with the database - so I'm not overly surprised this didn't work!

I've spent a few days now puzzling this one out - any help at all would be greatly appreciated! Thanks!

2
  • I'd suggest you to remake the Question. There is too much unrelated content, and little focusing on the actual problem: retrieve the same theme option from all sites of the network. It doesn't matter if it is NHP or any other. :::: Btw, does this work if you put it in a template file: echo get_option('nhp_opts');?
    – brasofilo
    Commented Dec 3, 2012 at 21:32
  • 1
    Hi brasofilo, thanks for chiming in. On re-reading my original post I agree, it was a tad bloated. I've taken your advice and edited for clarity. 'echo get_option('nhp_opts');' in the template file returns nothing.
    – Yelper
    Commented Dec 4, 2012 at 10:17

2 Answers 2

1

may be its a late reply, but i hope this code will help you and anothers to solve this problem.

 <?php
  global $wpdb;
  $blogs = $wpdb->get_results("SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' AND blog_id != 1 ");

  $sites = array();

 foreach ($blogs as $blog) {

 $sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'nhp_opts');

}
  ?>

now you can use img tag and loop through this to get your option, for a purpose the current site option is omitted from sql, if you want to include current site in your result as well you can ignore AND blog_id != 1 this line from sql.

I hope this will help others as well.

0

Using get_option() will do the unserializing for you and is much easier.

<?php

global $wpdb;

$sites = $wpdb -> get_results($wpdb -> prepare("SELECT * FROM wp_blogs ORDER BY blog_id"));

foreach ($sites as $site) {
    switch_to_blog( $site);
    echo get_option('nhp_opts');
    restore_current_blog();
}

I think there might be a better way to do this in 3.5 but I can't find the ticket at the moment.

5
  • Hiya developdaly, thanks very much for your input. I've played around with the code you entered, and a couple of variations of same, but nothing seems to be working. I even tried a var_dump on 'nhp_opts' and nada. Still scratching my head here! Maybe I just need to walk away from it for a whle! Thanks for taking the time to look at it tho - I do appreciate your help.
    – Yelper
    Commented Dec 3, 2012 at 15:28
  • Now that I think of it ... my answer won't work. It's looping through the sites, but get_option is still just checking the options of the current site. You're going to need to use switch_to_blog() for this. Commented Dec 3, 2012 at 15:35
  • Edited my answer to reflect that suggestion. Commented Dec 3, 2012 at 15:37
  • I'm beginning to think that this is going to be our Waterloo! I used your amended code - and did some reading up on switch_to_blog() to boot - but to no avail! It just don't wanna cooperate! I'm starting to think it's got us beaten! Thanks again. You have given me food for thought though.
    – Yelper
    Commented Dec 3, 2012 at 16:33
  • see also get_blog_option()
    – Michal Mau
    Commented Dec 5, 2012 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.