Français A propos Contact rss
WordPress from the beginning
Sharing some discoveries about WordPress
 

Creating a plugin, part two

This is the sequel to my article creating a plugin. Get ready to learn how to activate the plugin, how to upload content into a page, how to create an admin page for the plugin… But beware! You need to be familiar with php formular management in order to follow this article.

Activation process

If your plugin comes with actions that are performed only once, upon activation, you can ask their implementation by registering two new hooks (including one for the opposite process):

register_activation_hook(__FILE__,'my_plugin_install');
register_deactivation_hook(__FILE__,'my_plugin_uninstall');

Note: it is preferable not to create new tables in the database, as this would make updates difficult. You can use the options instead, to store parameters.

Integrating content into a page

In the previous article I showed you how a plugin interacts with its environment. Here’s a reminder: by creating a function that can be used by other plugins, by modifying Wordpress’ behaviour via filters and actions, by adding a widget to the interface.

Here’s an example of how to inject content into the body of an article, or a page, using the plugin. To do this you need to add a new filter:

add_filter('the_content','my_content_filter');

Then write the following function, which necessarily takes into account a $content variable as parameter, and returns it. Here’s an example that looks for a chain written in the page by the writer :

function my_content_filter($content){// change $contentif( is_page()&amp;&amp;strpos($content,'<!-- my replacement point -->')!==false){$content=str_replace('<!-- my replacement point -->','bla bla ...',$content);}return$content;}

An admin page: first, menus

Now let’s see how to create an admin page.

To begin with, it’s necessary to create a new entry in Wordpress’ menu, usually in “Settings” if one page is enough. The full method is explained here http://codex.wordpress.org/Adding_Administration_Menus. Here’s the code bit we’re interested in:

// new action
add_action('admin_menu','my_plugin_menu');
 
// your function that will add one or more admin menus for your pluginfunction my_plugin_menu(){// this function adds an entry in the "Settings" menu
  add_options_page('My Plugin Options','My Plugin',8,__FILE__,'my_plugin_options');// to see users' levels: http://codex.wordpress.org/User_Levels}
 
// your function which will be activated when the administrator will click on your admin linkfunction my_plugin_options(){echo"admin formular...";}

An admin page, simplified version

In the previous article I wrote about options used to register the plugin’s configuration, for example. The idea is to allow the blog’s admin to change these options thanks to a formular. And believe it or not, but Wordpress has a method to generate these formulars very rapidly and to take care of options update to boot.

You’ll find the code at this page http://codex.wordpress.org/Creating_Options_Pages. For it to work properly, you really need to respect the code provided at the end, i.e the formular’s action and the various hidden fields.

Here’s a cool detail: when you add to your browser the /wp-admin/options.php ending, you’ll see a page that lists all of Wordpress’ options!

A personalised admin page

If the simplified version weren’t enough, remember that you are free to create the formular that best suits you. But you’ll need to retrieve the posted values, do the tests, update options etc. by yourself. Here’s a full example http://codex.wordpress.org/Adding_Administration_Menus.

Among other subtleties you will notice that the formular’s posted as POST at action $_SERVER['REQUEST_URI'] . The developers advise that a hidden field be posted at the same time. It will be used as a test to retrieve data via the $_POST array.

To be continued…

Next step: plugin distribution and promotion

Suggestions de lecture

Spread this post over the world!

Share/Bookmark

How did you like this post?

Please don't go without leaving a mark!
3 vote(s) 1 Etoile2 Etoiles3 Etoiles4 Etoiles5 Etoiles Loading ... Loading ...

Top
Add/Remove Widgets

About this post

  • About this weblog

    "WordPress From the beginning" is written by a French web developer and teacher currently based in Switzerland.

    Originally planned in french, both blog and its functions were developped in this language. This explains why not all functions are available in the English version.

    However, new translated articles are regularly posted for use by the wordpress' web community.

Wordpress from the beginning is a production of Woodymood