Skip to content

undefined function add_filter() and apply_filters() #1310

Closed
javiertury opened this Issue Aug 3, 2014 · 5 comments

4 participants

@javiertury

After configuring automatic updates following http://codex.wordpress.org/Configuring_Automatic_Background_Updates using wp-cli gives the following error:
PHP Fatal error: Call to undefined function add_filter() in phar:///usr/local/bin/wp/php/wp-cli.php(23) : eval()'d code on line 50 The same happens for apply_filters() function

My configuration is working properly with respect to wordpress itself, and yes, I've put the configuration after this line:
require_once( ABSPATH . 'wp-settings.php' );

Has wp-cli problems with the previous line? Have I misconfigured anything?

This is my custom config of wp-config.php

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

define( 'FORCE_SSL_LOGIN', true );
define( 'FORCE_SSL_ADMIN', true );
define( 'WP_AUTO_UPDATE_CORE', true );
add_filter( 'allow_major_auto_core_updates', '__return_true' );
add_filter( 'automatic_updates_is_vcs_checkout', '__return_false', 1 );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
apply_filters( 'auto_core_update_send_email', false, success, $core_update, $result );

Edit: the error occurs with most commands such as wp plugin list but not with others e.g. wp --version. The version I'm using is WP-CLI 0.16.0

@danielbachhuber
wp-cli member

Er, require_once(ABSPATH . 'wp-settings.php'); is what actually loads WordPress. I'd imagine the constants and filters you've defined aren't actually applying on your site, as WordPress is loading completely before they're applied.

WP-CLI is a bit different because it reads the contents of your wp-config.php file, strips out the require_once(ABSPATH . 'wp-settings.php'); so WordPress doesn't load when it evaluates the contents, and has its own load process. Because add_filter() and apply_filters() aren't defined until WordPress is loaded, WP-CLI fatals.

You'll want to put the constants above the /* That's all, stop editing! Happy blogging. */ line, and your filters in an mu plugin. I'm not sure why you're calling apply_filters() too — that probably should be add_filter()

@eimajenthat

I saw this issue mentioned in the docs here:

http://codex.wordpress.org/Configuring_Automatic_Background_Updates#Conflict_with_wp-cli

I'm not using wp-cli, so I don't know if this will work, but couldn't you just wrap the filters in a conditional block? Like this:

    <?php
    if(php_sapi_name() == 'cli' || empty($_SERVER['REMOTE_ADDR'])) {
        define( 'FORCE_SSL_LOGIN', true );
        define( 'FORCE_SSL_ADMIN', true );
        define( 'WP_AUTO_UPDATE_CORE', true );
        add_filter( 'allow_major_auto_core_updates', '__return_true' );
        add_filter( 'automatic_updates_is_vcs_checkout', '__return_false', 1 );
        add_filter( 'auto_update_plugin', '__return_true' );
        add_filter( 'auto_update_theme', '__return_true' );
        apply_filters( 'auto_core_update_send_email', false, success, $core_update, $result );
    }

Sure, it's not the most elegant solution, but it's better than saying "sorry, you'll have to use a plugin".

Alternatively, when you have wp-cli strip out the require call, you could have it define dummy functions that don't do anything, so you don't get an error. Sure, those settings aren't set, but I don't think that matters in this scenario. I doubt wp-cli cares whether the auto-update features are enabled.

@danielbachhuber
wp-cli member

but couldn't you just wrap the filters in a conditional block? Sure, it's not the most elegant solution, but it's better than saying "sorry, you'll have to use a plugin".

Regardless of what you're trying to do, you can't use filters in your wp-config.php file because the add_filter() and apply_filters() aren't defined. If you add block with filters after require_once(ABSPATH . 'wp-settings.php');, then they don't actually execute until the entirety of WordPress has executed. Your proposed solution doesn't work — this is why I suggested putting the filters in a MU plugin.

@scribu
wp-cli member
scribu commented Aug 25, 2014

I updated the Codex; adding stuff below require_once(ABSPATH . 'wp-settings.php'); was a terrible suggestion to begin with.

@danielbachhuber
wp-cli member

Created a new canonical issue for the wp-config.php problem #1631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.