Make WordPress Core

Opened 8 months ago

Closed 8 months ago

#64828 closed defect (bug) (fixed)

add_filter() used on actions and add_action() used on filters in core

Reported by: apermo Owned by: SergeyBiryukov
Priority: normal Milestone: 7.0
Component: General Version:
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Three cases in core where hooks are registered with the wrong function:

  • add_filter( 'admin_print_styles', ... ) in wp-admin/includes/admin-filters.php:56 — hook fires via do_action()
  • add_filter( 'login_head', ... ) in wp-includes/default-filters.php:388 — hook fires via do_action()
  • add_action( 'use_block_editor_for_post_type', ... ) in wp-admin/includes/admin-filters.php:88 — hook fires via apply_filters()

A 4th case (get_block_type_variations) originates in Gutenberg: ​https://github.com/WordPress/gutenberg/issues/76296

Functionally equivalent since add_action() calls add_filter() internally, but semantically incorrect.

Found while working on #64224 (docblock improvements). Related to #64238 (PHPStan catchall).

PR: ​https://github.com/WordPress/wordpress-develop/pull/11204

Change History (3)

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


8 months ago
#1

## Summary

Fixes 3 cases where hooks are registered with the wrong function — add_action() used on a filter or add_filter() used on an action. Functionally equivalent (since add_action() calls add_filter() internally), but semantically incorrect.

### add_filter() → add_action() (hook is fired via do_action()):

  • admin_print_styles in wp-admin/includes/admin-filters.php
  • login_head in wp-includes/default-filters.php

### add_action() → add_filter() (hook is fired via apply_filters()):

  • use_block_editor_for_post_type in wp-admin/includes/admin-filters.php

A 4th mismatch (get_block_type_variations using add_action() on a filter) originates in the Gutenberg repo and is tracked separately: ​https://github.com/WordPress/gutenberg/issues/76296

For detailed investigation notes, see: ​https://github.com/apermo/wordpress-develop/milestone/4

## Use of AI Tools

Research (systematic cross-referencing of all add_action/add_filter registrations against their do_action/apply_filters call sites) and code were produced by Claude Code (claude-sonnet-4-6). The contributor reviewed the findings, verified each fix, and approved the final implementation.

#2 @SergeyBiryukov
8 months ago

  • Milestone Awaiting Review → 7.0
  • Owner set to SergeyBiryukov
  • Status new → reviewing

#3 @SergeyBiryukov
8 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 61866:

Code Quality: Correct hook registration type mismatches.

Though add_action() and add_filter() are functionally equivalent internally, for proper semantics the former should be used on actions and the latter on filters.

Follow-up to [37920], [38046], [53266].

Props apermo.
Fixes #64828.

Note: See TracTickets for help on using tickets.