To develop a Custom Shortcode based WordPress plugin first you need to prepare the folder structure as shown in below.
Shortcode WordPress plugin Code Folder
Here core-init.php you need to define file path (like JS or CSS files). Then using a public function make it accessible from sidebar-widgets.php.
if (!defined('WPINC')) { die; }
define('ITSW_CORE_INC',dirname( __FILE__ ).'/assets/includes/');
define('ITSW_CORE_CSS',plugins_url('assets/css/', __FILE__ ));
define('ITSW_CORE_JS',plugins_url('assets/js/', __FILE__ ));
function itsw_register_core_css(){
wp_enqueue_style('itsw-core', ITSW_CORE_CSS . 'jhp_main.css',null,time(),'all');
};
add_action('wp_enqueue_scripts', 'itsw_register_core_css');
add_action('admin_enqueue_scripts', 'itsw_register_core_css');
/*
* Register JS/Jquery Ready
*/
function itsw_register_core_js(){
// Register Core Plugin JS
wp_enqueue_script('itsw-core', ITSW_CORE_JS . 'jhp-core.js',null,time(),true);
};
add_action('wp_enqueue_scripts', 'itsw_register_core_js');
add_action('admin_enqueue_scripts', 'itsw_register_core_js');
/*
* Includes
*/
// Load the Shortcodes
if ( file_exists(ITSW_CORE_INC . 'jhp-shortcodes.php')) {
require_once ITSW_CORE_INC . 'jhp-shortcodes.php';
}
As per WordPress assets folder is mandatory. Where you can Store CSS and JS files.
To Store wp global variables use sidebar-widgets.php file as shown in the Folder structure. Sample Codes shared below.
if (!defined('WPINC')) { die; }
// Let's Initialize Everything
if (file_exists(plugin_dir_path( __FILE__ ) . 'core-init.php')) {
require_once(plugin_dir_path( __FILE__ ) . 'core-init.php');
if (get_option('itsw_how_many_records') === false ) {
update_option('itsw_how_many_records', 20);
}
if (get_option('itsw_height_of_i20_sidebar') === false ) {
update_option('itsw_height_of_i20_sidebar', 520);
}
if (get_option('itsw_dropdown_border') === false ) {
update_option('itsw_dropdown_border', 1);
}
if (get_option('itsw_dropdown_image') === false ) {
update_option('itsw_dropdown_image', 1);
}
ob_end_clean();
}
Here I am using ob_end_clean(); for removing unwanted white-spaces. In this kinds of variables itsw_how_many_records I am storing records count and in itsw_height_of_i20_sidebar storing height in pixels.
/assets/includes/jhp-shortcodes.php this is the main file which used to place HTML and functionalities. Here you need to create your shortcode function.
add_action( 'init', 'itsw_register_shortcodes');
if (!function_exists('itsw_register_shortcodes')) {
function itsw_register_shortcodes(){
// Registered Shortcodes
add_shortcode ('i20_Sidebar_Widgets', 'itsw_plugin_form_display');
};
}
Hope I resolved your Query. For further reference you can Download the Sample Codes.