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'));
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0