Riffaz Starr
Riffaz Starr

Reputation: 611

How to create a setting page for my custom plugin?

I have created this custom plugin and after the activation it shows "settings" on the installed plugin.

see the screen shot

I do not want to show my setting page LINK anywhere in the the admin panel menu but when user clicks the setting of the plugin (see the above screen shot) that should go to the plugin setting page. I understand that I have to do something on my second function "our_plugin_action_links" 's variable $settings_link see screen shot

I just tried to link a single php file that placed on the admin folder. Then it goes to that php file when clicks. But I want to show the plugin setting page inside the admin panel same like other pages such as add post or **general setting page when click setting of the plugin but not showing the links or menus at the admin menu for the plugin setting.

How can I do that?

my plugin code

<?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;
}

?>

Upvotes: 0

Views: 3594

Answers (2)

Chris Haas
Chris Haas

Reputation: 55457

First off, from a user's perspective I wouldn't recommend doing this. If your plugin warrants a settings page then put it where settings below. If you don't trust your users then don't allow the settings OR use capabilities to restrict your menu to only certain users.

However, if you've got a specific need for this then the easiest way is to just register a normal options page using add_options_page and then manually rip your menu out of the global array. This method ensures that permissions are accounted for correctly and is pretty safe.

Also, instead of plugin_action_links you can use the filter that is specific to your plugin via 'plugin_action_links_' . plugin_basename( __FILE__ ) which is what the code below does. You'll need to change the constant to something more specific to your code (or switch to a global variable or just use strings). See the code comments for more details.

//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!';
}

Upvotes: 2

TeeDeJee
TeeDeJee

Reputation: 3741

You can try the generator on this site.

http://wpsettingsapi.jeroensormani.com/

What it does is you make a callback function where you set your options. So not a seperate file but just your functions.php or plugin file.

Or checkout the codex http://codex.wordpress.org/Creating_Options_Pages

Upvotes: 2

Related Questions