WordPress.org

Support

Support » How-To and Troubleshooting » change $wpdb to global scope for use in widget?

change $wpdb to global scope for use in widget?

  • Hi, here is the page:
    http://wp.sacrag.com/

    the area in question is the error in the second SELECT in the right column. The following PHP is in the HEAD:

    $authorsArchives = $wpdb->get_results("SELECT * FROM $wpdb->users");

    and then the following PHP is used in a King Text widget:

    <?php foreach ($authorsArchives as $authorArchiveLink): ?>
    <option value="<?php echo $authorArchiveLink->user_nicename; ?>"><?php echo $authorArchiveLink->display_name; ?></option>
    <?php endforeach; ?>

    and you see what that does on the above link. I’m guessing it has something to do with the scope of $wpdb but I don’t really understand how to fix that. Can anybody help?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • TechGnome

    @techgnome

    Moderator

    Actualy… it’s not the select that’s causing your error…. it’s the foreach code…. for what ever reason, it thinks that $authorsArchives is empty…. I’d suspect the scope of the $authorsArchives rather than $wpdb. But with out more source code, it’s hard to say.

    -tg

    the foreach loop works fine when i use it in the header.php file, but barfs when i try to use it in the widget. what other source code do you think would help you see what’s going on?

    at the start of the widget, you’ll need to add:

    global $authorArchiveLink;

    if you really need wpdb as well, then add

    global $wpdb;

    I don’t know why I was stuck on doing the variables in the header.php. It worked fine when i stuck all the PHP in the widget. Thanks so much!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘change $wpdb to global scope for use in widget?’ is closed to new replies.
Skip to toolbar