public
Last active

Ye Olde PHP HTML Tag Function

  • Download Gist
ye-olde-html-tag-function.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
* 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>
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.