I need to create a shortcode in a plugin, when a person gets to install my plugin can he use shortcode inside the plugin :
that's my code plugin is working 100% but when I call to shortcode [splite] nothing returned
class aligoPlugin
{
public function __construct()
{
// hooks
add_action( 'init', array($this,'register_shortcodes') );
}
// activate
public function activate()
{
$this->splite_article();
// add_shortcode('splite', 'splite_article');
// flush rewrite rules
flush_rewrite_rules();
}
// functions
function register_shortcodes(){
add_shortcode('splite', 'splite_article');
}
// shortCodes
function splite_article()
{
$content = "hhhhh";
echo $content;
}
}
if (class_exists('aligoPlugin'))
$aligo = new aligoPlugin();
// activation
register_activation_hook(__FILE__, array($aligo,'activate'));
I need to create a shortcode in a plugin, when a person gets to install my plugin can he use shortcode inside the plugin :
that's my code plugin is working 100% but when I call to shortcode [splite] nothing returned
class aligoPlugin
{
public function __construct()
{
// hooks
add_action( 'init', array($this,'register_shortcodes') );
}
// activate
public function activate()
{
$this->splite_article();
// add_shortcode('splite', 'splite_article');
// flush rewrite rules
flush_rewrite_rules();
}
// functions
function register_shortcodes(){
add_shortcode('splite', 'splite_article');
}
// shortCodes
function splite_article()
{
$content = "hhhhh";
echo $content;
}
}
if (class_exists('aligoPlugin'))
$aligo = new aligoPlugin();
// activation
register_activation_hook(__FILE__, array($aligo,'activate'));
Share
Improve this question
asked Oct 3, 2020 at 11:54
AlhianeAlhiane
34 bronze badges
1 Answer
Reset to default 1There are 2 major bugs here
Problem 1: Your shortcode registration is invalid
You should be seeing lots of messages about this in your PHP error log
add_shortcode('splite', 'splite_article');
There is no function named splite_article
, there's a class function but the code didn't tell it to use it. So instead of calling your shortcode function, it generates a PHP error message and puts it in the log.
add_shortcode
expects a PHP callable value, so splite_article
is equivalent to splite_article()
, but you need $this->splite_article()
.
So instead use the appropriate callable [ $this, 'splite_article' ]
Problem 2: Shortcodes do not echo
echo $content;
That's not how shortcodes work, shortcode callbacks do not echo HTML, they return it. This shortcode will be breaking a lot of things as a result of this.
Luckily the solution is simple, return
the shortcodes content instead.