- Why plugin?
- Plugin is portable and independent for specific functionality.
- The plugin has no issue after theme update.
- Fundamentals
- Customize wp-config.php [Not mandatory if required]
- define('WP_DEBUG', false); -> define('WP_DEBUG', true);
- index.php file isn't mandatory.
- Folder Structure
- Plugin folder name should be unique. Like iftekhar.
- Inside iftekhar (plugin folder) folder create folder_name.php(iftekhar.php).
- Create index.php empty with single comment.
- Class
- Unique name for class.
- Plugin has three basic behavioral options
- Activation
- generate a CPT
- Flush rewrite rules
- register_activation_hook( __FILE__, array( $excellentPT, 'activate' ) );
- Deactivation
- Flush rewrite rules
- register_deactivation_hook( __FILE__, array( $excellentPT, 'deactivate' ) );
- Uninstall
- Delete CPT
- Delete all the plugin data form the db
- Limitation of uninstall hook -> Only a static method or function can be used in an uninstall hook.
- uninstall.php [Predefined file inside plugin folder]
- Path
- __FILE__ Global predefined location that PHP uses.
- plugins_url( '/assets/p_script.js', __FILE__ );
- require_once plugin_dir_path(__FILE__).'inc/abc.php';
- plugin_basename( __FILE__ );
- Shortcode
- Initialization isn't required inside functions.php
- Except functions.php initialization is required.
- Check Everything ok or prevent direct page access (required).
- if ( ! defined( 'ABSPATH' ) ) { die;} or defined( 'ABSPATH' ) or die('text');
- Plugin enqueue
- function enqueue() {
wp_enqueue_style( 'pluginstyle', plugins_url( '/assets/p_style.css', __FILE__ ) );
wp_enqueue_script( 'pluginscript', plugins_url( '/assets/p_script.js', __FILE__ ) );
} - Add Menu //Inside the class
- function register(){add_action, array($this, 'add_admin_menus');}
- public function add_admin_menus(){ add_menu_page( 'Iftekhar Plugin', 'Menu Title', 'manage_options', 'my_plugin', array($this, 'admin_index'), 'dashicons-nametag', 110 );}
- public function admin_index(){require_once plugin_dir_path( __FILE__ ) . 'templates/admin.php';}
- Add Link on installed plugin
- public $plugin;
- function __construct(){
$this->plugin = plugin_basename( __FILE__ );
} - function myFun(){add_filter( "plugin_action_links_$this->plugin", array( $this, 'settings_link' ) );}
- public function settings_link( $links ) {
$settings_link = '<a href="admin.php?page=my_plugin">Settings</a>';
array_push( $links, $settings_link );
return $links;
}
No comments:
Post a Comment