PHP Variable

Im coming along nicely learnin how to mess with all the template php and stuff but I came across somthin I havent been able to find on the wordpress codex or elsewhere yet...

how do you do this: add somthing onto get_template_directory_uri(); for instance and make it a variable

I know to call it like this:

$dir = get_template_directory_uri();

I just am unsure how to put /dir onto the end of that and make it the variable


  • Jonathan
    • The Incredible Code Injector

    I would use Constants...

    define( 'THEME_DIR', get_template_directory() );
    define( 'THEME_URI', get_template_directory_uri() );

    Now that you have identified your constants (THEME_DIR, THEME_URI) you can use them
    THEME_URI . '/someplace/overhere/'

    or use them to create more constants like this...

    define( 'THEME_LIBRARY', THEME_DIR . '/library' );
    define( 'THEME_ADMIN', THEME_LIBRARY . '/admin' );
    define( 'THEME_CSS', THEME_URI . '/library/css' );
    define( 'THEME_JS', THEME_URI . '/library/js' );

    And for child themes...

    define( 'CHILD_THEME_DIR', get_stylesheet_directory() );
    define( 'CHILD_THEME_URI', get_stylesheet_directory_uri() );

    Anyways, hope the above examples demonstrate the how to implement and use :wink:

    Reading material: Constants:

  • LCDist
    • The Incredible Code Injector

    If I may ask 1 more question on that.... I was thinkin with the constant...would that make it site wide the rest of the time they are there or would it have to be loaded via functionsphp?

    Only reason I ask is this is for 1 page in particular that Im coding in the php... well Im hard coding a lot of my pages atm but lets not get into that... lol

    anyway would constants, constants in the functions.php, or variables be best then if your gonna 1 and done it... soon as that page loads I wouldnt need the values anymore. I just want that page to adapt if I change servers is all lol.

    This is what I tested and it worked.. since I already had the framework there, Ill play with constants now just was testing the theory out lol

    		$dir = get_template_directory_uri();
    		$lcmore = '<img src="' . $dir . '/img/more.png" alt="Help" />';
    		$lcyes = '<img src="' . $dir . '/img/yes.png" alt="Yes" title="Yes" />';
    		$lcno = '<img src="' . $dir . '/img/no.png" alt="No" title="No" />';

    Anyway which way do you think is best for this purpose... I had just though if it was in functions.php it would be a load on every user every time they loaded the site... this way I was hoping to minimize load

  • Jonathan
    • The Incredible Code Injector

    would that make it site wide the rest of the time they are there or would it have to be loaded via functionsphp

    I would add the constants to the functions.php file. That would mean it is site wide (as long as theme is activated) or create a plugin. It really depends on your setup.

    But by having it global makes life so much easier :wink:

    Following on from your code (there are other ways, but we all have our own way and I don't know what you're trying to do)

    $lcmore = '<img src="' . THEME_URI . '/img/more.png" alt="Help" />';
    $lcyes = '<img src="' . THEME_URI . '/img/yes.png" alt="Yes" title="Yes" />';
    $lcno = '<img src="' . THEME_URI . '/img/no.png" alt="No" title="No" />';

    I had just though if it was in functions.php it would be a load on every user every time they loaded the site... this way I was hoping to minimize load

    Lol :wink: You Seriously don't have to worry about that. But If you are worried about it, then constants win hands down in the long term as they are global, and make changes a breeze. You only have to change the Single Constant and not multiple pages :wink:
    define( 'THEME_IMG', THEME_URI . '/img' );
    and now your code
    $lcno = '<img src="' . THEME_IMG. '/no.png" alt="No" title="No" />';
    So you can change the img directory anytime without a hassle etc etc
    Your wp-config.php file is full of constants as an example of their usage.

  • DavidM
    • DEV MAN’s Mascot

    Awesome stuff again Jonathan, sending points your way even though you're already a lifer here!

    Generally, I just go with initial instincts and don't think further into matters, such as using constants, but you're totally right, constants are definitely preferred, especially in case these functions get deprecated in the future.


  • LCDist
    • The Incredible Code Injector

    Ya thank you... Its amazing the more I learn the more I realize how much I dont know.

    Out of curiosity

    define( 'THEME_URI', get_template_directory_uri() );
    define( 'THEME_DIR', get_template_directory() );
    define( 'THEME_IMG', THEME_URI . '/img' );
    $lcno = '<img src="' . THEME_IMG. '/no.png" alt="No" title="No" />';
    $lcyes = '<img src="' . THEME_IMG. '/yes.png" alt="No" title="Yes" />';
    $lcmore = '<img src="' . THEME_IMG. '/more.png" alt="No" />';

    works like a charm... basically Im using it like this: I have those pics A LOT of places on a page so I was doing <td class="col3"><?php echo $lcyes ?></td> stuff like that. that way if I move the img somewhere or change servers itll be MUCH easier to change one or 2 lines then 50.

    But in the middle of this I tried to make the variables $lcno and such into constants also then in my line tried <td class="col3"><?php echo LCNO ?></td> and it wouldnt work... if I did echo THEME_URI it echoed it but all I could get out of the full line was the pictures showing up as their respective alts...
    Is this a rule I should know about how you can use variables and constants?

    Also ... I woulda used template_dir but for some reason it wont work either I can get it to echo the url fine but its like it wont interpret it in the browser.... is this possibly because for now I, testing on my XAMP shell before I bring it live?

    I appologize for the 20 questions but I feel like I jumped into the deep end of a pool without learnin to paddle... so Im trying to learn as I go lol.

    My short term goal for now is to get this theme up and running the way I like it then in the long term to work on a copy of it adding the backend support in dashboard so I can give it away to you guys as well as my members... its just takin a lot of work to learn how to do both lol

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.