Skip to content

Instantly share code, notes, and snippets.

@smeric
Created October 7, 2017 19:42
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 smeric/1e57d85c0096b77aaa6cf7ea86d00887 to your computer and use it in GitHub Desktop.
Save smeric/1e57d85c0096b77aaa6cf7ea86d00887 to your computer and use it in GitHub Desktop.
WordPress HTML tags shortcode. So you may add HTML to escaped form fields values :)
<?php
/**
* HTML tags shortcode
*
* Usage: [html tag="span" class="myClass" id="firstSpan"]Lorem ipsum[html tag='br']dolor sit amet[/html]
*/
function html_tag_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'tag' => 'div',
'id' => '',
'class' => '',
'href' => '',
'src' => '',
'alt' => '',
'name' => '',
'type' => '',
'value' => '',
'style' => '',
), $atts, 'html' ) );
$single = array( 'img', 'br', 'input' );
if ( in_array( $tag, $single ) ) {
$return = '<' . esc_attr( $tag ) .
( $id ? ' id="' . esc_attr( $id ) . '"' : '' ) .
( $class ? ' class="' . esc_attr( $class ) . '"' : '' ) .
( $src ? ' src="' . esc_attr( $src ) . '"' : '' ) .
( $alt ? ' alt="' . esc_attr( $alt ) . '"' : ( $src ? ' alt=""' : '' ) ) .
( $name ? ' name="' . esc_attr( $name ) . '"' : '' ) .
( $type ? ' type="' . esc_attr( $type ) . '"' : '' ) .
( $value ? ' value="' . esc_attr( $value ) . '"' : '' ) .
( $style ? ' style="' . esc_attr( $style ) . '"' : '' ) .
' />';
}
else {
$return = '<' . esc_attr( $tag ) .
( $id ? ' id="' . esc_attr( $id ) . '"' : '' ) .
( $class ? ' class="' . esc_attr( $class ) . '"' : '' ) .
( $href ? ' href="' . esc_attr( $href ) . '"' : '' ) .
( $style ? ' style="' . esc_attr( $style ) . '"' : '' ) .
'>' . do_shortcode( $content ) . '</' . esc_attr( $tag ) . '>';
}
return $return;
}
add_shortcode( 'html', 'html_tag_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment