Skip to content
Snippets Groups Projects
lbdesign-button-shortcode.php 2.79 KiB
Newer Older
lepittenger's avatar
lepittenger committed
<?php
/**
* Plugin Name: LBDesign Button Shortcode
* Description: A simple button shortcode
* Author: Lauren Pittenger @ LBDesign
Lauren's avatar
Lauren committed
* Author URI: http://laurenpittenger.com
* License: GPL
Kevin Cristiano's avatar
Kevin Cristiano committed
* Version: 1.4.0
lepittenger's avatar
lepittenger committed
*/

/* our main shortcode function */
Lauren's avatar
Lauren committed
function lbdesign_button_shortcode( $atts, $content = null ) {

    $classes[] = '';

Lauren's avatar
Lauren committed
    $atts = shortcode_atts(array(
    	'link'  => null,
      'new_tab' => false,
Lauren's avatar
Lauren committed
    	'type'  => 'default',
Lauren's avatar
Lauren committed
    	'size'  => 'default',
        'style' => 'default',
        'custom_class' => null,
Lauren's avatar
Lauren committed
    ), $atts, 'lbdesign_button' );
Lauren's avatar
Lauren committed
    foreach ( $atts as $key => $att ) {
        if( $key !== 'link' && $key !== 'new_tab' && $key !== 'custom_class' && $att !== null && $att !== '' && $att !== 'default' ) {
            $classes[] = 'lbdesign_' . strtolower( esc_attr( $att ) );
Lauren's avatar
Lauren committed
        }
Lauren's avatar
Lauren committed
        if( $key == 'custom_class' ) {
            $classes[] = esc_attr( $att );
Lauren's avatar
Lauren committed

        if( $key == 'new_tab' && false !== $att && "false" !== $att ) {
            $target = 'target="_blank"';
        }

Kevin Cristiano's avatar
Kevin Cristiano committed
   return '<a class="lbdesign_button ' . implode( " ", $classes ) . '" href="' . esc_url( $atts['link'] ) . '" ' . $target . '>' . do_shortcode( $content ) . '</a>';
Lauren's avatar
Lauren committed

Lauren's avatar
Lauren committed
add_shortcode( 'lbdesign_button', 'lbdesign_button_shortcode' );


/* enqueue the default button styles */
function lbdesign_button_styles() {

    /* default styles */
Lauren's avatar
Lauren committed
    wp_register_style( 'lbdesign-button-shortcode', plugins_url() . '/lbdesign-button-shortcode/css/lbdesign_button_shortcode.css' );
    wp_enqueue_style( 'lbdesign-button-shortcode' );

}
add_action( 'wp_enqueue_scripts', 'lbdesign_button_styles' );

function lbdesign_tinymce_buttons() {
Lauren's avatar
Lauren committed

    if ( is_admin() && current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) ) {
      add_filter( 'mce_external_plugins', 'lbdesign_add_buttons' ); // hooks plugin to TinyMCE
      add_filter( 'mce_buttons', 'lbdesign_register_buttons' ); // used to show which buttons to show on TinyMCE
    }
Lauren's avatar
Lauren committed

Lauren's avatar
Lauren committed
add_action( 'init', 'lbdesign_tinymce_buttons' );

/* add custom button to wp editor */
function lbdesign_add_buttons( $plugin_array ) {
Lauren's avatar
Lauren committed

    $plugin_array['lbdesign'] = plugins_url( '/js/lbdesign-button-shortcode.js', __FILE__ ); // LBDesignButtonShortcode is the plugin ID
    return $plugin_array;
Lauren's avatar
Lauren committed

Lauren's avatar
Lauren committed

function lbdesign_register_buttons( $buttons ) {
Lauren's avatar
Lauren committed

    array_push( $buttons, '|', 'buttonshortcode' ); // buttonshortcode is the button ID
Lauren's avatar
Lauren committed

lepittenger's avatar
lepittenger committed
}

/**
 * Localize Script
 */
function lbdbs_admin_head() {
  $plugin_url = plugins_url( '/', __FILE__ );
  ?>
  <!-- TinyMCE Shortcode Plugin -->
  <script type='text/javascript'>
  var lbdbs_plugin = {
      'url': '<?php echo $plugin_url; ?>',
  };
  </script>
  <!-- TinyMCE Shortcode Plugin -->
  <?php
}
add_action( "admin_head", 'lbdbs_admin_head' );