WordPress.org

Make WordPress Core

Opened 15 months ago

Last modified 11 months ago

#23237 new enhancement

Add tag function to format markup

Reported by: ryanve Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

Related to #23236 I'd like to add a generic function for making html tags:

wp_tag( $tagname, $attrs, $inner_html ) // returns an html tag
wp_tag_e( $tagname, $attrs, $inner_html ) // echoes an html tag

$attrs could be an array|string, and the tag should only close if $inner_html is defined, like tag() in phat.

Change History (4)

comment:1 ev3rywh3re15 months ago

  • Cc jess@… added

I'm always trying to get a good simple HTML writing function. Never mind the debate about when it's useful since PHP is already this funky parsing sort-of template language. When doing complex HTML structures with bizarre data a simple tag writing function is helpful.

My pocket-knife-style function I've carried around for years helps me fine, but I'd love to see something cool and mindful of the WordPress nature of actions, filters, and brevity. ;)

https://gist.github.com/4573482

Been so busy for quite a while, so I'm actually surprised this sort of thing hasn't happened yet.

comment:3 ryanve15 months ago

@ev3rywh3re @scribu Thanks—I'm gonna work on a version that takes the best ideas from all of em. #23236 should handle the attributes argument, and that's the hardest part. I'm on the fence about whether a hook is needed within wp_tag().

comment:4 ryanve11 months ago

This ticket is dependent on #23236. I added wphat.php in the phat repo called where I'm working on these. The syntax wp_tag($tagname, $attrs, $inner) described above is ideal IMO. A closing tag is added when $inner is not null. Do we need to provide a syntax for auto-closing XHTML self-closing tags? If needed I think one of these syntaxes could work:

wp_tag('img/', $attrs) reads better
wp_tag('img', $attrs, true)
makes secondary use of the $inner param

Note: See TracTickets for help on using tickets.