how to add page to wp plugin - wordpress

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.

Related

Wordpress plugin menu issue

I am building a wordpress plugin with the boilerplate framework (https://wppb.me/). That all works fine. I have an annoying issues which I cannot solve.
the boilerplate makes per object one php file. In this php file all funtionality for the object is arranged. Pages, lists, validations etc.
Example in the file for contact there is a method contactlog_menu:
Object: contact
public function contactlog_menu()
{
add_submenu_page('plugin-options', __('plugin', 'plugin'), __('am_contactlogs', 'plugin'), 'manage_options', 'contactlogs', array($this, 'plugin_contactlogs_page_handler'));
add_submenu_page('contactlogs', __('New contactlog', 'plugin'), __('Add new b', 'plugin'), 'activate_plugins', 'contactlogs_form', array($this, 'plugin_contactlogs_form_page_handler'));
}
Now it shows a menu option in the left menu of wordpress. I would like to remove this menu option and create a hyperlink on the dashboardpage to this page. If i remove the lines wordpress is telling me i have no permission to view the page. what am i missing here
So i want to remove the menu item but want to page to be accessible as a link on another page. Otherwise my menu will become very large :-).
I hope it is understandable, otherwise please ask..
If you use null for the parent_slug parameter the page won't show up in the menu.
add_submenu_page(
null,
'My Custom Submenu Page',
'My Custom Submenu Page',
'manage_options',
'my-custom-submenu-page',
'callback_function' );
The page will be accessible via any parent slug, as long as it exists:
https://yoursite.com/wp-admin/options-general.php?page=my-custom-submenu-page
https://yoursite.com/wp-admin/tools.php?page=my-custom-submenu-page
https://yoursite.com/wp-admin/index.php?page=my-custom-submenu-page
...
Or no parent slug at all (same effect as if you'd use index.php):
https://yoursite.com/wp-admin/?page=my-custom-submenu-page
But it's probably best to use admin.php for your hyperlinks (the menu item that belongs to the slug you use will be active - if you use admin.php no menu item will be active):
https://yoursite.com/wp-admin/admin.php?page=my-custom-submenu-page

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.

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 create a new button in WordPress admin menu

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.

Wordpress - Different sidebar for each page

Client needs an website to migrate into WordPress.
In this website each page has an sidebar with different content inside it
In some page accordion comes under sidebar, in some just text and images are visible
How to implement this in WordPress?
If template has to be created, there are many page it cant be done so
Even for every page different sidebar widget is not also possible
Guide me a way to implement this
A different sidebar (widget) are can be added to each page in two steps:
Add the sidebar to the theme template using the page slug as part of the sidebar name. This ensures that the sidebar has a unique name for that page.
Register the sidebars for each page in functions.php for your theme
Add the sidebar to the theme template
In your theme template, add the following code where you want the widget area to be:
<?php
global $post;
dynamic_sidebar( 'widget_area_for_page_'.$post->post_name );
?>
Register the sidebars
In functions.php for your theme add the following block of code to register the sidebars for each page in your site. Note that it includes draft pages etc so that you can edit the widgets while still in draft mode.
function myTheme_registerWidgetAreas() {
// Grab all pages except trashed
$pages = new WP_Query(Array(
'post_type' => 'page',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
'posts_per_page'=>-1
));
// Step through each page
while ( $pages->have_posts() ) {
$pages->the_post();
// Ignore pages with no slug
if ($pages->post->post_name == '') continue;
// Register the sidebar for the page. Note that the id has
// to match the name given in the theme template
register_sidebar( array(
'name' => $pages->post->post_name,
'id' => 'widget_area_for_page_'.$pages->post->post_name,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
}
add_action( 'widgets_init', 'myTheme_registerWidgetAreas' );
Hope it helps!
There's a great plugin for this:
https://wordpress.org/plugins/custom-sidebars/
Sometimes it is necessary to show different elements on the sidebars
for some posts or pages. The themes nowadays give you some areas to
put the widgets, but those areas are common for all the posts that are
using the same template. NOTE: You need to use a theme that accepts
widgets to make this plugin work.
I think this is what you're looking for.
This can be done with the https://wordpress.org/plugins/jetpack/ plugin. Once activated you can choose what widgets display on what pages:
The Widget Visibility module enables you to configure widgets to appear only on certain pages (or be hidden on certain pages) by using the Visibility panel.
Visibility is controlled by five aspects: page type, category, tag, date, and author. For example, if you wanted the Archives widget to only appear on category archives and error pages, choose “Show” from the first dropdown and then add two rules: “Page is 404 Error Page” and “Category is All Category Pages.”
Just tried custom-sidebars with the latest version of wp. Had a hard time deleting it. Database is still messy. Plugin did not work. Maybe the 'pro' version is useful, but the 'non-pro' version would not add a sidebar to the page. It was clunky and did not allow per-page sidebars. It says its not been tested with the latest wp, maybe that's the problem.
You can create custom sidebar for for each post and pages with the help of plugin. There are a few plugins which enables you to create your custom sidebar. Here in this tutorial we are using Easy Custom Sidebars. Download and install this plugin in your website and activate it.
After successfully activating the plugin go to Appearance > Sidebar Replacement
From this page give a name to your custom sidebar and click on the Create Sidebar button. After that go to the Sidebar Properties option. Here you need to select the properties where you want to replace your created custom sidebar instead of theme sidebar. You can view your available option in the drop-down menu. Select it and provide a description on the Sidebar Description field.
Now in the last step select the post or pages to add this custom sidebar. You can see all the option in the left column. You can select specific post, page, category , tag, author to add this sidebar. Just select it and click Add to sidebar option.
For better understanding you can follow this tutorial

Resources