Make WordPress Core

Opened 6 months ago

Closed 6 months ago

#64251 closed defect (bug) (fixed)

Documentation for `add_meta_boxes` hook is incorrect

Reported by: opr18's profile opr18 Owned by: westonruter's profile westonruter
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Administration Keywords: has-patch
Focuses: docs Cc:

Description

Related ticket: https://core.trac.wordpress.org/ticket/43229 that seems was solved a while ago, but has since changed back.

[The documentation for this hook](https://core.trac.wordpress.org/browser/tags/6.8.3/src/wp-admin/includes/meta-boxes.php#L1714) states that $post will be a WP_Post however in these locations it is not

I think some possible resolutions here would be:

  • Update the docs to loosen the type of the second param being passed to the hook
  • Fix instances where the second param to the hook is not a WP_Post

Change History (8)

#1 @soyebsalar01
6 months ago

The hook is too old and too widely used to be refactored without breaking plugins, so explicitly documenting:
we can add the one line so User might not confuse.

@param WP_Post | WP_Comment | stdClass $post

do_action( 'edit_form_after_title', $post, $title ) this hooks used in several admin screens it is fired with non-WP_Post types, for example:

Link editor -> passes a stdClass (legacy Link Manager structure)

Comment editor-> passes a WP_Comment

Some meta-box contexts -> might pass null or an incomplete object depending on screen

so @opr18 is right the docs expect $post to always be a WP_Post, which isn’t true. This creates fragile code, silent failures, type-hinting conflicts, confusion for people reading core docs.

#2 @opr18
6 months ago

  • Summary changed from Documentation for `add_meta_boxes hook` is incorrect to Documentation for `add_meta_boxes` hook is incorrect

#3 @westonruter
6 months ago

  • Component changed from Options, Meta APIs to Administration
  • Focuses docs added
  • Milestone changed from Awaiting Review to 7.0

Update the docs to loosen the type of the second param being passed to the hook

This makes sense to me.

This ticket was mentioned in PR #10527 on WordPress/wordpress-develop by @opr18.


6 months ago
#4

  • Keywords has-patch added

This patch updates the documentation for the add_meta_boxes_{$post_type} and add_meta_boxes hooks to correctly reflect that $post will not always be a WP_Post object.

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

#5 @nikunj8866
6 months ago

  • Keywords changes-requested added

@opr18 I've added a comment in PR for a minor adjustment.

#6 @nikunj8866
6 months ago

  • Keywords changes-requested removed

#7 @westonruter
6 months ago

  • Owner set to westonruter
  • Status changed from new to reviewing

#8 @westonruter
6 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 61278:

Docs: Update meta box action documentation to more accurately reflect the possible object types.

In addition to a meta box being able to be added for a post, it can also be added for a comment or a link, or even nothing at all when on the Dashboard.

Props opr18, nikunj8866, soyebsalar01, westonruter.
See #64224.
Fixes #64251.

Note: See TracTickets for help on using tickets.