How to create a new button in WordPress admin menu - wordpress

Please tell me how to create a new button here (at the bottom of the dashboard left-hand navigation): http://prntscr.com/6rlbda
This button only needs to transmit one GET parameter.
Thanks.

You can't add a "button" to the menubar, only a standard link (though you can choose the icon.)
Also your question doesn't specify what you actually want the button (or page it links to) to do, but the standard approach is:
add_action( 'admin_menu', 'register_my_menu_item' );
function register_my_menu_item() {
# the add_action ensures this is only run when admin pages are displayed
add_menu_page( 'Example page', 'Example menu', 'manage_options', 'query-string-parameter', 'my_menu_item');
}
function my_menu_item() {
# your new admin page contents (or behaviour go here)
echo 'Hello World!';
}
(This should ideally go in a plugin, but it can also go in your theme's functions.php)
Documentation: https://codex.wordpress.org/Function_Reference/add_menu_page

You will need to add function in the functions.php in your theme, or in a plugin.
The new button will be showed only when the plugin or the theme where you added the code is active.
Please refer to thiscodex pace.
This is the only proper way to do it, as far as I am aware.

Related

Custom Plugin - Posting data from main page to another page within your plugin

I created a new custom plugin, that adds a new admin menu to show a list of events. When you click any given event, I want to send you to another page in my plugin that will then render information about this event. I can keep it simple and just use a query string parameter, so don't need to do a form POST, but I would be interested in that as well.
There are two pages:
/my-plugin/reservation-management.php
/my-plugin/reservation-management-details.php
My setup in the base page (reservation-management.php):
add_action( 'admin_menu', 'addReservationManagementMenuItem' );
function addReservationManagementMenuItem(){
add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);
}
Inside my function to build out the first screen, I render some clickable links. To simplify:
function reservation_management_building_function(){
if(!current_user_can('manage_options')){
?>
<h2>Clickable Link</h2>
<?php echo("<a href='reservation-management-details.php?id=$id'>Event</a>"); ?>
<?php
}
}
?>
I just simplified the code, removed some loop logic, etc, but it works and renders out in a loop all of the events with a url of reservation-details.php?id=x where x is the unique post id of each event.
The thing is, this just sends me to a page not found. I even tried using things like get_admin_url() etc
I think I'm missing a fundamental step in how a custom plugin can post from one page to another all while still being within wp-admin.
How can I use an href to safely send the admin user to another admin page within my plugin directory?
Thanks!
Has the page been declared in the plugin? It sounds like you'd need to add a submenu page to your plugin as well.
Example (edited):
add_action( 'admin_menu', 'addReservationManagementMenuItem' );
function addReservationManagementMenuItem(){
add_menu_page('Reservation Management', 'Reservation Management', 'manage_options', 'reservation_management_slug', 'reservation_management_building_function','',3);
//add any relative submenu pages here
//you can have as many submenu pages as you need
//please keep in mind that add_submenu_page() requires a different set
//of parameters passed to it
//if you want the submenu page viewed, and your function exists on your
//plugin definition page
add_submenu_page('reservation-management-details.php', 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation_management_details_slug');
//if you don't want your submenu page accessible in the submenu
//use null as the first parameter to remove it's inclusion from the submenu
//specify the php file used
add_submenu_page(null, 'Reservation Management Details', 'Reservation Management Details', 'manage_options', 'reservation-management-details.php');
}
In the above example, you'd be able to post to your new page in your plugin, because you've now defined it. But often times, I find that I don't want anyone being able to get to a page I've designated as '_POST' only, so my first parameter is then set to null.
Edit:
The only thing that I can't remember off the top of my head is what that POST url needs to be. Please let me know if you need assistance with that, and I'll work it out for you.
Please let me know if you have any questions.

WordPress functions.php - Admin html injection and submitting forms

I created a new navigation item on the left for my WP Admin:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'issue_management_slug', 'issue_management_building_function','',3);
}
function issue_management_building_function(){
if(!current_user_can('manage_options')){
}
else {
?>
...
...
So where I have the ellipsis ... is where my HTML begins and I write out some information to the page with various php echo statements to print some data out.
What I would like to do is now give the user the ability to enter in a filter and press submit. This would issue a POST to another page which would receive the post data, run some stuff, and spit out something else to the screen. I was just thinking this would take the user away from the WP-ADMIN area entirely (what I want to do is keep the user all within the right pane so it looks like it's natively happening on WordPress under my new admin area)
Something feels wrong about this approach above where I'm putting tons of html into functions.php - what is the way to create pages for a custom admin section where I can do things like post forms and go to multiple pages?
I was thinking the best solution would be to put an iframe in my injected HTML in functions.php, and then the pages can talk to themselves just like normal behind the scenes in WP-admin.
Could anyone point me in the right direction?
thanks!
Considering the user input/_POST features you'd like to add to this, you may want to consider building this functionality out as your own plugin. I've always kept custom functionality limited to non-user interaction in the functions.php file, but anything further would probably be better fit as it's own plugin.
For example, what if you created a plugin directory named nullhypothesis:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'nullhypothesis/file_to_do_your_bidding.php', 'issue_management_building_function','',3);
}
It's that fourth parameter that in the documentation mentions that you should include the menu_slug, but it doesn't necessarily need to only be a function - it can also be a file you define.
Then, in your file_to_do_your_bidding.php file (within your plugin), you can add whatever _POST functionality you'd need it to. It could also exist as the 'admin' page that the administrator/whoever interacts with.
Was that what you were looking for?

Where do I put Settings API code for my Wordpress Theme

I am trying to set up a settings page for my wordpress theme (developed it in roots.io using Trellis and Sage etc).
I have found a lot of really good documentation on how to use the settings API, for example:
https://wpshout.com/making-an-admin-options-page-with-the-wordpress-settings-api/
or
http://qnimate.com/wordpress-settings-api-a-comprehensive-developers-guide/
and a few others.
So lets say that I want a theme option page, this code should do it:
add_action( 'admin_menu', 'NEW_admin_add_page' );
function NEW_admin_add_page() {
add_options_page(
'Theme settings Page',
'Theme settings',
'manage_options',
'nts',
'nts_options_page'
);
}
Should create a new page, but where within my theme do I add this code to ensure that when you load this theme (or when it loads if that is the right thing) this page turns up.
Found it, just add it to functions.php seems to do the trick :)
Strangely enough it was not until the tenth article I read that someone thought to mention this :)
https://code.tutsplus.com/tutorials/the-wordpress-settings-api-part-2-sections-fields-and-settings--wp-24619

how to add page to wp plugin

i'm creating a back-end WordPress plugin
and i want to add a second page but i don't want the second page to show-up on the side dashboard.
i want it to look like this:
admin.php?page=pageX
i have tried this:
add_submenu_page('', 'Page X', '', 'manage_options', 'page-X', 'pagex_func');
it worked fine with a little problem the title doesn't show up
but i don't think that's the right way to do it.
You should never call add_submenu_page() directly and that's because you are calling the callback function very early even before WordPress has fully loaded. So you should call it like so.
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'tools.php', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
Please do refer to this documentation page on codex: http://codex.wordpress.org/Function_Reference/add_submenu_page#Example
Check out the example section.

On WordPress Dashboard: Change default text for "X Posts" in "Right Now" widget to custom text

I have a customized admin where I have changed posts to "articles" and I changed this text throughout the rest of the site by adding some code to functions.php but can't find a way to alter it in the dashboard widget that, the main default one that is titled "Right Now." I'd like it to say "X Articles" instead of "Posts" here. Anyone know? I'd like to change "X Categories" to a custom taxonomy and eliminate the listings for tags and pages from this space as well.
Any help is greatly appreciated.
Looking at the core, I see the string is hardcoded. Two ways to change it: jQuery or the filter get_text.
You'll want to load it only in that specific page, so use:
add_action( 'load-index.php', 'callback' );
function callback()
{
// add_filter( 'get_text', etc...
# OR
// add_action( 'admin_footer', print jQuery...
}
Here's another example of usage.

Resources