Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get_option() inside wp-config.php

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

( ! ) 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' );

>Solution :

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 😀 i suggest you to just write it hard code like you did with WP_SITEURL and WP_HOME

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading