Created
October 10, 2018 12:23
-
-
Save greghunt/e1c213ffd9dc0e8bd91c75b4e7190e6c to your computer and use it in GitHub Desktop.
Simple class for organizing and registering your WordPress shortcodes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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