Make WordPress Core

Opened 3 years ago

Closed 4 months ago

Last modified 4 months ago

#58987 closed enhancement (fixed)

Unify usage of die(), die( '-1' ), exit();

Reported by: Presskopp Owned by: westonruter
Priority: normal Milestone: 7.1
Component: General Version:
Severity: minor Keywords: needs-dev-note has-patch
Cc: Focuses: coding-standards

Description

like mentioned here I'd like to know if we want to unify the different die() calls we use:

// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
        die( '-1' );
}

(mostly used, having a comment)

if ( ! defined( 'ABSPATH' ) ) {
        die();
}

(not having a comment), like seen in \wp-admin\link-parse-opml.php, \wp-admin\site-health-info.php

or even

if ( ! defined( 'ABSPATH' ) ) {
        exit();
}

like seen in \wp-includes\rss-functions.php

Change History (5)

#1 @rajinsharwar
3 years ago

  • Focuses coding-standards added
  • Keywords needs-dev-note added
  • Severity normal → minor
  • Type defect (bug) → enhancement

Based on this theory, I guess we should consider using die() everywhere instead of exit of die(-1).

https://img001.prntscr.com/file/img001/bvohLKHlQJ2PhOwMlQDaeA.png

Also, I don't think it's a bug, could go as a code enhancement.

This ticket was mentioned in ​PR #12340 on ​WordPress/wordpress-develop by ​@masteradhoc.


5 months ago
#2

  • Keywords has-patch added

Unifies all "Don't load directly" file guards across src/ to use a consistent
canonical form as recommended by the WordPress Plugin Developer Handbook
(​https://developer.wordpress.org/plugins/plugin-basics/best-practices/#avoiding-direct-file-access):

// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
        exit;
}

Previously, files used three different patterns inconsistently:

  • die( '-1' ); (36 files) — the -1 has no meaningful effect in a direct file-access context where no JavaScript AJAX handler is present to interpret it
  • die(); (3 files, some also missing the comment or using "Do not load directly.")
  • exit() (1 file, missing the comment)

This patch normalises all 40 affected files to exit; (bare, without parentheses,
matching the documented best practice) and ensures the // Don't load directly.
comment is present and consistently worded in every case.

Not changed:

  • pluggable.php:1449 — die( '-1' ) inside a function body as an intentional AJAX response value
  • wp-includes/assets/icon-library-manifest.php — auto-generated from a Gutenberg package source; must be fixed upstream
  • Files where ABSPATH is *defined* (not guarded against), e.g. wp-load.php, load-scripts.php
  • All other die() / exit() calls that are genuine program logic, not file guards

Trac ticket: https://core.trac.wordpress.org/ticket/58987

## Use of AI Tools
AI assistance: Yes
Tool(s): GitHub Copilot
Model(s): Claude Sonnet 4.8
Used for: Identifying all affected files, generating the batch edits, and verifying no functional die()/exit() calls were touched. All changes were reviewed and approved by me.

#3 @masteradhoc
5 months ago

Hey @Presskopp & @rajinsharwar
i just prepared a PR for this ticket. Though i went for this version to standardize it:

// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
        exit;
}

Reason is that this suggestion is in the Best Practices and therefore also noted for plugin developers. Same goes for Woo! Here the two links:

I kept the comment in the way WP used it until now but would be open to adjust this as well in case you like!

Thank you for your feedback.

#4 @westonruter
4 months ago

  • Owner set to westonruter
  • Resolution → fixed
  • Status new → closed

In 62649:

General: Unify direct file-access guards to use exit.

Previously, files used three different patterns to block direct access: die( '-1' ), die(), and exit(). They also lacked the explanatory comment or had inconsistent wording. This normalizes all 40 affected files under src/ to the canonical exit; form recommended by the Plugin Developer Handbook. Each is preceded by a consistent // Don't load directly. comment.

The -1 return value carried no meaning in a direct file-access context, where no JavaScript AJAX handler exists to interpret it; it was only ever relevant inside admin-ajax.php responses, which are left untouched.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12340.
Follow-up to r11768, r59678, r59688.

Props masteradhoc, presskopp, rajinsharwar, mukeshpanchal27, westonruter.
Fixes #58987.

#5 @sabernhardt
4 months ago

  • Milestone Awaiting Review → 7.1
Note: See TracTickets for help on using tickets.