Skip to content

Instantly share code, notes, and snippets.

@ev3rywh3re
Last active December 11, 2015 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ev3rywh3re/4573482 to your computer and use it in GitHub Desktop.
Save ev3rywh3re/4573482 to your computer and use it in GitHub Desktop.
Ye Olde PHP HTML Tag Function
/**
* FNBX HTML Tag
*
* Core utility function for the writing and manipulation of HTML tags.
*
* @since 1.0
* @echo string
*/
if ( !function_exists( 'fnbx_html_tag' ) ) {
function fnbx_html_tag( $html = array() ) {
if ( empty( $html ) ) return;
$attributes = '';
$composite = '';
$spacer = '';
if ( !isset( $html['return'] ) ) $html['return'] = false;
$reserved = array(
'tag', 'tag_type', 'attributes', 'tag_content', 'tag_content_before', 'tag_content_after', 'return'
);
foreach ( $html as $name => $option ) {
if ( in_array( $name, $reserved ) ) continue;
$attributes .= $name . '="' . $option . '" ';
}
if ( isset( $html['attributes'] ) ) $attributes .= $html['attributes'] . ' ' . $attributes;
if ( $attributes != '' ) {
$attributes = rtrim( $attributes );
$spacer = ' ';
}
if ( !isset( $html['tag_type'] ) ) $html['tag_type'] = 'default';
if ( isset( $html['tag_content_before'] ) ) $composite .= $html['tag_content_before'];
switch ( $html['tag_type'] ) {
case 'single':
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '/>';
break;
case 'open':
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '>';
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
break;
case 'close':
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '</' . $html['tag'] . '>';
break;
case 'attributes':
$composite = $attributes;
break;
case 'default':
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '>';
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '</' . $html['tag'] . '>';
break;
}
if ( isset( $html['tag_content_after'] ) ) $composite .= $html['tag_content_after'];
if ( $html['return'] == true ) return $composite ;
echo $composite;
}
}
//---- It's a sample ----//
/**
* Main Website Title HTML
*
* Writes HTML for website title. H1 by default but may be filtered for homepage div instead
*
* @since 1.0
* @echo string
*/
function fnbx_default_title() {
$title_link = fnbx_html_tag( array(
'tag' => 'a',
'href' => home_url(),
'rel' => 'home',
'title' => esc_html( get_bloginfo('name') ),
'tag_content' => get_bloginfo('name'),
'return' => true
) );
$title_defaults = array(
'tag' => 'h1',
'id' => 'blog-title',
'class' => 'blog-title-',
'tag_content' => $title_link,
'tag_content_before' => "\n",
'tag_content_after' => "\n"
);
$title_defaults = apply_filters( 'fnbx_default_title', $title_defaults );
fnbx_html_tag( $title_defaults );
}
/*
Produces something like
<h1 id="blog-title" class="blog-title-"><a href="" rel="home" title=""></a></h1>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment