如何为我的自定义插件创建设置页面?

时间:2014-08-29 12:07:43

标签: php wordpress plugins wordpress-plugin

我创建了这个自定义插件,激活后它会在已安装的插件上显示“设置”。

see the screen shot

我不想在管理面板菜单中的任何位置显示我的设置页面LINK,但是当用户点击插件的设置时(请参阅上面的屏幕截图)应该转到插件设置页。我知道我必须在我的第二个函数“our_plugin_action_links”的变量$settings_link see 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;
}

?>

2 个答案:

答案 0 :(得分:2)

您可以在此网站上试用生成器。

http://wpsettingsapi.jeroensormani.com/

它的作用是在您设置选项时进行回调功能。所以不是一个单独的文件,而只是你的functions.php或插件文件。

或结帐代码http://codex.wordpress.org/Creating_Options_Pages

答案 1 :(得分:2)

首先,从用户的角度来看,我不建议这样做。如果您的插件需要设置页面,请将其放在下面的设置中。如果您不信任您的用户,则不要允许设置或使用功能将菜单限制为仅限某些用户。

但是,如果您对此有特定需求,那么最简单的方法是使用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!'; } ,而下面的代码就是这样做的。您需要将常量更改为更具体的代码(或切换到全局变量或只使用字符串)。有关详细信息,请参阅代码注释。

{{1}}
相关问题