3

I want to add an additional php file, for example test.php in my wordpress theme, and access it with url like example.com/wordpress/test.php

When I place this test.php file in the theme directory, I can't access it. So I how can I do that? It allows to access only if i place the test.php in the wordpress root, but i want to add the file in the theme directory instead of the root directory, which is at

wordpress\wp-content\themes\mytheme\test.php

1 Answer 1

1

Avoid native php functions

  1. They are not relative, so breaking stuff is easy. (like in the OPs case)
  2. Why role your own if core got a function for you?
  3. They don't consider edge cases

You should NEVER use the constants

  • STYLESHEET_DIRECTORY
  • TEMPLATE_DIRECTORY
  • WP_PLUGIN_DIR
  • etc...

Remember: Those are not API! and can therefore change!

but INSTEAD USE the following API functions:

  • Parent Theme: get_template_directory() / get_template_directory_uri()
  • Child Theme: get_stylesheet_directory() / get_stylesheet_directory_uri()

You can use both in Plugins

  • plugin_dir_path(__FILE__).'/your/path/to/file.ext *) for Themes also
  • plugin_dir_url( __FILE__ ).'/your/path/to/file.ext

but plugin_dir_url() is reserved only for Plugins.

Keep in mind...

That there're

  • Plugins
  • MU-Plugins
  • Drop-Ins
  • Themes
  • Themes in directories registered via register_theme_directory( $dir );

So please stick with the native API functions like wp_upload_dir();, content_url();, etc.

In general: Take a look at /wp-includes/link-template.php for a whole bunch of nice path and URl related API functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Kaiser, working for me! Child Theme: <?php echo get_stylesheet_directory_uri().'/subfolder/file.php';?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.