• Hello, my shortcode is not working where the code is in my custom plugin.

    See the below code:

    <?php
    class Ajax_Activity {
      function __construct() {
        $this->cld_add_custom_shortcode();
      }
      public function cld_add_custom_shortcode(){
        add_shortcode('sc', [$this, 'cld_generate_form']);
      }
      public function cld_generate_form(){
        $form = '
          <form id="green_form" action="">
            field1:<br>
            <input type="text" name="field1" value="">
            <br>
            field2:<br>
            <input type="text" name="field2" value="">
            <br>
            <input type="submit" value="Submit">          
          </form>';
        return $form;
      }
    }

    Note: add_shortcode line is in a inner php file in my custom plugin.
    I include this class by below way:

    <?php 
    class Admin_Classes_Loader {
      function __construct() {
        require_once CA_FEEDER_DIR_PATH . '/admin/Ajax_Activity.php';
        new Ajax_Activity(); 
      }
    }

    And this class loader is invoked in main plugin file.

    • This topic was modified 3 years, 8 months ago by ashique12009.
Viewing 3 replies - 1 through 3 (of 3 total)
  • To be honest, manually reviewing I can’t see anything obvious.

    So I would at this stage revert to xdebug set some break points and see that everything is actually getting called.

    Not sure if you care, but WP has it own coding standards, whilst there is no compulsion to use them in plugins ( private or public ) many of them make sense
    1. class file names Ajax_Activity class file would be class-ajax-activity.php
    one reason is not all file systems are good with upper / lower cases and underscores
    2. WP coding standards prefer array() rather than [] for readability – it is useful to add codesniffer / WP coding standards to your IDE if you are interested

    • This reply was modified 3 years, 8 months ago by Alan Fuller.
    Moderator bcworkz

    (@bcworkz)

    The Ajax_Activity class works fine on my site. I didn’t bother breaking the classes into separate files since our file systems differ anyway. Do you have WP_DEBUG defined as true? Or did you check the error log for clues? If you’re sure Ajax_Activity is properly instantiating, maybe the issue is where you placed the actual “[sc]” on the page. Some content on a page isn’t filtered through do_shortcode(), as might be the case with certain widget content. When that’s the case “[sc]” would appear as plaintext.

    Thread Starter ashique12009

    (@ashique12009)

    Thanks I got the issue, I made a condition is_admin to shortcode class instantiation.
    That is why my shortcode was not working.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode is not working’ is closed to new replies.