最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hide admin menu items if it is empty

programmeradmin0浏览0评论

I have a plugin ( adds a new menu to the admin top bar ) and it works.. There is an option page where the user inputs the menu title into that field (shows as menu item) - then adds the url address in the second field (this is what the menu item is linked to).

Both these input data fields are saved in the options.php as separate options with thier own names - this data is removed if the plugin is deactivated (testing), or is deleted - keeps the DB clean.

This works and I have hardcoded three menus to accept the data from the options tables:

adm_topmenu_one_link
adm_topmenu_one_title

adm_topmenu_two_link
adm_topmenu_two_title

adm_topmenu_three_link
adm_topmenu_three_title

Not super imaginative, but it suffices to make it easier to find and work with.

The problem is that if the menu is empty it still shows (and before you'all say it; I know its hardcoded)

I wish I could make it dynamic, so on the option page you click on a "Add Menu" - something and a new set of fields appear to enter the details *then you could populate it with as many as you like Alas, I'm not that good, so I'm stuck with what I have.

Seeing that the fields are named as above and use an echo in the value that looks at the title option. Can they be hidden if the value is null? and if not, then show?

I have a separate function for each menu item (options page) which is:

function adm_topmenu_callback_function() {
    echo "<fieldset style='border-top:1px solid #d2d2d2; width:100%; display:inline;'>";
    echo "<br>";
    echo '<input name="adm_topmenu_one_title" id="adm_topmenu_insert" type="text" class="code" value="'.get_option('adm_topmenu_one_title').'" placeholder="Add > Title"/>';
    echo " &nbsp; ";
    echo '<input name="adm_topmenu_one_link" id="adm_topmenu_insert" type="text" class="code" value="'.get_option('adm_topmenu_one_link').'" placeholder="Add > URL"/>';
    echo "<br>";
    echo "<br>";
    echo "</fieldset>";

Which in turn echos out to the menu tool bar items array:

$admin_bar->add_menu( array(
    'id'    => 'my-sub-item',
    'parent' => 'wp-101',
    'title' => '<span style="color:'.$GLOBALS["subVC"].'">★</span> '.get_option('adm_topmenu_one_title').' ',
    'href'  => ''.get_option('adm_topmenu_one_link').'',
    'meta'  => array(
        'title' => __(''.get_option('adm_topmenu_one_link').''),
        'target' => '_blank',
        'class' => 'my_menu_item_class'
    ),
));

The three ids for the menus are:

my-sub-item
my-second-sub-item
my-third-sub-item

I added a class "my_menu_item_class" in hopes that this could be the way. Maybe set it to

if null - display:none;
and if (not) null - display:block;

But... again it needs to be checked, somehow... or as I said earlier, setup as a dynamic "add menu item" feature.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论