Skip to content

Instantly share code, notes, and snippets.

@greghunt
Created October 10, 2018 12:23
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 greghunt/e1c213ffd9dc0e8bd91c75b4e7190e6c to your computer and use it in GitHub Desktop.
Save greghunt/e1c213ffd9dc0e8bd91c75b4e7190e6c to your computer and use it in GitHub Desktop.
Simple class for organizing and registering your WordPress shortcodes.
<?php
namespace App;
class Shortcodes {
protected $shortcodes = [];
public function __construct()
{
$this->addShortcode('card');
}
private function addShortcode( $name )
{
$this->shortcodes[] = $name;
}
public function card( $atts , $content = null )
{
// Attributes
$atts = shortcode_atts(
array(
),
$atts,
'card'
);
$html = '<div class="card">';
$html .= $content;
$html .= '</div>';
return $html;
}
public function register()
{
/**
* Add the shortcodes
*/
add_action( 'init', function() {
foreach( $this->shortcodes as $name ) {
add_shortcode( $name, [$this, $name] );
}
});
}
}
/**
* Register Shortcodes
*/
$shortcodes = new Shortcodes;
$shortcodes->register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment