WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#15066 closed enhancement (duplicate)

Create a Meta Box class

Reported by: ericmann Owned by: nacin
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords: needs-patch
Focuses: Cc:

Description

Widgets became much more extensible when we introduced the WP_Widget class in version 2.8. New widgets were easier to make, and we finally had the ability to create multi-instance widgets without a lot of coding overhead and headaches.

Meta Boxes are used by themes and plug-ins in a similar fashion to customize the layout and functionality of the post/page/custom-post-type edit screen. Unfortunately, the code for creating a new meta box and handling the subsequent form submission is difficult to track down, prone to errors, and nearly impossible to use in a multi-instance situation.

We should abstract the meta box creation process so that adding a new meta box is as easy as adding a new widget - just extend a WP_Meta_Box class to add the functionality.

Change History (23)

comment:1 @Denis-de-Bernardy5 years ago

  • Cc Denis-de-Bernardy added

comment:2 follow-ups: @sirzooro5 years ago

  • Cc sirzooro added

+1 for this.

This new class should also include things listed in example for add_meta_box() - I mean nonce, capability and autosave checks - they are used in many custom meta boxes. Of course there should be option(s) to disable them if necessary.

comment:3 in reply to: ↑ 2 ; follow-up: @ericmann5 years ago

Replying to sirzooro:

Of course there should be option(s) to disable them if necessary.

I can't think of a situation where disabling nonce verification would be necessary or a good idea (given, I can't ever think of everything ... so if you can give me an example, that would be much appreciated).

But I definitely think noncing, capability and auto-save checks, and AJAX support should be built-in. I spend a lot of time helping new devs trouble-shoot their meta boxes, and most of them just forget to verify a nonce, or have a typo (missed semi-colon) in the code that we all copy-paste whenever making a new box.

comment:4 in reply to: ↑ 3 @sirzooro5 years ago

Replying to ericmann:

Replying to sirzooro:

Of course there should be option(s) to disable them if necessary.

I can't think of a situation where disabling nonce verification would be necessary or a good idea (given, I can't ever think of everything ... so if you can give me an example, that would be much appreciated).

I can think of three cases:

  • informational only box - it presents some information only and does not allow any modifications (or they are not sent back to server) - in this case nonces are not needed at all;
  • solution which uses more than one once to track multiple parts of meta box, which may be present or not depending on some condition - in this case plugin should be able to print and validate multiple onces;
  • custom nonce-like solution (although I do not see a point why someone would like to go this way).

comment:5 in reply to: ↑ 2 @scribu5 years ago

+1 on a metabox class

Replying to sirzooro:

This new class should also include things listed in example for add_meta_box() - I mean nonce, capability and autosave checks - they are used in many custom meta boxes. Of course there should be option(s) to disable them if necessary.

You have to remember that metaboxes are not only for the post editing page.

We should have a general WP_Metabox class and a WP_Post_Metabox that extends it.

comment:6 @scribu5 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

comment:7 @nacin5 years ago

Marked #12450 as a duplicate. Some good ideas there.

comment:8 @azizur5 years ago

  • Cc azizur added

comment:9 @sbressler5 years ago

  • Cc sbressler@… added

comment:10 @ramiy5 years ago

  • Cc ramiy added

comment:11 follow-up: @azizur5 years ago

I am working on a patch for this. If anyone wish to help reply to this note.

comment:12 in reply to: ↑ 11 ; follow-up: @westi5 years ago

  • Keywords gci added

Replying to azizur:

I am working on a patch for this. If anyone wish to help reply to this note.

Are you working on this for Google Code In?

We have it as a task for that so if not we need to know to remove it from there so we don't get a student who wants to work on this finding the work already done ;-)

comment:13 in reply to: ↑ 12 @azizur5 years ago

Replying to westi:

Are you working on this for Google Code In?

I am not a student so can't take part in Google Code In.

I am working on couple of projects that heavily depends on this feature and would really help if this was built-in as part of the core.

I have just started development and looking into replicating WP_Widget class into WP_Metabox class to keep the same coding standard. Unless you feel that I should take a different approach.

comment:14 @GamajoTech5 years ago

  • Cc gary@… added

comment:15 @mikeschinkel5 years ago

  • Cc mikeschinkel@… added

comment:16 @vteixeira5 years ago

  • Cc victorhteixeira@… added

comment:17 @vteixeira5 years ago

I just want to point out this class for reference: http://farinspace.com/wpalchemy-metabox/ or on github: https://github.com/farinspace/wpalchemy

comment:18 @bainternet5 years ago

  • Cc bainternet added

+1 on a metabox class

comment:19 @kingkool685 years ago

I wrote my own -> http://svn.kingkool68.com/projects/metabox/

Takes the same arguments as add_meta_box but adds a new option to specify category IDs so the boxes are only visible if that category is checked. It helps reduce the visual clutter.

Just throwing it out there.

comment:20 @scribu5 years ago

Related: #14877

comment:21 @nacin5 years ago

  • Keywords gci removed
  • Owner set to nacin
  • Status changed from new to reviewing

comment:22 @c3mdigital4 years ago

  • Cc chris@… added

comment:23 @koopersmith4 years ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from reviewing to closed

Let's give this a shot for 3.3 — a fresh ticket can be found at #18179.

Note: See TracTickets for help on using tickets.