I am getting the following warnings:
Warning: array_keys() expects parameter 1 to be array, object given in /home/inin3486/public_html/wordpress/wp-content/themes/classiera/inc/colors.php on line 7
And
Warning: max(): When only one parameter is given, it must be an array in /home/inin3486/public_html/wordpress/wp-content/themes/classiera/inc/colors.php on line 7
This is what we currently have showing in CPanel:
<?php
function classiera_wpcss_loaded() {
// Return the lowest priority number from all the functions that hook into wp_head
global $wp_filter;
$lowest_priority = max(array_keys($wp_filter['wp_head']));
add_action('wp_head', 'classiera_wpcss_head', $lowest_priority);
$arr = $wp_filter['wp_head'];
}
add_action('wp_head', "classiera_wpcss_loaded");
How can I resolve this?
Simply change the code from line 1 to 14:
You are getting the warning because
$wp_filter['wp_head']
is an object, i.e. an instance ofWP_Hook
. The PHP constructarray_keys
needs the variable to be an array data type.The array of callbacks is located in the public property
callbacks
, which you can access like this:Problems with your code
Beyond accessing the public property, your code has 2 fundamental problems:
classiera_wpcss_head
, will not work (more in a moment to explain).max
will get you the highest priority number and not the lowest.New Callback Won't Work - It's too Late in the Request Cycle
The event
wp_head
has already sorted all of the registered callbacks and is in the process of calling each of them. Your callback is at a priority of 10. The lowest one has already been processed and will not run.What do I mean by that?
The above code will not run. Why? Timing. The function that adds this callback is being called at priority 10. It's too late to add another callback to this event, i.e. to
What can you do instead?wp_head
.I assume you want
classiera_wpcss_head
to run first. Right? That's why you are looking for the lowest priority number. Instead, just register your callback with a priority of 0 to make sure it's the first (or one of the first) ones.Disclaimer: You cannot guarantee your callback will run before any others that are registered to priority 0. Why? All callbacks are adding to their priority based upon when the
add_action
runs. In other words, if anotheradd_action( 'wp_head', 'some_callback', 0 );
runs before yours, yours will occur after that one.max
gets you the highest priority numberThe PHP construct
max
is getting you the highest priority number and not the lowest. Instead, you'd want to usemin
.However, you don't even have to do that as the first element in the array is the lowest priority number. Why?
When
do_action( 'wp_head' );
fires (runs), it sorts all of the callbacks lowest to highest by the priority number. Then it loops through each of them and calls them in order one-by-one.Let's fix your code
Let's fix the timing issue in your code.
If you want the callback to fire as early as possible, do this:
Put this code at the root of the file, i.e. not wrapped in the function
classiera_wpcss_loaded
.If you meant to put it as the last one (maximum priority number), then you can set it to say a
9999
.