Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Last active August 23, 2024 15:22
Show Gist options
  • Save emre-edu-tech/f9687ec1d7e121122680fc9603629147 to your computer and use it in GitHub Desktop.
Save emre-edu-tech/f9687ec1d7e121122680fc9603629147 to your computer and use it in GitHub Desktop.
Simple Wordpress Plugin and Theme security measures using ABSPATH constant and built-in add_action() function.
<?php
/*
* ABSPATH is a constant in WordPress that stores the absolute path to the WordPress installation directory on the server.
* It is defined in the core wp-config.php file like this:
*/
define('ABSPATH', dirname(__FILE__) . '/');
/*
* PURPOSE OF THE ABSPATH CHECK
* This check below guarantees that no one from outside of the WordPress installation should access this file directly.
* If this constant is defined, then we are inside of WordPress and it can be counted as we are in a safe position.
* Note: The check for the ABSPATH if it is defined or not, is placed at the top of the PHP files in WordPress plugins or themes
* like this:
*/
if(!defined('ABSPATH')) {
die('This file should not be accessed outside of WordPress!');
}
/*
* Another check can be done using WordPress-specific function called 'add_action()', if this function does not exist, then
* Wordpress was not initialized.
*/
if(!function_exists('add_action') {
die('This file should not be accessed outside of WordPress!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment