Today I Learned - Rocky Kev

TIL about WPINC and ABSPATH

POSTED ON:

TAGS:

I've been seeing this a lot in WordPress plugins.
Sometimes it's WPINC, and sometimes it's ABSPATH.

Only after 5 years have I decided to figure out what's happening under the hood.

<?php
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}

if ( ! defined( 'WPINC' ) ) die; and if ( ! defined( 'ABSPATH' ) ) exit; add an extra layer of security by preventing any direct access to your plugin file. ABSPATH is a PHP constant defined by WordPress in its core.

If your plugin file is accessed from outside of WordPress, the constant ABSPATH or WPINC will not be defined, so it exits the plugin code, preventing any unauthorized access to your code.

ABSPATH and WPINC are defined in WordPress core as:

define( 'ABSPATH', dirname(dirname(FILE)) . '/' );
define( 'WPINC', 'wp-includes' );
Both are used for same purpose.

Via https://wordpress.stackexchange.com/a/196544/132722

And from another comment:

Given these are used for security, I'd go for ABSPATH. Rationale being that ABSPATH is not only defined first in the WP bootstrapper, WPINC is defined under a conditional in the same file and, as a result, is more likely to experience a future regression.

Via https://wordpress.stackexchange.com/a/269716/132722

REF:
https://wordpress.stackexchange.com/questions/108418/what-are-the-differences-between-wpinc-and-abspath


Related TILs

Tagged:

TIL Static Blocks vs Dynamic Blocks

A static block is a piece of content whose markup is known when the page is saved. The block saves its content and markup directly in the post content. A dynamic block is a piece of content whose markup and exact content are not known when the page is saved.

TIL how to convert a shortcode to a WP block

Traditionally, shortcodes were a way that plugin developers could provide users the ability to add specific plugin functionality anwhere on their site. But shortcodes are not very user friendly, nor was hunting down the relevant data you needed to render the correct data for the shortcode. Converting existing shortcodes to blocks provides a much greater user experience in all aspects.

TIL how WordPress does serverside

This isn't fully accurate, but for the means of describing server-side rendering, it's a good start!