• 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.

    • This topic was modified 6 years, 11 months ago by neo332.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter neo332

    (@neo332)

    I think when we create class shortcode we should do two initialization at first for main class and second in handle function?

    Moderator bcworkz

    (@bcworkz)

    It all depends on program flow. You may not need to instantiate at all, or it may be required. Instantiating the handler class may not be necessary because the shortcode handler method must be a class public static function so that it can be called without instantiating a class object. WP will want to call your handler with something like MyClass::my_shortcode_handler( $atts, $content ); There might be other reasons to instantiate, but not for the static handler method.

    Adding your shortcode with add_shortcode() can be done in any number of ways. It’s more a matter of when than how. Plugin code is processed fairly early, so adding shortcodes for plugins should be deferred until the “init” action fires. Thus the add_shortcode() should be called within a callback to the “init” action (or similar action that’s late enough but not so late that it’s after do_shortcode() is called).

    The “init” callback can be another public static function, or it could be a regular public class method as long as the class is instantiated before the action fires. This begs the question how does one add the “init” action callback? Once again, there’s any number of ways, as long as it’s done before “init” fires. It can be a procedural call, public static, or part of the class __construct() method, provided the class is instantiated before “init” fires.

    Whether you do this as separate classes or not is up to your organizational needs. There is no need for a separate class, but it can be separate if it makes more sense. I advise you to prefix all plugin class and global variable names with a unique string such as your plugin’s initials. Having a class called Action is likely to conflict with other themes or plugins a user might install. If your plugin name was Our Awesome Plugin, prefixing the initials “Oap” to Action — class OapAction is much less likely to conflict.

    I like to call the add_shortcode function in __construct function of the class.

    class PluginExample {
    	public function __construct() {		
    		add_shortcode('say-hello', array($this, 'hello'));
    	}
    	
    	public function hello() {		
    		echo '<h3>Hello World!</h3>';
    	}
    }
    
    $pluginExample = new PluginExample();
    Thread Starter neo332

    (@neo332)

    I like to call the add_shortcode function in __construct function of the class.

    it’s all in one class, if you have big plugin it won’t useful.

    for shortcode really that I used before is not well, will rebuild all without intermediate class

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to create shortcode in class’ is closed to new replies.