#wordpress #wordpresss_plugin #plugin_register_shortcode #plugin_oop_shortcode_register
How to declar add_shortcode in wordpress class file.
Here, we want to display a form with shortcode in out oop stractured plugin class file.
First we crate a plugin form display file like
=> public/class-display-forms.php
class DisplayForms{
private $plugin_name;
private $version;
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_shortcode( 'online_assesment_forms', array( $this, 'display_online_assesment_forms' ));
}
// display the assesment form on homepage
public function display_online_assesment_forms(){
ob_start();
echo 'you html form or code here . which you return with this shortcode..';
$content = ob_get_contents(); ob_end_clean();
return $content;
}
}
Now we call this file at out plugin main file. Here we have a plugin main file which name is abcd-plugin-main.php
Add this file using require
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-display-forms.php'; and define this class new DisplayForms( $this->get_plugin_name(), $this->get_version() );
For Help: https://blog.wplauncher.com/create-wordpress-shortcode/