Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace use of get_stylesheet_directory() with __DIR__ #221

Open
nickcernis opened this issue Mar 26, 2019 · 4 comments
Open

Replace use of get_stylesheet_directory() with __DIR__ #221

nickcernis opened this issue Mar 26, 2019 · 4 comments

Comments

@nickcernis
Copy link
Collaborator

nickcernis commented Mar 26, 2019

@hellofromtonya suggests that we consider replacing use of get_stylesheet_directory() with __DIR__ (or dirname( __FILE__ ) for pre PHP 5.3 support) in Genesis Sample and StudioPress child themes:

Pros

  • It avoids multiple unnecessary function calls. (Using __DIR__ is only faster by the tiniest fraction of a second, but it is still faster.)
  • get_stylesheet_directory() apparently causes confusion for some developers new to WordPress core functions as to what path it's relative to. __DIR__ is more explicit and clearly defined as “the directory of the file”.

Cons

  • Both __DIR__ and get_stylesheet_directory() require some knowledge/reading of docs for those new to development — there may be some who find __DIR__ more cryptic than the WP function.
@hellofromtonya
Copy link

Thank you @nickcernis for opening this ticket. I'd like to add a few things to the discussion:

When loading files from where you are right now in the filesystem, __DIR__ is more intuitive and faster. It explicitly says: "Hey, load from this directory." There's no functions to call. It's a light and fast constant.

get_stylesheet_directory()means from the root of the currently active theme. But it's not intuitive unless you look it up or already know that it means the root of the current theme. It stops me in my tracks every time to think about if it means to load from the root of the child theme or from Genesis. It's a frequent question I'm asked too.

And then there's the performance side of calling get_stylesheet_directory() over and over and over again. To understand why, look at the function itself within Core and trace out the number of functions that are called:

/**
 * Retrieve stylesheet directory path for current theme.
 *
 * @since 1.5.0
 *
 * @return string Path to current theme directory.
 */
function get_stylesheet_directory() {
    $stylesheet     = get_stylesheet();
    $theme_root     = get_theme_root( $stylesheet );
    $stylesheet_dir = "$theme_root/$stylesheet";

    /**
     * Filters the stylesheet directory path for current theme.
     *
     * @since 1.5.0
     *
     * @param string $stylesheet_dir Absolute path to the current theme.
     * @param string $stylesheet     Directory name of the current theme.
     * @param string $theme_root     Absolute path to themes directory.
     */
    return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
}

Each time you invoke it, It calls multiple functions and fires a filter.

Performance

__DIR__ is faster. Yes, it's only a few microseconds faster. However, I'd advocate that each area we optimize is additive.

@jb510
Copy link

jb510 commented Mar 26, 2019

Mixed feelings. I've always felt "get_stylesheet_directory" was the "WordPress Way" and should always be used. Thats said, you're right, it makes little sense in a child theme unless your anticipating someone deleting a file from a child theme with the expectation that it'll revert to the parent.

Someone wiser than me should think about if/how this would affect Genesis 3.0 "as a plugin" though.

Finally, no fallback. 5.3 is dead. WP 5.2 is in beta and is declaring PHP 5.6 as minimum supported version. "Genesis Sample" certainly doesn't need to support pre 5.6 anymore.

@seothemes
Copy link
Contributor

+1 for using __DIR__ instead. Shorter, cleaner and faster. get_stylesheet_directory() should only be used when absolutely necessary.

@kauaicreative
Copy link

kauaicreative commented Oct 18, 2020

__DIR__ has the added bonus of path code completion in PHP Storm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants