How to prevent users from opening my themes directory or at least preventing wordpress from outputting an error.

For example when I open this link in my wordpress blog https://www.example.com/wp-content/themes/themename/

I have this error Fatal error: Call to undefined function get_header() in ..

How can I prevent this from happening ?


You just want to add a check to see if a constant has been defined. If it hasn't you'll know the file is being accessed directly.

if ( ! defined ( 'ABSPATH') ) die ( 'No soup for you!' );

Add this to every file you don't want to be directly accessed.

Also, anything you add (like this) will be erased when a theme is updated -- unless you built your own theme and have control over that.

  • Another version would be: defined ( 'ABSPATH' ) or die ( 'No soup for you!' );
    – birgire
    Dec 28 '15 at 1:41
  • True, that is shorter, but for this example I think the first version is better for someone less experienced.
    – jgraup
    Dec 28 '15 at 1:44
  • ... and we should note that this insert would be lost for every theme update.
    – birgire
    Dec 28 '15 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.