Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Shortcode: Create Headings with anchor + link icon
/**
* Create a shortcode to insert a header with an anchor and link icon
* Use [heading size="2" id="anchor"]Heading[/heading]
*
* Recommended: Add prefix / change shortcode name to avoid conflicts
*/
function br_heading_shortcode( $atts, $content = null ) {
$a = shortcode_atts( array(
//set default size="2" to default to h2
'size' => '2',
'id' => 'anchor',
), $atts );
// Buffer output
ob_start();
// Replace <i class="fa fa-link heading-icon"></i> with a different icon or image if desired / not using FontAwesome
echo '<h' . esc_attr($a['size']) . ' id="'. esc_attr($a['id']) . '">' . $content . ' <a href="#' .esc_attr($a['id']) . '"><i class="fa fa-link heading-icon"></i></a></h' . esc_attr($a['size']) . '>';
// Return buffered contents
return ob_get_clean();
}
add_shortcode( 'heading', 'br_heading_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment