Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a small issue with one of my queries here. I cannot increase my wordpress memory limit as long as hostgator sets it to 64mb. I'm not sure if its possible to reduce the amount of memory used making some changes to my code, here is the code I'm using to query users from a specific role and displaying their name, profile and avatar:

<?php $users = get_users('role=s2member_level3'); ?> 
<?php foreach ($users as $user) {
    $avatar = get_avatar($user->ID, '96');
    if (get_the_author_meta('description', $user->ID) == "" && stristr($avatar,"gravatar.com/avatar")) { continue; }
    ?>
    <div class="colaborador">
        <div class="imagem-colaborador">
            <?php if ($avatar == "") { 
                echo '<img src="http://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=96" alt="Avatar" />'; 
            } else { 
                echo $avatar;
            } ?> 
        </div>

        <div class="texto-colaborador">
            <h2 class="nome-colaborador"><?php echo $user->display_name; ?></h2>
            <p><?php the_author_meta('description', $user->ID);  ?></p>
        </div>                          
    </div>
<?php } ?>
share|improve this question
    
Not that I'm a proponent of direct DB access, but if you're running into extreme memory situations, would that be a solution for you? get_users() will cache each "gotten" user in RAM; if you have a buttload of users, this will eat a ton of RAM. You might bypass this with a direct DB query. –  Tom Auger Aug 30 '11 at 19:22
add comment

2 Answers

up vote 3 down vote accepted

In your example, you are getting all the fields in the get_users call, but you are only really using the ID and display_name fields. So you can save some memory by forcing get_users to only get the fields you need.

$users = get_users(array(
  'role'=>'s2member_level3', 
  'fields'=>array('ID', 'display_name'),
));

That will help reduce your memory footprint.

share|improve this answer
    
Hey Otto tried your suggestion but the problem persists :/ –  Vitor Argos Aug 29 '11 at 22:26
    
You could also save some memory by getting the user_email field as well, and passing that directly into the get_avatar function instead of the user ID. –  Otto Aug 30 '11 at 12:35
    
Furthermore, your check for $avatar being blank is useless, since get_avatar will always return a valid avatar URL. So your call to stristr is also pointless and consumes memory for no reason. –  Otto Aug 30 '11 at 12:36
    
Hey Otto, just figured it out and removed this line hehe, but the memory problem still there. I also, tried to use pagination but with no success. I`m thinking about changing the default avatar adress, so it will load an image located on my localhost, do you think it will make any difference? –  Vitor Argos Aug 30 '11 at 12:41
    
By the way, is there any way that I can check what is actually using the memory on this page? –  Vitor Argos Aug 30 '11 at 12:42
add comment

Call author data

You could change your multiple get_the_author_meta() calls against a single $author_data = get_userdata( $user_id ); call. Then simply extract everything from the object like $author_data->ID;. This should save you some memory.

Avatar

If you leave out the avatar you will also save a lot of mem. Avatars take a long time to load and use a whole load of memory.

share|improve this answer
    
What about avatar-related functions uses a lot of server memory?.. –  Rarst Aug 29 '11 at 21:16
    
I just took a look at how the avatar uses memory (via Chrome dev bar) yesterday. I don't really understand why, but it seemed that there's something about it. –  kaiser Aug 29 '11 at 21:19
    
actually the avatar is the main thing about this page, unfortunatly. But i'll try your suggestion Kaiser, not really sure if it will work though, but lets give it a try. Thank you! –  Vitor Argos Aug 29 '11 at 21:26
    
I do not follow. How does Chrome show PHP memory usage? From quick test get_avatar() call increases memory usage by 88 bytes. –  Rarst Aug 29 '11 at 21:27
1  
Avatars don't use any real memory on the server. That's just in the browser. Don't worry about that. –  Otto Aug 29 '11 at 22:05
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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