Strict Standards: Only variables should be passed by reference

Strict Standards: Only variables should be passed by reference

I recently came across an interesting warning on one of my WordPress sites. The warning was “Strict Standards: Only variables should be passed by reference” and was referring to line 14. As you can see below, I wasnt’ really sure what the issue was here.

if ($post->post_parent != 0) {
 // On child page
 $postID = array_pop( get_post_ancestors( $post ) ); // Seagyn's Code - get highest level
 $parentTitle = get_the_title($postID);
 $parentTitle = strtolower(str_replace('.', '', $parentTitle));
 $parentTitle = strtolower(str_replace(' ', '-', $parentTitle));
}

After a big of research, I found that array_pop requires an array in a variable as the functions is initiated with a reference variable which means the function will return the last item in the array and update the passed variable without the last item. PHP.net have a great example that explains this further.

To fix my issue, I had to update line 14 as follows:

if ($post->post_parent != 0) {
 // On child page
 $ancestors = get_post_ancestors( $post );
 $postID = array_pop( $ancestors );// Seagyn's Code - get highest level
 $parentTitle = get_the_title( $postID );
 $parentTitle = strtolower(str_replace('.', '', $parentTitle));
 $parentTitle = strtolower(str_replace(' ', '-', $parentTitle));
}

Hope this helps you!

Share

Hire WordPress exportsLoading comments...

Leave a comment

Your email address will not be published. Required fields are marked *