You may delay, but time will not.

9月 02, 2009

wordpress plugin, 關於add_menu_page

在用php寫wordpress plugin時,關於add_menu_page的部份。




假設我們要在後台加入可以對plugin做修改的頁面有兩頁 (一頁為新 增事件,一頁為此plugin的config),我們會用下列的方法。

add_menu(‘test’, ‘test’, level, ‘FILE’, ‘add_menu’ );
add_submenu_page(‘FILE’, ‘event’, ‘event’, level, ‘FILE_A’, ‘add_event’);
add_submenu_page(‘FILE’, ‘config’, ‘config’, level, ‘FILE_B’, ‘config’);
通常我們會很直覺的降子寫code(或許只有我~ 哈),後來才發現降子wordpress的後台會變成三頁
就像是
test
test
event
config
這讓我那時候在寫的時候相當傷腦筋,後來又仔細的看了一下wordpress的document才發現它有寫到下頭這行。
In situations where a plugin is creating it’s own top-level menu, the first submenu will normally have the same link title as the top-level menu and hence the link will be duplicated. The duplicate link title can be avoided by calling the add_submenu_page function the first time with the parent and file parameters being given the same value.
所以看了之後應該知道上頭關於加入event頁面的add_submenu中的’FILE’及’FILE_A’應該內容值為相同,才不會像上面一樣多出現了一行test。

1 則留言:

匿名 提到...

add_submenu中的’FILE’及’FILE_A’應該內容值為相同

submenu中的FILE这个参数就好比package。