[wp-trac] [WordPress Trac] #38650: Provide some guidance on what conditionals can/should be used in default-filters.php

WordPress Trac noreply at wordpress.org
Thu Aug 29 09:10:30 UTC 2024


#38650: Provide some guidance on what conditionals can/should be used in default-
filters.php
----------------------------+---------------------
 Reporter:  helen           |       Owner:  jorbin
     Type:  enhancement     |      Status:  closed
 Priority:  low             |   Milestone:  6.7
Component:  Bootstrap/Load  |     Version:
 Severity:  minor           |  Resolution:  fixed
 Keywords:  has-patch       |     Focuses:  docs
----------------------------+---------------------

Comment (by devahrefs):

 Guidance on Safe Usage of Functions in default-filters.php
 default-filters.php is a core WordPress file responsible for defining
 default filters and actions that are applied during the loading process.
 Because this file is loaded very early in the WordPress bootstrap process,
 it’s essential to be cautious about which functions and conditionals are
 used. The following guidelines explain what is generally considered safe
 and what should be avoided:

 What to Use:
 Functions in load.php: Functions defined in load.php are typically safe to
 use since they are part of the early initialization of WordPress. These
 functions do not depend on the broader WordPress environment that is
 initialized later in the process.

 Basic Conditional Checks: Simple PHP conditional checks that do not depend
 on the full WordPress environment (such as defined(), isset(), etc.) are
 generally safe to use.

 Constants: Usage of constants (like ABSPATH, WP_CONTENT_DIR, etc.) that
 are defined early in the loading process is safe.

 What to Avoid:
 Functions That Depend on Full Initialization: Avoid using functions like
 get_option(), is_customize_preview(), and other functions that require the
 full WordPress environment to be loaded. These functions rely on the
 database, user capabilities, or other elements that may not be available
 at this stage.

 Functions That Depend on Plugins or Themes: Do not use functions that
 depend on plugins or themes, as they might not be loaded yet. This
 includes any calls to functions or methods that could be affected by
 plugin or theme code.

 Functions with Side Effects: Functions that perform actions with potential
 side effects (like wp_insert_post(), wp_redirect(), etc.) should not be
 used since their behavior may be unpredictable during this early phase.

 Suggested Inline Comment:
 To clarify what is safe to use, a comment like the following has been
 added to the top of default-filters.php:

 {{{#!php
 <?php
 /**
  * This file is used to define default filters and actions for WordPress.
  *
  * Important notes:
  * - Only use functions that are safe to call during the early
 initialization phase.
  * - Functions from `load.php` are generally safe.
  * - Avoid using functions that depend on the full WordPress environment,
 such as `get_option()` and `is_customize_preview()`.
  * - Be cautious with anything that relies on plugins or themes, as they
 may not be fully loaded yet.
  */

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/38650#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list