Support » Developing with WordPress » Reliable way to Include files under the wp-admin directory

  • Resolved umchal

    (@umchal)


    Hi there,

    What would be the reliable way to include files under the wp-admin directory?

    Normally, something like this would do the job.

    
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    

    However, some sites have unusual ABSPATH value like wordpress.com (more info).

    I thought of doing

    
    require_once( dirname( WP_CONTENT_DIR ) . '/wp-admin/includes/class-wp-list-table.php' );
    

    but there might be sites with a custom WP_CONTENT_DIR path then it might not work.

Viewing 13 replies - 1 through 13 (of 13 total)
  • What would be the reliable way to include files under the wp-admin

    There isn’t one.

    Why are you trying to include files under the WordPress core?

    Thread Starter umchal

    (@umchal)

    Hello,

    Why are you trying to include files under the WordPress core?

    If you want to use certain functions and classes such as dbDelta() and WP_List_Table defined in the files under the wp-admin directory in your plugin, you need to include them first. See here for example.

    I’m sorry – I read your issue incorrectly.
    I thought you wanted to add new files to the /wp-admin folder.

    > but there might be sites with a custom WP_CONTENT_DIR path then it might not work.

    At first guess, I would say that the WP_CONTENT_DIR is being stored as a CONSTANT, so would render the right path whether it is custom or not.

    Thread Starter umchal

    (@umchal)

    Hello,

    At first guess, I would say that the WP_CONTENT_DIR is being stored as a CONSTANT, so would render the right path whether it is custom or not.

    It doesn’t mean dirname( WP_CONTENT_DIR ) . '/wp-admin' always gives the right path among sites with different path configurations.

    If WP_CONTENT_DIR is being stored as a constant, then the dir path should be fine.
    There are plugins / people etc, that choose to rename their /wp-admin directory tho.

    Is that what you mean?

    Thread Starter umchal

    (@umchal)

    Hello,

    If WP_CONTENT_DIR is being stored as a constant, then the dir path should be fine.

    We are not taking about WP_CONTENT_DIR itself but the wp-admin path. In many cases, dirname( WP_CONTENT_DIR ) . '/wp-admin' would give the correct path for the wp-admin directory. However, sites with a custom path set to WP_CONTENT_DIR will have a problem with the code like

    
    require_once( dirname( WP_CONTENT_DIR ) . '/wp-admin/includes/class-wp-list-table.php' );
    

    There are plugins / people etc, that choose to rename their /wp-admin directory tho.

    Is that what you mean?

    No. I’m not talking about cases that users manually edit core directory names.

    Thread Starter umchal

    (@umchal)

    Hello,

    Other options?
    https://developer.wordpress.org/reference/functions/admin_url/

    You shouldn’t use a url to include a PHP file. We need a PATH to the wp-admin directory.

    Here’s a good thread:
    https://wordpress.stackexchange.com/questions/119064/what-should-i-use-instead-of-wp-content-dir-and-wp-plugin-dir

    It doesn’t mention the wp-admin path.

    Moderator bcworkz

    (@bcworkz)

    WP_CONTENT_DIR is unrelated to /wp-admin/. That’s a dead end, forget it.

    What wordpress.com does or doesn’t do shouldn’t be a factor on a .org self-hosted site. In any case, even .com implies you can use ABSPATH for core files:

    You may not use ABSPATH on our platform, as this points to the core WordPress folder, unless you are requiring a core file.

    (my emphasis)

    In the case of list table classes, if your plugin page needing it has been added to the admin area, the classes will be available by default, no need to require the files.

    For dbDelta and similar, ABSPATH . 'wp-admin/includes/filename.php' should be fine since /wp-admin/ is supposed to not be moved or renamed. And if ABSPATH has been hacked and abused such that it no longer points to the WP installation folder, who ever did that is doing it wrong and you shouldn’t be obligated to accommodate them. There is no full /wp-admin/ path function. If there was, it’d be based on ABSPATH as well.

    Thread Starter umchal

    (@umchal)

    For dbDelta and similar, ABSPATH . ‘wp-admin/includes/filename.php’ should be fine since /wp-admin/ is supposed to not be moved or renamed.

    Are you certain that it works on wordpress.com as well? I’m not familiar with their platform. Just in case you or somebody else might know it.

    Moderator bcworkz

    (@bcworkz)

    You could ask through .com’s support. I’m not 100% sure, but the excerpt I quoted previously strongly implies it’s OK if you are accessing core resources. If someone knows for certain speak up!

    Thread Starter umchal

    (@umchal)

    I had somebody to check on wordpress.com and

    
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    

    works as their path constants are

    • ABSPATH: /wordpress/core/5.8/
    • WP_CONTENT_DIR: /srv/htdocs/wp-content
    Moderator bcworkz

    (@bcworkz)

    Cool! Thanks for reporting back.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Reliable way to Include files under the wp-admin directory’ is closed to new replies.