Skip to:
Content

BuddyPress.org

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#8005 closed defect (bug) (fixed)

PHP warning when getting BuddyPress page title part

Reported by: venutius's profile Venutius Owned by: espellcaste's profile espellcaste
Milestone: 5.0.0 Priority: normal
Severity: normal Version: 3.0.0
Component: Navigation Keywords: good-first-bug has-patch
Cc:

Description

I've been getting the following error in my logs:

reset() expects parameter 1 to be array, boolean given
Type: PHP Warning
Line: 3038
File: /wp-content/plugins/buddypress/bp-core/bp-core-template.php

Line 3038 has:

if ( ! empty( $bp->members->nav ) ) {
        $primary_nav_item = $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
        $primary_nav_item = reset( $primary_nav_item );
}

I had a play and one way of dealing with the issue could be:

if ( ! empty( $bp->members->nav ) ) {
        $primary_nav_item = $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
        $primary_nav_item = reset( $arr = array($primary_nav_item) );
                }

Attachments (1)

8005.diff (590 bytes) - added by espellcaste 6 years ago.

Download all attachments as: .zip

Change History (13)

#1 @Venutius
6 years ago

That fix was not good, now getting the following error:

[21-Nov-2018 21:00:40 UTC] PHP Notice: Only variables should be passed by reference in /wp-content/plugins/buddypress/bp-core/bp-core-template.php on line 3039.

I'm not able to replicate this as yet, It's a live site and this error just seems to come in randomly. The last time I got it, I had installed a new plugin BWS Error Log, disabled another WP Error Log and saved the new settings of the BWS plugin, it went to view the error log and the error appeared so I'm not sure it's me that's triggering it.

#2 @Venutius
6 years ago

My latest fix is to wrap a check to see if the variable is an array:

if ( ! empty( $bp->members->nav ) ) {
        $primary_nav_item = $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
        if ( is_array( $primary_nav_item ) ) {
                $primary_nav_item = reset( $primary_nav_item );
        }
}

#3 @DJPaul
6 years ago

  • Component changed from Core to Navigation
  • Keywords good-first-bug added
  • Milestone changed from Awaiting Review to Awaiting Contributions

get_primary looks like it can return boolean false. We just need to handle that case in the code you mentioned. Good find!

#4 @klawton
6 years ago

Hello, I've been getting the same error with my project so I added a check to make sure $primary_nav_item is not false which seems to be working for me. I replaced

$primary_nav_item = $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
$primary_nav_item = reset( $primary_nav_item );

with

$primary_nav_item = $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
$primary_nav_item = $primary_nav_item ? reset( $primary_nav_item ) : $primary_nav_item;

My first time contributing here so if there is a better place to put fixes i'll be happy to repost there.

#5 @espellcaste
6 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Contributions to 5.0.0
  • Owner set to espellcaste
  • Status changed from new to accepted

This is something we can do at the next milestone.

@espellcaste
6 years ago

#6 @espellcaste
6 years ago

  • Keywords has-patch added; needs-patch removed

Suggested patch added! The approach seems ok to me.

This ticket was mentioned in Slack in #buddypress by espellcaste. View the logs.


6 years ago

#8 @imath
6 years ago

Hi @espellcaste

I think I’d do something like this :

$primary_nav_item = (array) $bp->members->nav->get_primary( array( 'slug' => $component_id ), false );
$primary_nav_item = reset( $primary_nav_item );

#9 @espellcaste
6 years ago

  • Summary changed from Reset() expects parameter 1 to be and array, boolean given - buddypress/bp-core/bp-core-template.php 3038 to PHP warning when getting BuddyPress page title part

#10 @espellcaste
6 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 12332:

Casting the $primary_nav_item variable to an array when fetching the nav primary item to avoid PHP warning.

The get_primary method returns false when there is no nav primary item assigned. The false value returned triggered PHP warning when trying to get the first value of the array with reset.

Props imath, klawton

Fixes #8005

#11 @johnjamesjacoby
5 years ago

This debug warning has recently started clogging up some log files on BuddyPress.org.

I'm going to merge this change to plugins.svn.wordpress.org/buddypress/trunk to silence them.

#12 @Maniou
5 years ago

Hi @johnjamesjacoby
Is it planned to release this fix on master ?
Thank you :)

Note: See TracTickets for help on using tickets.