gdfgdfg
gdfgdfg

Reputation: 3566

add_shortcode in a plugin

I have a problem adding a shortcode in a custom plugin. The custom plugin code is:

<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
  Plugin Name: ......
  Plugin URI: ..........
  description:
     ..........
  Version: 1.2
 Author: .............
 Author URI: ...........
 License: GPL2
*/



function sdsdsd_my_shortcode(){
    return '9999999999999999';
}

add_action( 'init', 'sdsdsd_add_shortcode' );

function sdsdsd_add_shortcode() {
    add_shortcode( 'sdsdsd_my_shortcode', 'sdsdsd_my_shortcode' )
}

The in WP admin edit post: [sdsdsd_my_shortcode], but instead the content, it renders exactly [sdsdsd_my_shortcode]

The plugin is registered and activated in WP admin/plugins.

The same code works fine in functions.php in the child theme. The plugin is plugins/my-custom-plugin/my-custom-plugin.php

In the plugin file, it doesn't work and without add_action( 'init',...

Upvotes: 0

Views: 327

Answers (1)

Krishan Kaushik
Krishan Kaushik

Reputation: 52

Try This --

if ( ! defined( 'ABSPATH' ) ) {
    die( 'You are not allowed to call this page directly.' );
}

if ( ! class_exists( 'Class_Name' ) ) {

    class Class_Name{

        public function __construct() { 
            
            $this->register_hooks();
            
        }

        function register_hooks() {

            add_shortcode( 'sdsdsd_my_shortcode', array($this, 'sdsdsd_shortcode_content') );
        }

        function sdsdsd_shortcode_content() {

            ob_start();
            //Content


            <?php 
            $form = ob_get_contents();
            ob_clean();
            return $form;
        }

    }

return new Class_Name();
}

Upvotes: 1

Related Questions