Opened 4 months ago

Last modified 8 days ago

#23236 new feature request

Add functions to format attributes

Reported by: ryanve Owned by:
Priority: normal Milestone: Awaiting Review
Component: Formatting Version:
Severity: normal Keywords: needs-patch
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 (6)

  • Component changed from General to Formatting
  • Keywords needs-patch added

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.

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

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.

Last edited 8 days ago by ryanve (previous) (diff)
Note: See TracTickets for help on using tickets.