create admin menu in wordpress

Lets learn how to create admin menu in wordpress. Before we move ahead, you need to review my previous blog which is
, you must be wondering why, because in previous blog I have created one table which we are referring in this blog; sorry for the back and forth.

Step 1: Copy the code that we have used for creating tables, On the top of current script. so that it will create one table if its not there.

/* Create table when plugin is activated and will be removed once dactivated - end */
// Create menu for Admin
// DEF: add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position )
// DEF: add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
add_action('admin_menu','srv_admin_menu');
function srv_admin_menu() {
	add_menu_page(
		"Menu Tutorial",
		"phptechi Menu",
		8,
		__FILE__,
		"srv_admin_menu_list",
		SRV_URL."/images/menu.gif"
	);
	add_submenu_page(__FILE__,'Second Menu','Second Menu','8','list-site','srv_admin_list_site');
}
function srv_admin_menu_list()
{
	echo "Now i know how to create a plugin in wordpress!";
}
// function for the site listing
function srv_admin_list_site()
{
	 include 'admin-listing.php';
}

Step 2: Now create one more file “admin-listing.php” inside the plugin folder “phptechi” and copy an code written bellow inside the “admin-listing.php” file.

$columns = array(
		'name' => 'Name',
		'website' => 'Website',
		'description' => 'Description'
	);
register_column_headers('srv-list-site', $columns);
echo "

" . __( 'Site Listing' ) . ""; print_column_headers('srv-list-site'); print_column_headers('srv-list-site', false); global $wpdb; $sql = "SELECT *FROM ".SRV_TABLE_PREFIX."tutorial where 1"; $results = $wpdb->get_results($sql); if(count($results) > 0) { foreach($results as $result) { echo ""; } }
".$result-> name."".$result->website."".$result->description."

Share this nice post: