Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active February 4, 2017 01:07
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 rgchris/b473a165b6c44275b6b8a2e693e9deca to your computer and use it in GitHub Desktop.
Save rgchris/b473a165b6c44275b6b8a2e693e9deca to your computer and use it in GitHub Desktop.
WordPress Plugin: adds shortcodes to assist in incorporating features of Bootstrap including elementary grid handling.
<?php
/**
* @package Shortcodes for Bootstrap: Grid, Misc
* @version 0.6
*/
/*
Plugin Name: Shortcodes for Bootstrap: Grid, Misc
Plugin URI: https://gist.github.com/rgchris/b473a165b6c44275b6b8a2e693e9deca
Description: Adds shortcodes to assist in incorporating features of Bootstrap including elementary grid handling.
Author: Christopher Ross-Gill
Version: 0.6
Author URI: http://ross-gill.com
*/
defined( 'ABSPATH' ) or die( 'Shortcodes for Bootstrap Plugin' );
/**
* Container/Paragraph-level Shortcode Fix.
* https://gist.github.com/bitfade/4555047
*/
function rgc_content_filter($content) {
// array of custom shortcodes requiring the fix
$block = join(
"|",
array("break","row","column","well","page","header")
);
// opening tag
$rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
// closing tag
$rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
// wordpress auto-paragraph handling can f*ck right off
$rep = preg_replace("/<p>(.*<\/div>)/","$1",$rep);
$rep = preg_replace("/(<div.*)<\/p>/","$1",$rep);
// return $content;
return $rep;
}
add_filter("the_content", "rgc_content_filter");
/**
* Break [break]
*/
function rgc_break( $atts ) {
return '<hr class="divider">';
}
add_shortcode( 'break', 'rgc_break' );
/**
* Grid [row]\n[column width="one third"]\n...paragraphs...\n[/column]\n[/row]
*/
function rgc_row( $atts, $content = null ) {
$options = shortcode_atts(
array('class' => null,),
$atts
);
$class = "row";
if ($options['class']) {
$class .= ' ' . $options['class'];
}
return "\n<div class='" . $class . "'>" . do_shortcode($content) . "</div><!-- /row -->\n";
}
add_shortcode( 'row', 'rgc_row' );
function rgc_column( $atts, $content = null ) {
$options = shortcode_atts(
array(
'width' => null,
'medium' => null,
'small' => null,
'offset' => null,
'align' => null,
'break' => null,
'class' => null,
),
$atts
);
$class = 'column';
switch ($options['offset']) {
case 'sixth':
case 'one sixth':
case 'one-sixth':
$class .= ' col-md-offset-2';
break;
case 'twelfth':
case 'one twelfth':
case 'one-twelfth':
$class .= ' col-md-offset-1';
break;
};
switch ($options['medium'] ? $options['medium'] : $options['width']) {
case 'half':
case 'one half':
case 'one-half':
$class .= ' col-md-6';
break;
case 'third':
case 'one third':
case 'one-third':
$class .= ' col-md-4';
break;
case 'two thirds':
case 'two-thirds':
$class .= ' col-md-8';
break;
case 'quarter':
case 'one quarter':
case 'one-quarter':
$class .= ' col-md-3';
break;
case 'three quarters':
case 'three-quarters':
$class .= ' col-md-9';
break;
case 'sixth':
case 'one sixth':
case 'one-sixth':
$class .= ' col-md-2';
break;
case 'five sixths':
case 'five-sixths':
$class .= ' col-md-10';
break;
default:
$class .= ' col-md-4';
break;
};
switch ($options['small']) {
case 'half':
case 'one half':
case 'one-half':
$class .= ' col-sm-6';
break;
case 'third':
case 'one third':
case 'one-third':
$class .= ' col-sm-4';
break;
case 'two thirds':
case 'two-thirds':
$class .= ' col-sm-8';
break;
case 'quarter':
case 'one quarter':
case 'one-quarter':
$class .= ' col-sm-3';
break;
case 'three quarters':
case 'three-quarters':
$class .= ' col-sm-9';
break;
case 'sixth':
case 'one sixth':
case 'one-sixth':
$class .= ' col-sm-2';
break;
case 'five sixths':
case 'five-sixths':
$class .= ' col-sm-10';
break;
};
switch ($options['align']) {
case 'left':
$class .= ' text-left';
break;
case 'center':
$class .= ' text-center';
break;
case 'right':
$class .= ' text-right';
break;
};
return "<div class='" . $class . "'>" . do_shortcode($content) . "</div><!-- /" . $class . " -->";
}
add_shortcode( 'column', 'rgc_column' );
/**
* Well [well]...paragraphs...[/well]
*/
function rgc_well( $atts, $content = null ) {
return "<div class='well'>" . $content . "</div><!-- /well -->";
}
add_shortcode( 'well', 'rgc_well' );
/**
* Page Content [page]...paragraphs...[/page]
*/
function rgc_main_content( $atts, $content = null ) {
$options = shortcode_atts(
array(
'contained' => null,
'class' => null,
),
$atts
);
$class = 'main';
if ($options['class']) {
$class .= ' ' . $options['class'];
}
if ($options['contained'] = 'true') {
return "\n<div class='container'>\n<div class='" . $class . "'>" .
do_shortcode($content)
. "</div><!-- /main -->\n</div><!-- /container -->\n";
}
else {
return "\n<div class='" . $class . "'>\n<div class='container'>" .
do_shortcode($content)
. "</div><!-- /container -->\n</div><!-- /main -->\n";
}
}
add_shortcode( 'page', 'rgc_main_content' );
/**
* Jumbotron [
*/
function rgc_page_header( $atts, $content = null ) {
$options = shortcode_atts(
array(
'contained' => null,
'type' => null,
'align' => null,
'class' => null,
),
$atts
);
$class = 'jumbotron';
switch ($options['type']) {
case 'grey':
case 'gray':
$class .= ' grey';
break;
case 'featured':
case 'feature':
$class .= ' feature';
break;
};
switch ($options['align']) {
case 'left':
$class .= ' text-left';
break;
case 'center':
$class .= ' text-center';
break;
case 'right':
$class .= ' text-right';
break;
};
if ($options['class']) {
$class .= ' ' . $options['class'];
}
if ($options['contained'] = 'true') {
return "\n<div class='container'>\n<div class='" . $class . "'>" .
do_shortcode($content)
. "</div><!-- /jumbotron -->\n</div><!-- /container -->\n";
}
else {
return "\n<div class='" . $class . "'>\n<div class='container'>" .
do_shortcode($content)
. "</div><!-- /container -->\n</div><!-- /jumbotron -->\n";
}
}
add_shortcode( 'header', 'rgc_page_header' );
/**
* Icon [icon image="floppy-disk"]
*/
function rgc_icon( $atts ) {
$options = shortcode_atts(
array(
'image' => 'question-sign',
'size' => null,
),
$atts
);
$class = 'glyphicon glyphicon-';
if (in_array($options['image'], array(
"adjust", "alert", "align-center", "align-justify", "align-left", "align-right",
"arrow-down", "arrow-left", "arrow-right", "arrow-up", "asterisk",
"backward", "ban-circle", "barcode", "bell", "bold", "book", "bookmark",
"briefcase", "bullhorn",
"calendar", "camera", "certificate", "check", "chevron-down", "chevron-left",
"chevron-right", "chevron-up", "circle-arrow-down", "circle-arrow-left",
"circle-arrow-right", "circle-arrow-up", "cloud", "cloud-download",
"cloud-upload", "cog", "collapse-down", "collapse-up", "comment", "compressed",
"copyright-mark", "credit-card", "cutlery",
"dashboard", "download", "download-alt", "earphone", "edit", "eject", "envelope",
"euro", "exclamation-sign", "expand", "export", "eye-close", "eye-open",
"facetime-video", "fast-backward", "fast-forward", "file", "film", "filter",
"fire", "flag", "flash", "floppy-disk", "floppy-open", "floppy-remove",
"floppy-save", "floppy-saved", "folder-close", "folder-open", "font", "forward",
"fullscreen",
"gbp", "gift", "glass", "globe",
"hand-down", "hand-left", "hand-right", "hand-up", "hd-video", "hdd", "header",
"headphones", "heart", "heart-empty", "home",
"import", "inbox", "indent-left", "indent-right", "info-sign", "italic",
"leaf", "link", "list", "list-alt", "lock", "log-in", "log-out",
"magnet", "map-marker", "minus", "minus-sign", "money", "move", "music",
"new-window",
"off", "ok", "ok-circle", "ok-sign", "open",
"paperclip", "pause", "pencil", "phone", "phone-alt", "picture", "piggy-bank", "plane",
"play", "play-circle", "plus", "plus-sign", "print", "pushpin",
"qrcode", "question-sign",
"radar", "random", "record", "refresh", "registration-mark", "remove", "remove-circle",
"remove-sign", "repeat", "resize-full", "resize-horizontal", "resize-small",
"resize-vertical", "retweet", "road",
"save", "saved", "screenshot", "sd-video", "search", "send", "share",
"share-alt", "shopping-cart", "signal", "sort", "sort-by-alphabet",
"sort-by-alphabet-alt", "sort-by-attributes", "sort-by-attributes-alt",
"sort-by-order", "sort-by-order-alt", "sound-5-1", "sound-6-1", "sound-7-1",
"sound-dolby", "sound-stereo", "star", "star-empty", "stats", "step-backward",
"step-forward", "stop", "subtitles",
"tag", "tags", "tasks", "text-height", "text-width", "th", "th-large", "th-list",
"thumbs-down", "thumbs-up", "time", "tint", "tower", "transfer", "trash",
"tree-conifer", "tree-deciduous",
"unchecked", "upload", "usd", "user",
"volume-down", "volume-off", "volume-up",
"warning-sign", "wrench",
"zoom-in", "zoom-out"
))) :
$class .= $options['image'];
else :
$class .= 'question-sign';
endif;
switch ($options['size']) {
case 'large':
$class .= ' large';
break;
case 'small':
$class .= ' small';
break;
};
return '<i class="' . $class . '" aria-role="hidden"></i>';
}
add_shortcode( 'icon', 'rgc_icon' );
/**
* Bullet [bullet name="floppy-disk"]This is a Bulleted Item[/bullet]
*/
function rgc_bullet( $atts, $content = null ) {
$options = shortcode_atts(
array(
'image' => 'question-sign',
),
$atts
);
return "\n<div class='media'>\n<div class='media-left'>" . do_shortcode('[icon image="' . $options['image'] . '"]') . "</div>\n"
. "<div class='media-body'>\n" . do_shortcode($content) . "</div>\n</div><!-- /media -->";
}
add_shortcode( 'bullet', 'rgc_bullet' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment