Wordpress Plugin Tutorial Part VIII - The Admin Page

Up to now we have been able to specify a Custom Comment Message on a per-post basis. But setting the comment message per post would be too much of a task. So we will look at how we can specify a default message, so that it will be used if the user did not specify a custom message for the post. Since this default message is common for all posts we will add an admin page where the user will be able to specify the default message.

First of all we have to be able to tell Wordpress to add the our page when the admin dashboard is shown. In order to do this we will hook into the 'admin_menu' action. In order to do this change the constructor of our class to look like the following.
public function __construct()
{
    add_action( 'add_meta_boxes',
                array( &$this, 'add_ccm_meta_box' ) );

    add_action('save_post', 
                array(&$this,'ccm_save_data') ) ;

    add_filter('comment_form_defaults',
               array(&$this, 'edit_cm') ) ;
    //hook into the admin_menu action
    //and run the 'add_ccm_admin_page' function
    add_action('admin_menu', 
               array(&$this, 'add_ccm_admin_page') );
} 
Our 'add_ccm_admin_page' function will look like this.
function add_ccm_admin_page(){
    //This method adds an options menu item and calls the 
    //function 'print_ccm_admin_page' to print the page
    add_options_page('Custom Comment Message Otions',
                     'Custom Comment Message',
                     'manage_options',
                     plugin_basename(__FILE__),
                     array(&$this,'print_ccm_admin_page'));
}


To learn the parameters passed to the function 'add_options_page', take a look at this page on the Wordpress function reference.
Now if you log in to the admin console, you will see that there is a menu item under the 'settings' section like shown on the image on the right. But clicking on the menu-item will not show anything. That is because we did not define the 'print_ccm_admin_page' function that we specified as a callback to the 'add_options_page' function. To do so. add the following function to the 'CustomCommentMessage' class.





function print_ccm_admin_page(){
?>
<div>
  <form method="post" 
        action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
    <h2>Default Custom Comment Message</h2>            
    <textarea cols="100" rows="5" id="ccm_title_reply">
    </textarea>
    <br/>
    <input type="button" 
           value="Save Changes"
           name="update_ccm_options"/>
  </form>
</div>
<?php
}

Now if you click on the menu item on the admin page you will be directed to a page like below.

Next we will see how to save and retrieve the options.

No comments:

Post a Comment