2014-08-29 186 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

相關問題