Sep 03

WordPress: How to: Simple Plugin: Step By Step

  1.  Create a folder with the name of your plugins under /wp-content/plugins/  such as /wp-content/plugins/my-simple-plugin folder
  2. Create a php file under your plugin directory with the name of your plugins such as my-simple-plugin.php i.e /wp-content/plugins/my-simple-plugin/my-simple-plugin.php
  3. On this file, you can provide some information about your plugin; yes in a comment section, usually at the top of the file
  4. sample information block:     /*
    Plugin Name: X
    Plugin URI: http://
    Description: yyy
    Version: 1.0
    Author: your name
    Author URI: http://
    License: GPLv2+
    Text Domain:
    */
  5. you can create a folder structure for your plugin if you will need other files such as : css, images, js, lib, views, includes
  6. you can put an icon under images folder if you want an icon to show up beside the name of your plugin on the navigation menu (provided, you create an entry in the navigation menu)
  7. on the my-simple-plugin.php file, you might want to write code to create an entry in the wordpress navigation menu. You can use both procedural and object oriented approach - your choice. if you use OOP, you can write the navigation entry creation code under the constructor method.
  8. add_action( 'admin_menu', array( $this, 'wpa_add_menu' ));  //under constructor method to say that wpa_add_menu method will be used to add entries in the menu/navigation
  9. you can use add_menu_page, add_submenu_page to add entries and sub-entries. you can use  add_menu_page  method to add an entry called 'My Simple Plugins' and then you can use add_submenu_page  to add pages such as settings, and dashboard.
  10. you can use register_activation_hook( __FILE__, array( $this, 'wpa_install' ) ); to create the activate link/function for your plugin. you can put this line in the constructor method or in yoiur flow
  11. you can use register_deactivation_hook( __FILE__, array( $this, 'wpa_uninstall' ) ); to create deactivate link/function for your plugin.
  12. note: empty wpa_install, and wpa_uninstall functions will be sufficient
  13. you can use the method: wpa_page_file_path to link the sub-navigation entries to corresponding pages (provide the path) for example for settings and dashboard pages
  14. if you want to use custom css pages for your plugins, you can load your own css files using: add_action( 'admin_enqueue_scripts', array( $this, 'wpa_styles') );  //add this line in the constructor
  15. and then define the method below outside the constructor:  public function wpa_styles( $page ) {  wp_enqueue_style( 'wp-analytify-style', plugins_url('css/wp-analytify-style.css', __FILE__));  

 

 

Skip to toolbar
dead