wp-cli: Call to undefined function add_filter()

Hey! I’m attempting to automate updates of my WordPress sites/plugins/themes, and I’m getting an error just trying to run a simple wp core check-update command. I read the documentation and the best practices, and I’m reporting what may well be a support issue in case there’s some kind of bug. This is my first time attempting to use wp-cli, but I’ve also been working with WordPress for 8 or so years now, so I’m hopefully not wasting anyone’s time. Apologies if I’m way off-base.

I installed following the installation instructions, and made sure the executable is in the recommended location, /usr/local/bin/wp. Its permissions are 755. My first attempt at running a command was immediately greeted with a fatal error. I went over the common issues, and I have not made any of the mistakes in there. And to be extra clear, I have not removed require_once(ABSPATH . 'wp-settings.php'); from wp-config.php. My wp-config.php is one directory above my public directory (where WordPress lives), though I tried temporarily moving it into pub to see if the command would run. No dice, however. I also tried running the nightly, but got the same error. I reinstalled the stable version of wp-cli after that and tested again.

I’m running on Debian Jessie 8.6, totally stable, no Sid packages or anything weird. The box is a simple web server, and its packages are pretty standard (LAMP stack), and up to date as of this writing.

Here’s the --info:

$ wp --info
PHP binary:     /usr/bin/php5
PHP version:    5.6.27-0+deb8u1
php.ini used:   /etc/php5/cli/php.ini
WP-CLI root dir:        phar://wp-cli.phar
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 0.25.0

And it reports being on the latest version:

#wp cli update --allow-root
Success: WP-CLI is at the latest version.

Here’s the command I was attempting to run with the --debug flag attached.

$ wp core check-update --debug
Debug (bootstrap): No readable global config found (0.009s)
Debug (bootstrap): No project config found (0.01s)
Debug (bootstrap): No package autoload found to load. (0.151s)
Debug (bootstrap): ABSPATH defined: <domain-lives-here>/pub/ (0.151s)
Debug (bootstrap): Begin WordPress load (0.152s)
Debug (bootstrap): wp-config.php path: <domain-lives-here>/wp-config.php (0.152s)
PHP Fatal error:  Call to undefined function add_filter() in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(952) : eval()'d code on line 96

And here’s the request debug info:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.6 (jessie)
Release:        8.6
Codename:       jessie
$ uname -a
Linux nocturne 4.6.5-x86_64-linode71 #2 SMP Fri Jul 29 16:16:25 EDT 2016 x86_64 GNU/Linux
$ which -a php
/usr/bin/php
$ php -v
PHP 5.6.27-0+deb8u1 (cli) (built: Oct 15 2016 15:53:28)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
$ php -m | grep -i suhosin
$ grep '^$wp_version\s' wp-includes/version.php
$wp_version = '4.6.1';
$ which -a wp
/usr/local/bin/wp
$ stat $(which wp)
  File: ‘/usr/local/bin/wp’
  Size: 5123908         Blocks: 10032      IO Block: 4096   regular file
Device: 800h/2048d      Inode: 14289       Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (   50/   staff)
Access: 2016-11-16 04:12:15.000000000 -0700
Modify: 2016-11-16 04:12:15.000000000 -0700
Change: 2016-11-16 04:12:40.000000000 -0700
 Birth: -
$ wp package list
Error: Composer directory for packages couldn't be created.

Again, I tried to verify that this is not a support issue, but apologies if I’ve made a mistake. Thanks for any light you guys can shed on this.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Hi @GabLeRoux , Bitnami Engineer here.

As a workaround in the Bitnami stack, you can simply include the pingback’s configuration lines in the wp-config.php file inside a if block like this one:

if ( !defined( 'WP_CLI' ) ) {
// remove x-pingback HTTP header
...
}

We also noticed that we need to set the HTTP_HOST variable when using WP CLI and that can be fixed using this:

if ( defined( 'WP_CLI' ) ) {
     $_SERVER['HTTP_HOST'] = 'localhost';
}

I hope this information helps. Jota

Found this issue from a google search with following error message:

PHP Fatal error: Uncaught Error: Call to undefined function add_filter() in phar:///usr/local/bin/wp/php/WP_CLI/Runner.php(1070) : eval()'d code:110

Useful things from above comments:

wp-cli attempts to parse your wp-config.php and removes the code that starts WordPress. Attempting to call a WordPress function will result in a failure. Any modifications that cause this can be moved to an mu-plugin as a workaround.

Related documentation: wp-cli common issues: PHP Fatal error: Call to undefined function \ #

I installed fresh install of a wordpress bitnami installation on aws as their suggested installation method and the undefined function add_filter() error occurs because bitnami has a custom wp-config.php file where it adds the following lines at the end of the file:

//  Disable pingback.ping xmlrpc method to prevent Wordpress from participating in DDoS attacks
//  More info at: https://docs.bitnami.com/?page=apps&name=wordpress&section=how-to-re-enable-the-xml-rpc-pingback-feature

// remove x-pingback HTTP header
add_filter('wp_headers', function($headers) {
    unset($headers['X-Pingback']);
    return $headers;
});
// disable pingbacks
add_filter( 'xmlrpc_methods', function( $methods ) {
        unset( $methods['pingback.ping'] );
        return $methods;
});
add_filter( 'auto_update_translation', '__return_false' );

Error message is indeed obvious, we need to get rid of the add_filter functions from wp-config.php.

A simple workaround for this is to move these lines from wp-config.php to your theme’s functions.php instead.

Suggestion for a wp-cli fix

Instead of failing with the error, the script could also parse the document with a regex to find calls to add_filter since this seems quite common, strip them in a similar way to what it does with require_once(ABSPATH . 'wp-settings.php');, and write a warning with links to the wp-cli common issues: PHP Fatal error: Call to undefined function \ #.

Not a big deal anyway. Thanks for the awsome wp-cli ✌️

As Gabriel indicated, I just got rid of all the add_filter functions from WP-Config.php.

Thank you @GabLeRoux!