Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created March 8, 2018 04:47
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 maheshwaghmare/1945cdc951a81c82581471e7678bf762 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/1945cdc951a81c82581471e7678bf762 to your computer and use it in GitHub Desktop.
Create a simple shortcode in WordPress
<?php
/**
* Prefix Shortcode
*
* @todo Change the `Prefix` with your own unique prefix.
* @todo Change the `prefix` with your own unique prefix.
*
* @package Prefix
* @since 1.0.0
*/
if ( ! class_exists( 'Prefix_Shortcode' ) ) :
/**
* Prefix_Shortcode
*
* @since 1.0.0
*/
class Prefix_Shortcode {
/**
* Instance
*
* @access private
* @var object Class Instance.
* @since 1.0.0
*/
private static $instance;
/**
* Initiator
*
* @since 1.0.0
* @return object initialized object of class.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct()
{
// Register shortcode.
add_shortcode( 'prefix-portfolio', array( $this, 'shortcode_markup' ) );
}
/**
* Shortcode
*
* @since 1.0.0
* @param array $data Shortcode attributes.
* @return mixed Shortcode markup.
*/
function shortcode_markup( $data = array() )
{
$data = shortcode_atts(
array(
'attribute-1' => '',
'attribute-2' => '',
), $data
);
ob_start();
?>
<div class="prefix-shortcode">
<?php echo esc_html( $data['attribute-1'] ); ?>
<?php echo esc_html( $data['attribute-2'] ); ?>
</div>
<?php
return ob_get_clean();
}
}
/**
* Kicking this off by calling 'get_instance()' method
*/
Prefix_Shortcode::get_instance();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment