Home > other >  get_option() inside wp-config.php
get_option() inside wp-config.php

Time:09-28

I'm trying to customize the wp-config.php file on a WordPress website, and stumbled upon this (old) article on digwp.com that recommends setting Cookie Domains in wp-config, which I tried it to check if I could see a performance boost.

The same method is described at https://developer.wordpress.org/apis/wp-config-php/#additional-defined-constants

define( 'COOKIEPATH', preg_replace( '|https?://[^/] |i', '', get_option( 'home' ) . '/' ) );
define( 'SITECOOKIEPATH', preg_replace( '|https?://[^/] |i', '', get_option( 'siteurl' ) . '/' ) );
define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . 'wp-admin' );
define( 'PLUGINS_COOKIE_PATH', preg_replace( '|https?://[^/] |i', '', WP_PLUGIN_URL ) );

However, the result is the following error:

( ! ) Fatal error: Uncaught Error: Call to undefined function get_option() in /Users/jrmy/Sites/secretsoflifeanddeath/local/app/public/wp-config.php on line 30
( ! ) Error: Call to undefined function get_option() in /Users/jrmy/Sites/secretsoflifeanddeath/local/app/public/wp-config.php on line 30

I searched for answers and was only able to find a comment by @Nathan Dawson on this question that made mention of this:

get_option() isn't going to work inside wp-config.php so you'll need to manually enter the URL or use another method of detecting it.

➡️ I'd like to understand why get_option() doesn't seem to work inside wp-config.php, even though it's recommended by WordPress, and what's the preferred alternative method?


NB:

  • I'm using WordPress 6.0.2
  • My wp-config.php file also contains the following, above the cookie domain declarations:
define( 'WP_SITEURL', 'https://domain.tld' );
define( 'WP_HOME', 'https://domain.tld' );

CodePudding user response:

get_option is declared in option.php which is loaded by wp-load.php (after some more inner includes). If you open wp-load.php around line 40-60 you will see that Wordpress includes wp-config.php and only after that it goes for wp-load.php that means that when you are inside wp-config.php you cannot use the get_option function.

The error is legit, he is right :D i suggest you to just write it hard code like you did with WP_SITEURL and WP_HOME

  • Related