add menu item to the dashboard menu of wordpress - wordpress

I am trying the following syntax to add menu-item on dashboard:
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
But, I am confused where to add this code.
Whether to add it in my theme's functions.php or my plugin's functions.php ?
Or any other way to add custom menu-item on the dashboard of the wordpress logged in as admin ?
I want to add custom menu item like the following image:

What you see in the screenshot is the screen of a Custom Post Type. In the documentation you will find an example code on how to add such a screen.
About where to place the code - it depends. Do you want to be able to use this Custom Post Type in other themes, or you will only need it in this theme?
If you want to use it in other themes as well, put the code in your plugin's code.
If you want to use it only in this theme, put it in your theme's functions.php.
And in case you still want to add a custom menu page, here you will find examples on what is the proper way to use this function. What you should note, is that the call to add_menu_page() should be done inside a function that is run on the admin_menu action.
Here's an example working code with WP 3.4.2
function register_custom_menu_page() {
add_menu_page('custom menu title', 'custom menu', 'add_users', 'custompage', '_custom_menu_page', null, 6);
}
add_action('admin_menu', 'register_custom_menu_page');
function _custom_menu_page(){
echo "Admin Page Test";
}

This is a perfect answer.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}

Related

How do I create a shortcode for a WordPress sidebar?

I have added a custom sidebar to my WordPress installation using the following script that also creates a shortcode:
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => 'abeng',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
add_shortcode( 'add_sidebar', 'custom_sidebars' );
When I add the [add_sidebar] shortcode to a Gutenberg block the sidebar does not appear. However, when I use a plugin called Sidebar Shortcode [sidebar id="gutenbar"] the sidebar is displayed perfectly. I assume that my shortcode is associated with a specific function and does not need a name or id for the sidebar to be chosen.
I had been thinking that because the sidebar was added using a plugin that allows the insertion of functions available site-wide rather than only in a theme using its functions.php, that was the reason the shortcode did not do its job. But since the sidebar shows in the widgets area and allowed me to add widgets, and now works with this plugin, that's obviously not the issue.
Looking under the hood of the concisely written Sidebar Shortcode, I can't see what I might want to add to make my code functional.
Why am I doing this? I'm using full-width pages for my posts, disabling the theme's default right sidebar (which doesn't play well in a responsive design) but still need to reinsert the sidebar in a column.
Thanks for any help.
With the help of Prashant Singh on the WordPress support forum, I learned that the function that creates the sidebar cannot be used to create a shortcode to place it on a page. The shortcode has to be created calling the WP native function dynamic_sidebar() that gives access to all sidebars (https://developer.wordpress.org/reference/functions/dynamic_sidebar/). Below is the full script that includes cleanup code to allow the correct positioning inside a page.
/**
* Create sidebar and shortcode
*/
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => '',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
/* Add the shortcode and allow positioning in page*/
add_shortcode( 'add_sidebar', 'my_custom_sidebar' );
function my_custom_sidebar(){
ob_start();
dynamic_sidebar( 'gutenbar');
$sidebar_left = ob_get_clean();
$html = ' <div class="sidebar-content"> ' . $sidebar_left . ' </div> ';
return $html;
}

Custom button in Wordpress Dashboard

We would like to create a custom button in the wordpress dashboard,
We want this button to hold our pages we create for clients,
Because I want to keep the pages section separate from our "Client" pages
i hope this makes sense
It's really quite straight forward, just takes a bit of getting used to.
You ideally need to follow the docs:
https://codex.wordpress.org/Adding_Administration_Menus
https://developer.wordpress.org/reference/functions/add_menu_page/
This will guide you in how to set up an admin menu.
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
Will be the code you call when creating the admin sidebar menu item, however, there are more functions for create sub menu pages under it etc.
What I think you are looking for though, is a Custom Post Type for your clients...
https://codex.wordpress.org/Post_Types
function create_post_type() {
register_post_type( 'client_posttype',
array(
'labels' => array(
'name' => __( 'Clients' ),
'singular_name' => __( 'Client' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'create_post_type' );
Something similar to above will get you started.
If it's all a bit much, you can generate CPT code:
https://generatewp.com/post-type/
Then have a look at the generated code to work out what it is that you would have coded yourself.
Test this :)
class options_page {
function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
function admin_menu() {
add_menu_page( 'Clients', 'Clients Page', 'edit_posts', 'clients_page', 'my_clients', '', 24);
}
function settings_page() {
echo 'This is the page content';
}
}
new options_page;

WordPress menu add link to plugins

When the admin is logged in and is in front end area, there is a small dropdown of the quick menus. How can I add link to plugins page here?
function my_tweaked_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'id' => 'plugins',
'title' => 'Plugins',
'href' => get_site_url().'/wp-admin/plugins.php',
'parent'=> 'site-name'
));
}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' );
Just paste this code in functions.php in your current theme's directory.
For more information check WordPress Codex:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_before_admin_bar_render

wordpress, custom menu is created but does not show up in the top head part in the admin panel

My theme has several custom menus. I can manage them in the WP admin panel.
But look at the picture. You see 2 menus already created - Footer Menu and Header. But in my case those 2 names do not appear so I cannot do anything with them later.
Code I used:
add_action( 'init', 'menus_all' );
function menus_all() { register_nav_menus( array(
'menu1' => _( 'menu1 loc'),
'menu2' => _( 'menu2 loc'),
) ); }
How to make the menu names appear ?
register_nav_menu goes straight in your functions.php file, without any hook:
register_nav_menus( array(
'menu1' => _( 'menu1 loc'),
'menu2' => _( 'menu2 loc'),
));

How can i use use add_plugins_page() and order it on top of the installed plugins sub-menu

I am using the function add_menu_page
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
and i need to put this in order.
I am making a more in depth plugin management page and i was hoping that i could list it under the plugin menu but above the other sub-menu options.
the current sub-menu order is
1.installed plugins
2.add new
3.editor
I want my plugin to be number 1 and shift all other down one level.
Any help is much appreciated! :)
Just like you have add_menu_page, there is also add_submenu_page which you can use.
Codex: http://codex.wordpress.org/Function_Reference/add_submenu_page
Just add the position by number to the end of your file.
add_menu_page( 'page title', 'menu title', 'manage_options', 'link-to-admin', '', plugins_url('/images/icon.png', __FILE__) 4);
In the case above, the menu title will be in the 4th position.
I know you asked this a long time ago, but there were no proper answers. Hope this helps you or others - PBM
function adjust_plugin_menu() {
// remove the existing plugin menu items
remove_submenu_page( 'plugins.php', 'plugins.php' ); // Plugins - Installed Plugins
remove_submenu_page( 'plugins.php', 'plugin-install.php' ); // Plugins - Add New Plugins
remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor
// add in your custom menu item
$destination = "path/to/your/plugin/code" ;
add_plugins_page (
'My Plugin Stuff',
'My Plugin Stuff',
'manage_options',
$destination );
// Put the other menu items back in
add_plugins_page('Installed Plugins', 'Installed Plugins', 'manage_options', 'plugins.php');
add_plugins_page('Add New', 'Add New', 'manage_options', 'plugin-install.php');
add_plugins_page('Editor', 'Editor', 'manage_options', 'plugin-editor.php');
}
add_action('admin_menu', 'adjust_plugin_menu');

Resources