WordPress Plugins



  • Can be a single php file in the wp-content/plugins folder.
  • Usually recommended to give its own folder, e.g. /plugin-name/plugin-name.php
  • Minimal required header info:
    * Plugin Name: Plugin Name
  • See WP documentation on creating a full plugin header.
  • All plugin files, variables, and functions should have a unique prefix.
  • Hooks – Occur at strategic points within code to allow one to make modifications to WordPress without editing any core files.
    • Actions – Add or change WP functionality.
    • Filters – Change content as it is loaded.
    • To use one of these you need to create a custom function (“callback”) and register it with WP for a specific hook.
  • register_activation_hook() – Sets up things when the plugin is activated.
    register_activation_hook(  __FILE__, 'plugin_prefix_to_run' );
  • register_deactivation_hook() – Runs when a filter is deactivated, can be used to clear up temporary data, should not be used to uninstall, see below.
    register_deactivation_hook( __FILE__, 'plugin_prefix_to_run');
  • register_uninstall_hook() – Removes things set up when plugin is deleted.
    • Alternative: Create uninstall.php in plugin folder.
      // Make sure this is being called correctly, not by a hacker:
      if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
      $option_name = 'plugin_option_name';
      delete_option( $option_name );
      delete_site_option ( $option_name ); // For multisite
  • register_post_type() – Register with WP CPTs to avoid 404 errors.
  • flush_rewrite_rules() – Clears out and rebuilds permalink rules. Use when activating and deactivating a plugin.
  • __FILE__ – Points to the main plugin file, where the functions should be, if placed in another file, __FILE__ needs to be replaced with appropriate file path.
  • add_action()
    • [php]
      add_action( 'init', 'my_callback_function' );
    • init is the hook, my_callback_function is the function w/the custom functionality I wrote.
    • A priority parameter determines the precedence of this function as opposed to other functions operating on the same hook. Default: 10.
    • Can also register additional parameter outlining how many arguments you want to be returned.
      • [php]
        add_action( 'save_post', 'my_save_post_callback', 10, 2 );
      • [php]
        function my_save_post_callback ( $post_id, $post ) {
        // do stuff
  • add_filter() – Operates similarly to add_action()
    • The value returned by one’s callback function replaces the value passed to it.
  • Make Use of WordPress APIs


  • do_action() – Add hooks within plugin to allow it to be extended.
  • apply_filters() – Best Practice: Use wherever text is printed to screen.
  • remove_action() – Can remove functions that have been added via other plugins, etc.
  • rewrite – An option when creating a CPT. Allows one to determine what permalinks look like for this CPT.
    'rewrite' => array( 'slug' => 'series', 'with_front' => false );
    • with_front – Determines whether any WP wide prefixed structure will be kept (e.g. /blog, /archives).

File Organization

  • plugin-name.php and uninstall.php go in the main plugin-name folder.
  • One can separate out admin code into a separate file if desired:
    if ( is_admin() ) {
    require_once( dirname(__file__) . '/admin/myplugin_admin.php' );
  • Sub-folders of plugin-name may include /admin, /js, /css, /images, /includes, /settings, etc.
  • No need to over complicate if it is a small plugin, leave in minimal files.
  • See Best Practices for information on File Organization, also Architecture Patterns.

Bibliography/Other Resources