I have created this custom plugin and after the activation it shows "settings" on the installed plugin.
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
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 = 'Settings';
// add the link to the list
array_unshift($links, $settings_link);
}
return $links;
}
?>
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[] = 'Settings';
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!';
}
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
Related
I need help in renaming a tab menu item under woocommerce tab on wordpress admin. We installed a plugin that appears as a submenu on woocommerce tab. Can anyone please help me on this?
I found this code below to rename a tab menu, but I dont know what is the tabmenu key of it. Or anyone here how to check tab menu key on my current tab menu items?
add_action( 'admin_menu', 'custom_change_admin_label', 99);
function custom_change_admin_label() {
global $menu;
//global $submenu;
$menu[5][0] = 'Articles';
}
Thank you
The first part of the code is to debug, this will show you the menu in detail on the dashboard. (you can remove this afterwards)
The 2nd part in this example changes the 'coupons' label to 'voucher'
It is therefore a matter of adjusting based on the detail
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only.
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label, in this example changes the 'coupons' label to 'voucher'
function custom_change_admin_label() {
global $menu, $submenu;
$submenu['woocommerce'][2][0] = 'Voucher'; // rename 'coupons' label
}
add_action( 'admin_menu', 'custom_change_admin_label' );
I need to detect when user goes to :
Plugins -> Installed Plugins and Appearance -> Themes
in wordpress admin.
Something like :
add_action("core_upgrade_preamble", "action_core_upgrade_preamble")
You have 2 options:
Option 1:
Use the callback $hook, first get the $hook of the page using this:
function load_custom_wp_admin_style($hook) {
var_dump($hook);
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Check your page in the browser you should get the string name, then you can use:
function load_custom_wp_admin_style($hook) {
// Load only on my specific page
if($hook != 'page_hook_i_got') {
return;
}
//DO MY LOGIC
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Option 2:
Use get_current_screen();
add_action( 'current_screen', 'this_screen' );
function this_screen() {
$current_screen = get_current_screen();
if( $current_screen ->id === "widgets" ) {
// Run some code, only on the admin widgets page
}
}
you will need to do a var_dump to find the id of the page, like in option 1, you can find more info here.
I have a lot of CSS scripts in my WordPress plugin which may affect the other WordPress tags, such as form as an example:
.form {
padding:30px;
background-color:#fff;
}
I can change the CSS, but I want to have a script which only allows the stylesheet to show on the plugin page.
So to clear it up, currently the stylesheet <link> is always in the source code of the admin panel, but I want a script which only puts the stylesheet <link> in the source code when the user is on the plugin page.
You can use get_current_screen:
$screen = get_current_screen();
if ( $screen->id == 'your_plugin_page' ) ){
$custom_css = ".form {.....}";
wp_add_inline_style( 'your_main_style_handle', $custom_css );
}
Update 1:
To add a submenu to your admin menu:
add_action( 'admin_menu', 'your_plugin_admin_menu' );
//Add this to register you styles
add_action( 'admin_init', 'your_plugin_admin_init' );
then:
/**
* Register your stylesheet.
*/
function wpdocs_plugin_admin_init() {
wp_register_style('your-style', plugins_url('scripts/jquery-ui.css',__FILE__ ));
wp_register_script( 'jquery-ui', plugins_url('scripts/jquery-ui.js',__FILE__ ));
}
/**
* Register your plugin page and hook stylesheet loading.
*/
function your_plugin_admin_menu() {
$page = add_submenu_page(...., 'your_plugin_manage_menu' );
//Call 'your_plugin_admin_styles' only on the plugin’s options page
add_action( "admin_print_styles-{$page}", 'your_plugin_admin_styles');
}
/**
* Enqueue our stylesheet.
*/
function your_plugin_admin_styles() {
wp_enqueue_style('your-style');
wp_enqueue_script('jquery-ui');
$custom_css = ".form {.....}";
wp_add_inline_style( 'your-style', $custom_css );
}
/**
* Output our admin page.
*/
function your_plugin_manage_menu() {
// ...
}
you can create style-sheet, and create hook for that style-sheet reference. Then you can add WordPress using add_action(), within plugin call, where you like to call that script. So it will be referenced at head of page for on specified page as needed.
Read more on
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
https://developer.wordpress.org/reference/functions/add_action/
I am attempting to create a plugin that uses the setting API to add some options for my user. I currently create a new menu page and then create a setting section. This all works fine but then my callback function isn't being called. Can anyone see why?
function register_my_custom_menu_page() {
add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'menu-slug', 'sandbox_initialize_theme_options' );
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function sandbox_initialize_theme_options() {
// First, we register a section. This is necessary since all future options must belong to one.
add_settings_section(
'general_settings_section', // ID used to identify this section and with which to register options
'Sandbox Options', // Title to be displayed on the administration page
'sandbox_general_options_callback', // Callback used to render the description of the section
'menu-slug' // Page on which to add this section of options
);
} // end sandbox_initialize_theme_options
function sandbox_general_options_callback() {
echo '<p>Select which areas of content you wish to display.</p>';
} // end sandbox_general_options_callback
i have been making a plugin which makes it possible to show a schedule in wordpress, but until now i did not find a good way to show the page in wordress.
until now what i did was check if certain querystring variable was set.
but now i found a way to add virtual pages to wordpress:
https://coderwall.com/p/fwea7g/create-wordpress-virtual-page-on-the-fly
i renamed it to virtualClass and used it like this:
new \helpers\VirtualPage(array(
'slug' => '1002',
'post_title' => 'Fake Page Title',
'post content' => 'This is the fake page content'
));
to tryout, i went to the page: mywordpress.site?page_id=1002
this did work, but when i went to the backend in the menu builder, i wont see this page, i want it to be shown there so my users can add the page to their website.
Is there good tutorial on how to do this? is there a hook i can use to add a section to the menu builder?
Like in the image below: add it to either place of the red arrow
Instead of using a virtual page, you should create a page with your plugin's activation and then overwrite that page's content. You will also want to delete
the page on deactivation. In case you want to protect that page from user deletion, I have included that code as well, but it uses and exit; so it's a bit harsh.
function pluginxyz_activate(){
$title = 'Whatever You Want To Call It';
$new_id = wp_insert_post(array(
'post_title'=>$title,
'post_status'=>'publish',
'post_type'=>'page'
));
update_option('pluginxyz_page_id',$new_id);
}
function pluginxyz_deactivate(){
$old_id = get_option('pluginxyz_page_id');
if($old_id)wp_delete_post($old_id,true);
delete_option('pluginxyz_page_id');
}
function pluginxyz_protect_page($post_id){
$old_id = get_option('pluginxyz_page_id');
if($post_id == $old_id)exit('You can not delete this page');
}
function pluginxyz_use_content($content){
$old_id = get_option('pluginxyz_page_id');
global $post;
if($post->ID == $old_id){
//que up your content
} else {
return $content;
}
}
register_activation_hook(__FILE__,'pluginxyz_activate');
register_deactivation_hook( __FILE__, 'pluginxyz_deactivate');
add_action('wp_trash_post', 'pluginxyz_protect_page', 10, 1);
add_action('before_delete_post', 'pluginxyz_protect_page', 10, 1);
add_filter( 'the_content', 'pluginxyz_use_content' );