2

I've updated my plugin, Disable Blogging, to the latest version which is available in the WordPress repository.

Everything works as it should. However, one of my users has encountered an error when updating my plugin.

Fatal error: Can’t use function return value in write context in .../wp-content/plugins/disable-blogging/includes/functions-extra.php on line 74

They run this plugin on two other sites that they own and there is no issue. The only difference is the PHP version:

  • The one that has the error is a GoDaddy server and it might be PHP 5.4.45
  • The others are on Digital Ocean and PHP 5.6.25

Looking into my source code, here's the referring code on line 74 that's part of a function:

'meta' => array( 'class' => empty( get_avatar( get_current_user_id(), 28 ) ) ? '' : 'with-avatar', ),

But here is the full code to that function. This function simply removes the "Howdy" greeting in the admin bar.

public function admin_greeting( $wp_admin_bar ) {
    # Remove admin greeting in all languages
    if ( 0 != get_current_user_id() ) {
        $wp_admin_bar->add_menu( array(
            'id' => 'my-account',
            'parent' => 'top-secondary',
            'title' => wp_get_current_user()->display_name . get_avatar( get_current_user_id(), 28 ),
            'href' => get_edit_profile_url( get_current_user_id() ),
            'meta' => array( 'class' => empty( get_avatar( get_current_user_id(), 28 ) ) ? '' : 'with-avatar', ),
        ) );
    }
}

From my guess, this could be a compatibility issue with PHP 5.4? I've been developing the plugin on 5.6 and according to PHP, version 5.4 is no longer being supported.

If thats the case, I'd like to have confirmation on that. This way I can relay that back to the user and even add a function to check the PHP version of any WordPress site before it's activated.

  • 2
    I haven't tested it, but my guess is that it's related to using empty(). Here's a good answer to what sounds like a similar issue. I'd do away with empty() like this: array( 'class' => ( get_avatar( get_current_user_id(), 28 ) ) ? 'with-avatar' : '', ) (I flipped the logic around too for readability) – Dave Romsey Nov 26 '16 at 0:12
  • 1
    @DaveRomsey Thanks for simplifying the line for me. I switched to this and also added a return $wp_admin_bar at the end of the function which helped. – Ethan Jinks O'Sullivan Nov 26 '16 at 14:11
  • 1
    you should always be careful with supplying function return values to things that look like functions but are really language constructs, lots of gotchas in this area – Mark Kaplun Nov 26 '16 at 14:53
2

The cause of the inconsitent behavior on the hosts has its roots in the different PHP versions. If you check the PHP manual for the empty() language construct, you can see in the changelog:

5.5.0 empty() now supports expressions, rather than only variables.

So it is only since PHP 5.5 that it is possible to use expressions inside of empty()) as is done in your plugin. In this case the usage of empty() is not needed and the code should work simply without it:

'meta' => array( 'class' => get_avatar( get_current_user_id(), 28 ) ? 'with-avatar' : '', ),
2

As stated by Dave Romsey and JHoffmann, I've implemented a simplier logic without using empty():

'meta' => array( 'class' => empty( get_avatar( get_current_user_id(), 28 ) ) ? '' : 'with-avatar', ),

is now:

'meta' => array( 'class' => ( get_avatar( get_current_user_id(), 28 ) ) ? 'with-avatar' : '', ),

I also included a return at the end of the function:

return $wp_admin_bar;

Here's the complete code:

public function admin_greeting( $wp_admin_bar ) {
    # Remove admin greeting in all languages
    if ( 0 != get_current_user_id() ) {
        $wp_admin_bar->add_menu( array(
            'id' => 'my-account',
            'parent' => 'top-secondary',
            'title' => wp_get_current_user()->display_name . get_avatar( get_current_user_id(), 28 ),
            'href' => get_edit_profile_url( get_current_user_id() ),
            'meta' => array( 'class' => ( get_avatar( get_current_user_id(), 28 ) ) ? 'with-avatar' : '', ),
        ) );
        return $wp_admin_bar;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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