WordPress Trac: Ticket #16749: Hooks alter/break array-formatted callbackshttps://core.trac.wordpress.org/ticket/16749
<p>
When hooking to static methods of a class (plugin, theme, etc), there are two possible formats:
</p>
<p>
<tt>array('class','method')</tt> <strong>or</strong> <tt>'class::method'</tt>
</p>
<p>
The prior works on PHP 4+, where the latter requires PHP 5.2.3+
</p>
<p>
The problem is that any time I try to use an array-formatted callback with a WordPress hook (for maximum compatibility), PHP throws an invalid callback error for wp-includes/plugin.php.
</p>
<p>
It turns out that the array ultimately being passed to <tt>call_user_func_array()</tt> always has an integer <tt>1</tt> appended to the array... thus making the callback invalid as <tt>call_user_func_array()</tt> is now being sent this:
</p>
<p>
<tt> array('class','function',1) </tt>
</p>
<p>
<strong>String-formatted callbacks work perfectly, but array-formatted callbacks are altered and therefore broken.</strong> This can be fixed by adding the following check to four different functions in plugin.php (<tt>apply_filters()</tt>, <tt>apply_filters_ref_array()</tt>, <tt>do_action()</tt>, and <tt>do_action_ref_array()</tt>), but I'm not sure this is the "best" solution.
</p>
<p>
Here's the "corrective" check:
</p>
<pre class="wiki">if(is_array($the_['function']) &amp;&amp; isset($the_['function'][2])){
unset($the_['function'][2]);
}
</pre>en-usWordPress Trachttps://core.trac.wordpress.org/chrome/site/your_project_logo.pnghttps://core.trac.wordpress.org/ticket/16749
Trac 1.0.1scribuFri, 04 Mar 2011 18:38:21 GMTkeywords sethttps://core.trac.wordpress.org/ticket/16749#comment:1
https://core.trac.wordpress.org/ticket/16749#comment:1
<ul>
<li><strong>keywords</strong>
<em>reporter-feedback</em> added
</li>
</ul>
<p>
I use array callbacks all the time without problems.
</p>
<p>
Are you sure there's not something else going on?
</p>
TicketscribuFri, 04 Mar 2011 19:01:18 GMThttps://core.trac.wordpress.org/ticket/16749#comment:2
https://core.trac.wordpress.org/ticket/16749#comment:2
<p>
Maybe you're adding the priority in the array by mistake:
</p>
<p>
<tt>add_action( 'foo', array( 'class', 'method, 1 ) );</tt>
</p>
TicketwestiFri, 04 Mar 2011 19:34:50 GMTstatus, severity changed; resolution set; keywords, milestone deletedhttps://core.trac.wordpress.org/ticket/16749#comment:3
https://core.trac.wordpress.org/ticket/16749#comment:3
<ul>
<li><strong>keywords</strong>
<em>reporter-feedback</em> removed
</li>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>worksforme</em>
</li>
<li><strong>severity</strong>
changed from <em>major</em> to <em>normal</em>
</li>
<li><strong>milestone</strong>
<em>Awaiting Review</em> deleted
</li>
</ul>
<p>
Replying to <a class="closed" href="https://core.trac.wordpress.org/ticket/16749#comment:2" title="Comment 2 for Ticket #16749">scribu</a>:
</p>
<blockquote class="citation">
<p>
Maybe you're adding the priority in the array by mistake:
</p>
<p>
<tt>add_action( 'foo', array( 'class', 'method, 1 ) );</tt>
</p>
</blockquote>
<p>
That exactly how he shows it in the ticket description which is incorrect.
</p>
TicketVeraxusFri, 04 Mar 2011 21:18:12 GMTstatus changed; resolution deletedhttps://core.trac.wordpress.org/ticket/16749#comment:4
https://core.trac.wordpress.org/ticket/16749#comment:4
<ul>
<li><strong>status</strong>
changed from <em>closed</em> to <em>reopened</em>
</li>
<li><strong>resolution</strong>
<em>worksforme</em> deleted
</li>
</ul>
<p>
No, I've confirmed the callback is being entered correctly.
</p>
<p>
For instance, here's one of the problem hooks:
</p>
<p>
<tt>add_action('admin_init', array('CTXPS_App','admin_init'));</tt>
</p>
<p>
Now if I write it like this, it works with no issues, Warnings, or Fatal Errors (but requires PHP 5.2.3)...
</p>
<p>
<tt>add_action('admin_init', 'CTXPS_App::admin_init');</tt>
</p>
<p>
Written as an array, I get this...
</p>
<p>
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, <em> was given in /var/www/vhosts/contextureintl.com/subdomains/plugintest/httpdocs/wp-includes/plugin.php on line 488
</em></p>
<p>
If I <tt>print_r($the_['function'])</tt> everywhere it appears in plugin.php, I see that the array now looks like this (which is not how it's written in the hook)...
</p>
<p>
<tt>array('CTXPS_App','admin_init',1)</tt>
</p>
<p>
So far I just haven't been able to figure out where that 1 is coming from.
</p>
TicketVeraxusFri, 04 Mar 2011 21:44:32 GMTstatus changed; resolution sethttps://core.trac.wordpress.org/ticket/16749#comment:5
https://core.trac.wordpress.org/ticket/16749#comment:5
<ul>
<li><strong>status</strong>
changed from <em>reopened</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>worksforme</em>
</li>
</ul>
<p>
I loaded a fresh install of WordPress on a different server and there's no issues. There's definitely something wrong with the server, not WordPress.
</p>
<p>
I'll close this out unless I come up with something concrete to workaround.
</p>
Ticket