Created
April 6, 2015 19:06
-
-
Save mattheu/88f86762486f863a5763 to your computer and use it in GitHub Desktop.
Shortcode Classes
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 | |
/** | |
* Example Shortcode Classes. | |
* Shamelessly stolen fron Daniel Bachhuber | |
* | |
* Short-term - a wrapper around add_shortcode. | |
* Long-term - WP should use shortcode classes and povide compatability with old-style shortcodes. | |
*/ | |
/** | |
* Wrapper for registering a shortcode. | |
* @param WP_Shortcode $shortcode [description] | |
* @return [type] [description] | |
*/ | |
function register_shortcode_class( WP_Shortcode $shortcode ) { | |
add_shortcode( $shortcode->get_tag(), get_class( $shortcode ) . '::render' ); | |
if ( ! empty( $shortcode->get_ui_args() ) ) { | |
Shortcode_UI::get_instance()->register_shortcode_ui( | |
$shortcode->get_tag(), | |
$shortcode->get_ui_args() | |
); | |
} | |
} | |
abstract class WP_Shortcode { | |
/** | |
* Get Shortcode Tag | |
*/ | |
public function get_tag() { | |
$parts = explode( '\\', get_called_class() ); | |
$tag = array_pop( $parts ); | |
return strtolower( str_replace( '_', '-', $tag ) ); | |
} | |
/** | |
* Output Shortcode | |
*/ | |
static public function render( $atts, $content = null ) {} | |
/** | |
* Get UI args. | |
* | |
* @return array UI args. | |
*/ | |
public function get_ui_args() { | |
return array(); | |
} | |
} | |
/** | |
* Example of creating your own shortcode. | |
*/ | |
class My_Shortcode extends WP_Shortcode { | |
static public function render( $atts, $content = null ) { | |
var_dump( 'My Shortcode' ); | |
} | |
} | |
add_action( 'init', function() { | |
register_shortcode_class( new My_Shortcode ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment