rtCamp notes, day 19 of undefined

Administration menus

Administration menus help us to interact with our admin settings, these are the menus located in the back-end of the wordpress.

Adding custom admin menus to wordpress

We can add custom admin menus to wordpress via our plugin.

  • We need to create a function to add the menu via the add_menu_page.
  • Secondly, we need to hook our function to the action admin_menu so that our menu would be loaded when the admin menu are triggered.
  • We should add the content of our Admin menu passed as a function to the add_menu_page function.
  • Implementation code below
function add_item_to_menu(){
	add_menu_page( 'Test', 'Test menu', 'user', 'home' , 'add_item_to_menu_html', 'dashboard', 20 );
}

add_action('admin_menu', 'add_item_to_menu');
  • In the add_item_to_menu function we need to pass some arguments which are as follows
  • $page_title -> It holds the title for the page we are adding
  • $menu_title -> It holds the title which should be displayed in the admin menu
  • $capability -> It holds the capability which is required for the user to be able to view/interact with this menu
  • $menu_slug -> it holds the slug for this menu which would be used to render it when loading it, ( admin.php?page=home , here home is slug)
  • $callback (optional)-> it holds the callback function which should be called when this menu is accessed. (contains the html content for the menu)
  • $icon_url (optional)-> holds the icon information about the icon that should be displayed in the menu list
  • $position (optional)-> holds the position for the admin menu to be, refer to menu positions to view positions of different menus.

Top level vs sub level menu

The above function we implemented adds a top-level menu, we can also add a sub level menu which means a menu inside a top level menu

Adding a sub level menu

We can add a sub menu to a menu a similar way as we added the menu, only some things are different, for e.g. adding the slug of the parent menu.

  • First we need to create a function which will register our sub menu page with the funciton add_submenu_page
  • We would be adding the function to the hook admin_menu to be loaded when the admin menus are loaded.
  • We can add the content of the sub-menu page to the add_submenu_page function as parameters.
  • Implementation below
function add_sub_menu(){
	add_submenu_page(
		'home', 'Sub menu', 'Sub menu oho', 'user', 'home_sub', function() {
			echo 'test test hello';
		}
	);
}
add_action('admin_menu', 'add_sub_menu');
  • Here the passed arguments are defined below
  • $parent_slug -> it holds the slug for the parent menu to which the submenu must be added.
  • $page_title -> It holds the title for the page we are adding
  • $menu_title -> It holds the title which should be displayed in the admin menu
  • $capability -> It holds the capability which is required for the user to be able to view/interact with this sub-menu
  • $menu_slug -> it holds the slug for this menu which would be used to render it when loading it, ( admin.php?page=home_sub , here home_sub is slug)
  • $callback (optional)-> it holds the callback function which should be called when this sub menu is accessed. (contains the html content for the menu)

Also explored post_meta with implementation and testings after the mid-day question

  • add_post_meta()
    This function accepts 4 params (3 required, 1 optional). add_post_meta( $post_id, $meta_key, $meta_value, $unique = false )
    If the unique value is set to true, then the meta will not be inserted to the database if there already exists data for the same meta key.
    This function cannot be used to update/override any rows, it can only be used to add new rows in the database.
  • update_post_meta
    This function also accepts 4 params (3 required, 1 optional). update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
    If the value is set for the $prev_value field it will only update the rows where meta_key matches the specified and previous meta_value matches the $prev_value.
    If the value for $prev_value is not specified then values will be updated for all the rows where meta_key matches the specified key.
    If the meta_key does not exists already in the database, it will create and add a new row to the database with the same function which add_post_meta() uses i.e. add_metadata()

Both of these functions automatically serializes data when it is sent as an array before storing into the database.

Leave a Reply

Your email address will not be published. Required fields are marked *