Skip to content

Instantly share code, notes, and snippets.

@JRyven
Created April 20, 2021 09:12
Show Gist options
  • Save JRyven/fce8144552297be57be8fc3f15501a91 to your computer and use it in GitHub Desktop.
Save JRyven/fce8144552297be57be8fc3f15501a91 to your computer and use it in GitHub Desktop.
WordPress Shortcode Template OOP with Style and Scrip
<?php
/*
*
*
*
*
*/
class Name__Class {
public function __construct( $uri = false ){
$this->Name__Class_shortcodes();
$this->styles();
$this->scripts();
}
public function Name__Class_shortcodes(){
add_shortcode('Handle__Shortcode', array( $this, 'Handle__Shortcode_callback'));
}
/* Handle__Shortcode_callback
*
* Useage
* [Handle__Shortcode &params]
*
* Params
*
* @ include - (required) enter comma separated list of post tag slugs to populate navigation.
*/
public function Handle__Shortcode_callback( $atts ){
$atts = shortcode_atts( array(
'taxonomy' => '', // defaults to category
'include' => '', // defaults to all terms
), $atts );
ob_start(); ?>
<div id="" class="">
</div>
<?php return ob_get_clean(); ?>
<?php
}
/* Another__Shortcode_callback
*
* Useage
* [Another__Shortcode &params]
*
* Params
*
* @
public function Another__Shortcode_callback( $atts ){
$atts = shortcode_atts( array(
'id' => '',
), $atts );
ob_start(); ?>
<?php return ob_get_clean(); ?>
<?php
}
*/
private function styles() {
ob_start();
?>
<?php
$Name__Class__Styles = ob_get_clean();
// only enqueue styles if they're not already enqueued.
if ( ! wp_style_is( 'bpm-module-styles', 'enqueued' ) ) {
wp_register_style( 'bpm-module-styles', FALSE );
wp_enqueue_style( 'bpm-module-styles' );
}
wp_add_inline_style( 'bpm-module-styles', $Name__Class__Styles );
}
private function scripts( $uri = false ) {
ob_start();
?>
<?php
$Name__Class__Scripts = ob_get_clean();
// only enqueue styles if they're not already enqueued.
if ( ! wp_script_is( 'bpm-module-scripts', 'enqueued' ) ) {
wp_register_script( 'bpm-module-scripts', FALSE );
wp_enqueue_script( 'bpm-module-scripts' );
}
wp_add_inline_script( 'bpm-module-scripts', $Name__Class__Scripts );
}
} // close the Name__Class() Class
/* Call the Name__Class() Class to activate shortcode
*
*
*/
add_action('wp_head', 'activate__Name__Class');
function activate__Name__Class(){
new Name__Class();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment