Make New Hello World WordPress Plugin

Posted by & filed under , .

WordPress is a sophisticated  Blogging platform and and genuine and popular CMS between web developers.Plugins are on of major part of wordpress that we can use for enhance features and abilities of wordpress. In this lesson I’m going to make a simple hello world plugin.first of all you have to have an idea about fundamentals of php.

1st step:-

first you have to go to the folder that you have installed wordpress.and You have to make a folder in your plugin folder which is situated in  wp-content>plugins .next you have to assign a unique name for that folder.next you have to get in to that folder( i was name it as “new hello world”).Now make a new php file in the folder and  i was name it as new-hello-world.php

2nd Step:-

Open the php file and past this comment on top of your file.this part helpful to recognize the plugin.then save it.

<?php /* Plugin Name: New-Hello-World Plugin URI: http://www.webdesignsrilankablog.com Description: A simple new hello world wordpress plugin Version: 1.0 Author: maya Author URI: http://www.webdesignsrilankablog.com License: GPL */ ?>

3rd Step:-

Go to your WordPress admin>plugin. Now your plugin have  listed.

Plugins ‹ plug — WordPress

we are going to do a simple activity to print “Hello world ” when we call it form theme.

4th Step:-

open your plugin file again and write add_action code below  the commented plugin information area.

<?php
function new_hello_world()
{
echo "Hello World";
}
?>

5thStep:-

Test our Plugin you have to call this new_hello_world function on index.php or header or footer.php or place whatever you like to appear this text.paste this function in appropriate place.


<?php  new_hello_world(); ?>

now it can appear on your front-end,But if you delete or deactivate your plugin it display Fatal error:Call to undefined function.for solve this error you have to replace

<!--?php  new_hello_world(); ?-->

with this

<?php if(function_exists('new_hello_world')) { new_hello_world(); } ?>

6th Step:-

If user wants to change this “Hew Hello World” to “New Text” he have to change plugin file its not a good idea.You have to make a back-end plugin option page to change this “New Hello World “text.Wordpress gives you 4 functions to do this job.four functions are register_activation_hook,register_deactivation_hook,add_option,delete_option.

<?php /* Runs when plugin is activated */ register_activation_hook(__FILE__,'new_hello_world_install'); /* Runs on plugin deactivation*/ register_deactivation_hook( __FILE__, 'new_hello_world_remove' ); function new_hello_world_install() { /* Creates new database field */ add_option("new_hello_world_data", 'Default', '', 'yes'); } function new_hello_world_remove() { /* Deletes the database field */ delete_option('new_hello_world_data'); } ?>

above code make a new database-field through the add_option function .We want to automatically delete this database-field when we deactivate or delete the plugin.then we have to use the delete_option function.

7th Step:-

add these two codes also in your plugin file

<?php if ( is_admin() ){ /* Call the html code */ add_action('admin_menu', 'new_hello_world_admin_menu'); function new_hello_world_admin_menu() { add_options_page('new_Hello World', 'Hello World', 'administrator', 'new_hello-world', 'new_hello_world_html_page'); } } ?>

 

<?php function new_hello_world_html_page() { ?></pre>
<div>
<h2>Hello World Options</h2>
<form action="options.php" method="post"><!--?php wp_nonce_field('update-options'); ?-->
<table width="510">
<tbody>
<tr valign="top">
<th scope="row" width="92">Enter Text</th>
<td width="406"><input id="new_hello_world_data" type="text" name="new_hello_world_data" />value="<!--?php echo get_option('new_hello_world_data'); ?-->" />
(ex. Hello World)</td>
</tr>
</tbody>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="new_hello_world_data" />

<input type="submit" value="<?php _e('Save Changes') ?>" /> </form></div>
<pre>
<?php } ?>

8th Step:-

go to  Wordpress admin view>Settings>Hello World and type some text on Enter Text field and save changes.

new_Hello World ‹ plug — WordPress

Now you have to replace


<?php?>
if(function_exists('new_hello_world')) {
new_hello_world();
}
?>

(which we use in 5th Step) code with this code


<?php echo get_option('new_hello_world_data'); ?>

for display you have given.

Now your plugin is ready to work. :D

Thanks for passing by: ↓



  • Guest

    I bet you can guess the only problem I have with this tutorial is. Your single really long line of code is a little hard to understand which is why there’s syntax.

    So instead of:

    Maybe try: