monakh2
3/28/2020 - 8:09 AM

Add my custom shortcode in OOP style

if ( ! class_exists( 'testShortcode' ) ) {
	class testShortcode {

		function __construct() {
			$this->init_shortcode();
		}

		function init_shortcode() {
			add_shortcode( 'testShortcode', array( $this, 'render_testShortcode' ) );
		}

		function render_testShortcode( $atts, $content = null ) {
			extract( shortcode_atts( array(
					"class" => "testClass",
				), $atts )
			);

			ob_start();
			?>
			<p class="<?= $class ?>">Konstantin Nikolaevich Monastyrskiy</p>
			<?php
			return ob_get_clean();
		}
	}

	new testShortcode;
}
class BaseShortcode {
	public function loadCss( $cssFileName, $src = '', $deps = array(), $ver = '1.0.0', $media = 'all' ) {
		$src = ! empty( $src ) ? get_stylesheet_directory_uri() . '/' . $src . '/' . $cssFileName . '.css' : get_stylesheet_directory_uri() . '/' . $cssFileName . '.css';

		wp_register_style( $cssFileName, $src, $deps, $ver, $media );
		wp_enqueue_style( $cssFileName );


		return true;
	}

	public function loadJS( $jsFileName, $src = '', $deps = array(), $ver = '1.0.0', $in_footer = false ) {
		$src = ! empty( $src ) ? get_stylesheet_directory_uri() . '/' . $src . '/' . $jsFileName . '.js' : get_stylesheet_directory_uri() . '/' . $jsFileName . '.js';

		wp_register_script( $jsFileName, $src, $deps, $ver, $in_footer );
		wp_enqueue_script( $jsFileName );

		return true;
	}

}