WordPress.org

Make WordPress Core

Opened 15 months ago

Closed 3 months ago

#23236 closed feature request (wontfix)

Add functions to format attributes

Reported by: ryanve Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Formatting Keywords: needs-patch
Focuses: Cc:

Description

I'd like to see WP add functions for attributes. These would be extremely useful and resuable for theme devs and internally. I've developed very solid generic functions for this in phat and would be happy to make WP versions to the effect of:

wp_attrs( $array ) // converts array to attributes string
wp_attrs( $name, $value ) // formats attribute string
wp_attrs( $string ) // reformats attribute string
wp_parse_attrs( $string ) parses attributes into assoc array

Change History (7)

comment:1 alexvorn215 months ago

  • Component changed from General to Formatting

comment:2 alexvorn215 months ago

  • Keywords needs-patch added

comment:4 ryanve15 months ago

Here's how I think wp_parse_attrs() should be. It uses a primitive loop and is very fail-safe. I don't think regexp is viable due to complications from quotes (single, double, unquoted, JSON). DOMDocument is viable but too brittle.

comment:5 ryanve15 months ago

I'll work up a wp_attrs() function similar in effect to phat\attrs() but specific to WordPress.

comment:6 ryanve11 months ago

I added wphat.php in the phat repo called where I'm working on this. I think it'll be better to name the functions wp_attr and wp_attr_parse b/c both can handle 1+ attributes.

How do we want to encode complex (array|object) attributes? Currently it checks for the ~10 attributes that take tokens ([class], [media], etc.) and implode them with the correct glue. Otherwise it uses json_encode. Should we skip the check and go with the simple approach of using json_encode for any complex value? In either case I'll include a 'complex_attr_value' filter.

I avoided esc_attr b/c it breaks JSON and b/c it double-encodes entities.

Version 0, edited 11 months ago by ryanve (next)

comment:7 nacin3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Closing to due lack of interest. It's possible for these concepts to come back up as the metadata API is discussed, of course.

Note: See TracTickets for help on using tickets.