Shortcode helper for WordPress
The Shortcode component uses two classes: - AbstractShortcode, which you extend to create a Shortcode. - ShortcodeHelper, which hooks into everything for you and registers the shortcodes., (*1)
Lets create a pimple config for both of these, (*2)
$pimple["documentLinkShortcode"] = function () { return new DocumentLink(); }; $pimple['ShortcodeHelper'] = function ($c) { return new ShortcodeHelper($c['objectStorage'], array( $c["documentLinkShortcode"] )); };
Create a concreate class that implements the AbstractShortcode class and implements the getName() and getCallback() methods., (*3)
class DocumentLink extends AbstractShortcode { public function getName() { return "document_link"; } public function getCallback() { $link = func_get_arg(0); return '<a href="' . $link . '">Download document</a>'; } }
Once you have setup the pimple config you are use the ShortcodeHelper like this, (*4)
$helper = $pimple['ShortcodeHelper']; $helper->registerShortcodes();
That's it, the helper will then add all the needed hooks and register all the shortcodes you have provided it., (*5)