Click to See Complete Forum and Search --> : Call to a member function on a non-object


maxaud
March 28th, 2008, 04:36 PM
I'm working wordpress trying to make a sidebar widget.
The following code works fine if entered directly into the sidebar template file.
If I make a seperate plugin/widget so it can be easily moved around within the sidebar from the wordpress CP I get the following error:
Fatal error: Call to a member function on a non-object

My code is as follows:

<h2 class="widgettitle">Contributors:</h2>
<ul>
<?php
$order = 'user_nicename';
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
foreach($user_ids as $user_id) : // start authors' profile "loop"
$user = get_userdata($user_id);
?>



<li>
<a href="/home/authors/<?php echo $user->user_nicename; ?>" alt="<?php echo $user->nickname; ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/authors/<?php echo $user->last_name; ?>.jpg" alt="<?php echo $user->nickname; ?>" border="1" /></a>

<?php echo '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?><br />
<?php echo '<a href="' . $user->user_url . '">View Website</a>'; ?><br />
<a href="/home/authors/<?php echo $user->user_nicename; ?>" alt="<?php echo $user->nickname; ?>">Profile</a>

</li>




<?php endforeach;
// end of authors' profile 'loop'
?>
</ul>

Any help would be greatly appreciated.

Also, how could I change the sort order to be random?
Thanks!



The following code is my widget.
It stated that the error was on line 26.


<?php
/*
Plugin Name: Contributors Widget
Description: A widget that ads the Contributors to the sidebar. Works with Author-Image to display the users uploaded avatar.
Author: Dustin Dempsey
Version: 1
Author URI: http://www.playforwarddesigns.com
*/

function widget_contributor_init() {
if (!function_exists("register_sidebar_widget")) {
return;
}

function widget_contributor($args) {
extract($args);
?>
<?php echo $before_widget; ?>
<?php echo $before_title
. 'Contributors::'
. $after_title; ?>

<div class="bottombar"><h2 class="widgettitle">Contributors:</h2>
<ul>
<?php
$order = 'user_nicename';
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
foreach($user_ids as $user_id) : // start authors' profile "loop"
$user = get_userdata($user_id);
?>



<li>
<table>
<tr>
<td>
<a href="/home/authors/<?php echo $user->user_nicename; ?>" alt="<?php echo $user->nickname; ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/authors/<?php echo $user->last_name; ?>.jpg" alt="<?php echo $user->nickname; ?>" border="1" /></a>
</td>
<td>
<?php echo '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?><br />
<?php echo '<a href="' . $user->user_url . '">View Website</a>'; ?><br />
<a href="/home/authors/<?php echo $user->user_nicename; ?>" alt="<?php echo $user->nickname; ?>">Profile</a>
</td>
</tr>
</table>
</li>




<?php endforeach;
// end of authors' profile 'loop'
?>
</ul>
</div>


<?php echo $after_widget; ?>
<?php
}
register_sidebar_widget('Contributors',
'widget_contributor');
}

add_action("plugins_loaded", "widget_contributor_init");

?>

PeejAvery
March 28th, 2008, 06:27 PM
Please don't use bold tags for posting code. Use [code] tags instead.

This means that one of your variables in that line is either not created or not an object. I would suggest checking the variables in that line with isset() and is_object().

maxaud
March 28th, 2008, 07:20 PM
Thanks for the reply.
My apologies for the bolded code.

How would I go about checking that?
I don't know much about php.

PeejAvery
March 28th, 2008, 10:28 PM
By using the two functions I already stated. They both have boolean returns. In other words, they either return true or false. Simply try all the variables from the erroring line with those. If any return false, they are your culprits.