Define a constant, then retrieve it from a different scope

If you define a constant in WordPress on file_1.php

define( ‘SOMETHING’, location );

Read More

how can I use this in file_2.php?

If I define it again it sends a “Constant already defined” error. But if I don’t define it, it doesn’t know what it is. If it detects that it already exists, there must be a way to access it.
Is there a way I can access it from file_2.php?

SOMETHING = a specific URL, and I’m trying to use it in file_2,php to load a script,, “wp_register_script(‘script’, SOMETHING . ‘/filename.css’)”

If I don’t define SOMETHING again and try to use it in “wp_register_script” it says “Use of undefined constant SOMETHING – assumed ‘SOMETHING’”

Thanks

Related posts

Leave a Reply

1 comment

  1. You’d need to place the Define() in a file that every script will come across in WordPress (i.e. a file that’s always included, wp-config.php for site-wide definitions).

    If you were to define a constant in one file, it doesn’t necessarily mean that the other pages automatically knows it exists, until it’s ‘included‘.

    At the top of functions.php to make sure it’s always going to use SOMETHING, you can see if it exists first:

    if ( !defined( 'SOMETHING' )) {
        define( 'SOMETHING', 'location/here/folder' );
    }
    

    For Testing purposes, before trying to use register_script – save your expected string to a variable.

    <?php
        $ScriptLocation = SOEMTHING . '/filename.css';
        echo $ScriptLocation; 
    ?>