Creating a WordPress Plugin Part 1: Adding the Admin Page

WordPress is one of the most popular content management systems on the internet. Created using PHP it can handle all things blogging to commercial websites. In fact we use WordPress for both our blog and website. In this article I’ll show you how to create a WordPress plugin which creates a menu item and corresponding page in the admin console.

Recently I have been spending some quality time with PHP and WordPress to create a plugin which uses our PDF to HTML5 web service to convert content and then display it on a page (more on this later).

Hello World!

Let’s dive right in with a simple plugin that will say “Hello World!” on all the pages.

The header comment here is what the wordpress uses to recognise that this is a plugin, it is only needed for the point of entry of the plugin (other php files get called with include/require_once). At the bare minimum you need to have “Plugin Name” in the header for it to be listed in the plugin subdirectory. Now to save the file, make sure it’s in the sub folder of your wordpress installation “wp-content/plugins”, I suggest placing it into a subdirectory there so that you can organise your plugin.

To turn on the plugin, go to the wordpress admin panel and go to the “Plugins” menu option. Find the new plugin and activate it.

plugin panel

 

Now on every page of the website it will say “Hello world!” at the top left.

hello-world

 

It’s not very practical, but it gives an insight into how WordPress plugins work. WordPress handles plugins in a manner that allows them to be as pluggable as possible. When a plugin is activated, WordPress will run the main php file of the plugin. This means that we need to lock down when our plugin will be active.

Creating a New Admin Panel

Now that we know how to get a plugin recognised by WordPress, we can continue on to creating a proper plugin that does something useful. A good starting point would be to have a menu item on the sidebar, allowing us to have a page in the admin console. This is useful for allowing the admin of a wordpress site configure your plugin.

To do this, we need to hook into the action where the admin menu is loaded. When WordPress loads a page, it runs through a set of actions which we can hook into with the add_action function. In the following code we create a menu item in the admin panel which takes us to a page which says “Hello World!”:

Let’s take a look at the functions:

add_action(‘admin_menu’, ‘test_plugin_setup_menu’);

Here we hook into the point where the menu bar on the admin panel starts loading and tell wordpress that we want to run the function ‘test_plugin_setup_menu’, which is a few lines below.

add_menu_page( ‘Test Plugin Page’, ‘Test Plugin’, ‘manage_options’, ‘test-plugin’, ‘test_init’ );

Here is where we add the menu page and the menu item entry. The first option ‘Test Plugin Page’ is the title of our options page. The second parameter ‘Test Plugin’ is the label for our admin panel. The third parameter determines which users can see the option by limiting access to certain users with certain capabilities. ‘test-plugin’ is the slug which is used to identify the menu. The final parameter ‘test_init’ is the name of the function we want to call when the option is selected, this allows us to add code to output HTML to our page. In this case we just write out “Hello World!”.

admin-menu

Now that we have setup our options page, we have a staging ground to play with some more features of WordPress.

Next time I’ll show you how to use a Web Service with your plugin.

For more information on creating plugins for WordPress, I recommend checking their official Codex, which is very well documented.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Related Posts:

The following two tabs change content below.
Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.
Simon

About Simon Lissack

Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

25 thoughts on “Creating a WordPress Plugin Part 1: Adding the Admin Page

  1. Great tutorial so far!

  2. Andre Allin

    Nice, but what if you want to make exact menu page like Posts but displaying posts from specific category, with all its functionality…is it possible?

    • We did not try this specific functionality but it should be possible to add. We put the code out as a starting point for people to experiment with.

  3. thank sir

  4. thank you
    nice article

  5. Bruno Amadori

    Thank you very much for this, my friend.

  6. navas

    guys i have one question,in this what is test_plug-in?

  7. I am not clear if I understand your question. Test Plugin’ is the label for our admin panel

  8. Thank you Simon. I created my first WordPress Plugin following your article. Merci beacoup.

  9. nice and useful custom plugin.
    thanks for this article

  10. Rein

    how can I put the plugin and its admin on a different php file?
    the admin only show if I put all the codes on one php file

  11. VINAY SINGH

    Great Job Nice Post Thank You..!

  12. Felix

    Nice!! many thanks for this blog.. 🙂

  13. Johnthan

    Thank you for this easy guide I just started learning about wordpress plugin so this is so great I found your page.

  14. avinash

    Nice !! great job……many many thanks for this blog.

  15. neda

    persian:
    بسیار ممنونم . خیلی عالی بود . مرسی 🙂

    very very thanks.

  16. Amit

    Thank you so much for publishing..

  17. Amit

    Is there any other article which will help me to learn more about plugins ? How do I add more functionalities to it..?

  18. Nikunj Vaghasiya

    Your article is really helpful, thanks for writing. I am looking forward to read more articles in future. Keep writing!!!
    Thanks for sharing

  19. Philip Schilling

    Thanks! This made getting started a lot easier!

  20. ak

    Thank you, It is useful

  21. laurens

    any way to call a function directly without add_menu_page(); so when loading, that it shows a page function that you define.

  22. rahul

    thanks… its so useful …but please add submenu page also …

  23. GL

    Hi,
    please excuse my english, so french… 🙁
    I tried to include a php file, it works with a html but with a php, the page doesn’t load…
    An idea ?
    Thanks, have a nice day

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>