2014-08-29 157 views
0

我已经创建了这个自定义插件,并且在激活之后它会在安装的插件上显示“设置”。如何为我的自定义插件创建设置页面?

see the screen shot

我不想在任何地方展示我的设置页面链接的管理面板菜单,但是当用户点击设置插件的(见上面的屏幕截图)应该去插件设置页面。我明白我必须对第二个函数“our_plugin_action_links”的变量做些什么$settings_linksee screen shot

我只是试图链接放在admin文件夹中的单个php文件。然后当点击时它会进入该php文件。但我想显示管理面板中的插件设置页面,就像其他页面一样,如添加帖子或**常规设置页面点击设置插件但未显示管理菜单中的链接或菜单为插件设置。

我该怎么做?

我的插件代码

<?php 
/* 
Plugin Name: admin menu remover 
Description: Remove the admin menus just by a single plugin installation 
Version: 1.0 
Author: Author 
Author URI: http://URI_Of_The_Plugin_Author 
License: A "Slug" license name e.g. GPL2 
*/ 

/* This function and action removes following menu item from the admin panel */ 
add_action('admin_menu', 'remove_links_menu'); 
function remove_links_menu() { 
    remove_menu_page('index.php'); // Dashboard  
    //remove_menu_page('edit-comments.php'); // Comments 
    remove_menu_page('themes.php'); // Appearance 
    //remove_menu_page('plugins.php'); // Plugins  
    //remove_menu_page('tools.php'); // Tools 
    //remove_menu_page('options-general.php'); // Settings 
    //remove_menu_page('users.php'); // Users 
} 


/* This function and filter append "setting" immediately after the "admin menu remover" plugin activation */ 
add_filter('plugin_action_links', 'our_plugin_action_links', 10, 2); 
function our_plugin_action_links($links, $file) { 
    static $this_plugin; 

    if (!$this_plugin) { 
     $this_plugin = plugin_basename(__FILE__); 
    } 

    // check to make sure we are on the correct plugin 
    if ($file == $this_plugin) { 
     // the anchor tag and href to the URL we want. For a "Settings" link, this needs to be the url of your settings page 
     $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=font-uploader.php">Settings</a>'; 
     // add the link to the list 
     array_unshift($links, $settings_link); 
    } 

    return $links; 
} 

?> 

回答

1

首先,从用户的角度来看,我不建议这样做。如果你的插件保证设置页面,然后把它放在下面的设置。如果你不信任你的用户,那么不要允许设置或使用功能来限制你的菜单仅限于某些用户。

但是,如果您对此有特定需求,那么最简单的方法是使用add_options_page注册一个正常选项页,然后手动将菜单从全局数组中取出。这种方法可以确保正确解释权限并且非常安全。

此外,而不是plugin_action_links你可以通过'plugin_action_links_' . plugin_basename(__FILE__)这是下面的代码使用特定于您的插件的过滤器。您需要将常量更改为更具体的代码(或切换到全局变量或仅使用字符串)。查看代码评论以获取更多详细信息。

//We'll key on the slug for the settings page so set it here so it can be used in various places 
define('MY_PLUGIN_SLUG', 'my-plugin-slug'); 

//Register a callback for our specific plugin's actions 
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'my_plugin_action_links'); 
function my_plugin_action_links($links) 
{ 
    $links[] = '<a href="'. menu_page_url(MY_PLUGIN_SLUG, false) .'">Settings</a>'; 
    return $links; 
} 

//Create a normal admin menu 
add_action('admin_menu', 'register_settings'); 
function register_settings() 
{ 
    add_options_page('My Plugin Settings', 'My Plugin Settings', 'manage_options', MY_PLUGIN_SLUG, 'my_plugin_settings_page'); 

    //We just want to URL to be valid so now we're going to remove the item from the menu 
    //The code below walks the global menu and removes our specific item by its slug 
    global $submenu; 
    if(array_key_exists('options-general.php' , $submenu)) 
    { 
     foreach($submenu['options-general.php'] as $k => $v) 
     { 
      if(MY_PLUGIN_SLUG === $v[2]) 
      { 
       unset($submenu['options-general.php'][$k]); 
      } 
     } 
    } 
} 

//This is our plugins settings page 
function my_plugin_settings_page() 
{ 
    echo 'Hello from my plugin!'; 
} 
+0

完美..谢谢@chris – 2014-08-30 07:11:42

相关问题