Create WordPress Plugin

To create a plugin, all you need to do is create a folder and then create a single file. First go to the wp-content/plugins folder, and create a new folder named myPlugin. Inside this new folder, create a file named myPlugin.php. Open the file in a text editor, and paste the following information in it:

<?php
   /*
   Plugin Name: myPlugin Creator
   Plugin URI: http://www.nikolaynikolov.net
   Description: My fist plugin
   Version: 1.2
   Author: Nikolay Nikolov
   Author URI: http://www.nikolaynikolov.net
   License: GPL2
   */

You can go into the back end to activate your plugin. Of course, this plugin doesn’t do anything, but you can add functionality by yourself.

Now we will show 3 methods to display new plugin in back end.

/* ADD FUNCTION TO ADMIN MENU */
add_action( 'admin_menu', 'my_plugin_menu' );

function my_plugin_menu() {
    /* 1 - Add sub menu page to the Settings menu */
    add_options_page( 'Page Title', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_function' );
    /* 2 - CREATE PLUGIN IN SEPARATE MENU */
    add_menu_page( 'Page Title', 'My Plugin 2', 'manage_options', 'my-unique-identifier2', 'my_plugin_function' );
    /* 3 - Add submenu page to my-unique-identifier2(which is myPlugin) */
    add_submenu_page( 'my-unique-identifier2', 'Page Title', 'My Plugin 3', 'manage_options', 'my-submenu-identifier', 'my_plugin_function');
}
    
function my_plugin_function() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    echo '<div class="wrap"><p>Here is where the form would go if I actually had options.</p></div>';
}

Preferable method is 2 – CREATE PLUGIN IN SEPARATE MENU. All three methods call the same function “my_plugin_function” to execute.

There is also option to place new plugin in already existing admin tabs:
add_management_page – Adds a menu to the Tools tab. [tools.php]
add_options_page – Adds a menu to the Settings tab. [options-general.php]
add_theme_page – Adds a menu to the Appearance tab. [themes.php]
add_plugins_page – Adds a menu to the Plugins tab. [plugins.php]
add_users_page – Adds a menu to the Users tab. [users.php or profile.php]
add_dashboard_page – Adds a menu to the Dashboard tab (topmost). [index.php]
add_posts_page – Adds a menu to the Posts tab. [edit.php]
add_media_page – Adds a menu to the Media tab. [upload.php]
add_links_page – Adds a menu to the Links tab. [link-manager.php]
add_pages_page – Adds a menu to the Pages tab. [edit.php?post_type=page]
add_comments_page – Adds a menu to the Comments tab. [edit-comments.php]

So now you are ready to add some functionality …

Working demo with functionality is uploaded here. In order to work you need to extract it to your plugins folder and activate it in back end. The plugin replaces one word by choosing with another in all posts.

Leave a Reply

Your email address will not be published. Required fields are marked *