Skip to content

Instantly share code, notes, and snippets.

@mkormendy
Last active January 21, 2016 05:30
Show Gist options
  • Save mkormendy/34e7e22a165f0a02d566 to your computer and use it in GitHub Desktop.
Save mkormendy/34e7e22a165f0a02d566 to your computer and use it in GitHub Desktop.
Site-Specific WordPress Plugin Template (shortcodes)
<?php
/*
Plugin Name: Template Site Plugin for example.com
Description: Site specific plugin template for example.com, in this case we are creating a widget
*/
////////////////////////////////////////////
// Start Adding Functions Below this Line //
////////////////////////////////////////////
/* self-closing shortcode */
function prefixname1_shortcode() {
/* do something that outputs at that point where the shortcode is placed */
return 'I am something that is placed in place where the shortcode used to be.';
}
add_shortcode('shortcodename1', 'prefixname1_shortcode');
/* usage: [shortcodename1] */
/* self-closing shortcode with parameters */
function button_shortcode($type) {
extract(shortcode_atts(array(
'type' => 'type'
), $type));
// check what type user entered
switch ($type) {
case 'twitter':
return '<a href="http://twitter.com/twitterusername" class="twitter-button">Follw me on Twitter!</a>';
break;
case 'rss':
return '<a href="http://example.com/rss" class="rss-button">Subscribe to the feed!</a>'
break;
}
}
add_shortcode('button', 'button_shortcode');
/* usage
[button type="twitter"]
[button type="rss"]
*/
/* enclosing shortcode with parameters */
function button_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'account' => 'account',
'style' => 'style'
), $atts ) );
return '<a href="http://twitter.com/' . esc_attr($account) . '" class="twitter-button ' . esc_attr($style) . '">' . $content . '</a>';
}
add_shortcode('button', 'button_shortcode');
/* usage
[button account="twitterusername" style="simple"]Follow me on Twitter![/button]
/* results
<a href="http://twitter.com/twitterusername" class="twitter-button simple">Follow me on Twitter!</a>
*/
/* code in posts */
function code_shortcode( $attr, $content = null ) {
$content = clean_pre($content); // Clean pre-tags
return '<pre"><code>' .
str_replace('<', '<', $content) . // Escape < chars
'</code></pre>';
}
add_shortcode('code', 'code_shortcode');
/* usage [code]<?php echo 'Hello World!'; ?>[/code] */
/* private note to authors */
function sc_note( $atts, $content = null ) {
if ( current_user_can( 'publish_posts' ) )
return '<div class="note">'.$content.'</div>';
return '';
}
add_shortcode( 'note', 'sc_note' );
/* usage [note]Psst! This is just for authors.[/note] */
///////////
// Other //
///////////
/* use shortcodes in a widget */
add_filter('widget_text', 'do_shortcode')
/* use shortcodes in php files directly */
do_shortcode("[button]");
///////////////////////////////////////////
// Stop adding functions below this line //
///////////////////////////////////////////
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment