When I’m writing plugins for WordPress, there are a lot of ‘models’ I follow to help speed up the process and one of my favorites is using snippets in Sublime Text. In WordPress, when building plugins, it’s common to use the following code at the beginning of PHP files you do not want to be accessed directly:

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

Some people add a comment like // Silence is golden to this to be witty.

What does this do?

Well, since PHP files can be directly accessed from the web browser, anything you don’t want to be loaded individually (without the rest of your plugin) needs a way to prevent this. What this does is looks for the constant ABSPATH, which is defined during the WordPress load process. The wp-load.php file to be specific. This means that unless WordPress has loaded, our file cannot be accessed directly from the browser.

I’m kind of forgetful

Well, I often forget to add this to the beginning of new files added to my projects for the sheer fact that I’m in the mindset of getting something written down quickly…so, I’ve turned it into a snippet to make it easy and quick to add it, without losing a thought.

The following is a Sublime Text Snippet that you can save it as Packages/User/silence-is-golden.sublime-snippet in the location that Sublime Text stores packages, that then lets you simply start typing silence, and hit enter to enter this quick if statement.

<snippet>
	<content><![CDATA[
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
]]></content>
	<tabTrigger>silence</tabTrigger>
	<scope>source.php</scope>
</snippet>

The workflow looks something like this:
Screen Shot 2015-07-22 at 2.13.24 PM
Hit ‘Enter’
Screen Shot 2015-07-22 at 2.14.47 PM

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

16 Comments

  1. I am genuinely happy to glance at this webpage posts which carries plenty of useful information, thanks for
    providing these data.

    Reply

  2. I was suggested this web site by my cousin. I am not sure whether this post is written by him as
    no one else know such detailed about my trouble.
    You’re incredible! Thanks!

    Reply

  3. buy viagra boca or viagra overnight – viagra coupon

    Reply

  4. generic for amoxicillin generic amoxil – buy amoxicillin 500mg usa

    Reply

  5. buy prednisone nz online prednisone – how much is prednisone 10mg

    Reply

  6. purchase stromectol online cost of ivermectin lotion – ivermectin pills

    Reply

  7. stromectol uk buy ivermectin pills for humans – ivermectin brand

    Reply

  8. viagra without a doctor prescription reasons for ed – erection pills that work

    Reply

  9. cheap ed medication buy prescription drugs without doctor – best ed pill

    Reply

  10. generic plaquenil pills plaquenil – hydroxychloroquine 100 mg

    Reply

  11. ivermectin cost ivermectin tablets for humans for sale – cost of ivermectin medicine

    Reply

  12. Hello! Do you use Twitter? I’d like to follow you if that
    would be okay. I’m undoubtedly enjoying your blog and look forward to
    new posts.

    Reply

  13. clomiphene for sale clomid – buy clomid

    Reply

  14. Hey there just wanted to give you a quick heads up. The words in your
    post seem to be running off the screen in Ie.
    I’m not sure if this is a format issue or something to do with internet
    browser compatibility but I figured I’d post to let you know.
    The layout look great though! Hope you get the problem fixed
    soon. Cheers

    Reply

  15. I used to be able to find good information from your content.

    Reply

  16. Hi there I am so delighted I found your web site, I really found you by mistake, while I was searching
    on Google for something else, Regardless I am here
    now and would just like to say thank you for
    a marvelous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to go
    through it all at the moment but I have bookmarked it and also
    included your RSS feeds, so when I have time I will be back to read much more,
    Please do keep up the excellent b.

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.