WordPress Plugin Tutorial - Hello World Continue

Posted on May 22nd, 2008

Training

Last week we started building a very basic Hello World WordPress plugin. This week we will expand on that tutorial by adding the ability to change some of the content in the admin area of WordPress.

To being open the helloword.php file that we used last week.

Modify your hello_world function to look like this…

function hello_world() {
        $hello = get_option(’hello_global’);
        Print(”$hello”);
}

The above change allows for a variable to be set in the admin area. Next we will need to add functions so that we can add an admin setting page. Add the following…

function modify_menu() {
        add_options_page(
                ‘Hello World’,
                ‘Hello World’,
                ‘manage_options’,
                _File_,
                ‘admin_hello_options’
        );
}

add_action(’admin_menu’,'modify_menu’);

The above code will add a page to the admin area but it will be blank. Next add the following code…

function admin_hello_options(){
Print(”<h2>Hello World Plugin</h2>”);
if ($_REQUEST['submit']){
        upate_hello_options();
}
print_hello_form();
}

The above code creates the admin settings page. Now we need to add content…

function upate_hello_options(){
if ($_REQUEST['hello_global']) {
update_option(’hello_global’,$_REQUEST['hello_global']);
}
}

function print_hello_form() {
$default_greeting = get_option(’hello_global’);
Print (”<form method=’post’><label for=’hello_global’>Greeting:
<input type=’text’ name=’hello_global’ value=’$default_greeting’>
</label>
<br />
<input type=’submit’ name=’submit’ value=’Submit’ >
</form>”);
}

Be sure to upload these changes to your plugin folder. You may need to deactivate and activate again. You will then be able to enter your own text which will show up on your main page.

If you get stuck let me know in the comments.

Posted in Development | 424 views


Related Topics:
WordPress Plugin Tutorial - Hello World
WordPress Plugin Requests
WordPress Plugin Requests
PHP Tutorial - Part 3 of 5
WordPress Plugin Requests

RSS feed

Comments

No comments yet.

Sorry, the comment form is closed at this time.

© 2007 BlueFur Hosting | Privacy Policy
Theme by Unique Blog Designs