How to create shortcode in class
-
Hi!
I’m trying to create shortcode in class, using OOP
I have main class plugin for example it will Test<?php if(!class_exists('Test')){ class Test{ var $shortcode; var $action; var $settings; function __construct(){ } function ini(){ $this->load_core(); $this->settings = new Settings(dirname(dirname(__FILE__)).'/settings/settings.php' ); $this->shortcode = new Shortcode($this->settings); $this->action = new Action(); } function load_core(){ $this->load_class(); $this->load_helpers(); } function load_class(){ $dir = dirname(__FILE__).'/../class/'; if (is_dir($dir)) { if ($dh = opendir($dir)) { while ($filename = readdir($dh)) { if (strstr($filename,'.php') ) require_once($dir.$filename); } } else { echo " can't open dir <br>"; } } else { echo $dir.' incorect path to dir<br>'; } } function load_helpers(){ $dir = dirname(__FILE__).'/../helpers/'; if (is_dir($dir)) { if ($dh = opendir($dir)) { while ($filename = readdir($dh)) { if (strstr($filename,'.php') ) require_once($dir.$filename); } } else { echo " can't open dir <br>"; } } else { echo $dir.' incorect path to dir<br>'; } } }//Select_car_brand } ?>
Exists class shortcode in file ShTest.php
<?php if(!class_exists('ShTest')){ class ShTest{ var $settings; var $shortcode; function __construct(&$settings, $shortcode){ $this->settings=$settings; $this->shortcode=$shortcode; $this->ini(); echo "<pre>"; print_r($this); echo "</pre>"; die('end'); } function ini(){ add_shortcode( $this->shortcode, array(&$this, 'handlerTest') ); } function handlerTest(){ echo "<pre>"; print_r($this); echo "</pre>"; // wp_die('end'); ob_start(); ?> <h1>Test shortcode</h1> <?php return ob_get_clean(); } } } ?>
How I should create shortcode and where add add_shortcode action???
in main class something like add_shortcode(‘test’, array($this, ‘handler_function’); ???
Or I can somehow to create it in class ShTest ???When I do in class I can’t pass there settings from main class and seems a few times creates the same class ShTest instead once.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘How to create shortcode in class’ is closed to new replies.