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

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.

Related

Plugin Development: Dynamically change post title (the_title()) from a plugin, without changing database title

I am developing a custom plugin that needs to change the post title dynamically (NOT the "title" tag for the head) without using a page template.
I want the end user of the plugin to be able to use their own theme templates, which will already output the title, usually in an h1 tag, but I don't know where and how for sure, because it is not my theme.
I will need to just change the output on the frontend, not in the database. I want to change it based on whether a url's query string matches certain values, or whether the frontend user is logged in or not. I know how to do this from a custom template, but I want to do this from a template the user already has selected.
I noticed that if I filter the_title(), it changes it everywhere in the admin dashboard and in the frontend navigation menus, which is what I don't want, I just want to change where it is outputted in the user's page template (usually in an h1 tag).... does anyone have any suggestions on how to go about this?
You can use the_title filter, which would have influence only on front-end, this doesn't change record in Database. Your code would be something like this:
add_filter( 'the_title', 'add_text_to_post_title' );
function add_text_to_page_title( $title ) {
if( is_post() )
$title = 'Your text: '. $title;
return $title;
}

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?

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.

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.

wordpress : how to add categories and tags on pages?

I have generated pages using a custom template by creating a php file in my theme directory
something like :
<?php
*
* Template Name: Contact Page
*/
?>
<html ..... </html>
and then adding a new page on the dashboard selecting this new template
How can i now associate tags and categories to each pages ?
Is creating posts instead of pages the only solution?
Even better is to add to functions.php in your theme folder:
function myplugin_settings() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');
}
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );
Tried using the accepted answer but for some reason it only shows the Post types and none of the Pages shows in the category page. E.g. /category/entertainment/
To fix that, I have to do this:
// add tag and category support to pages
function tags_categories_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}
// tag and category hooks
add_action('init', 'tags_categories_support_all');
add_action('pre_get_posts', 'tags_categories_support_query');
Try this:
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats(){
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
Not at all helpful to say 'download plugin' for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages with regular accounts - ie you're not a developer.
First, make sure you have your pages in your menu set up properly.
YOU DO NOT NEED TO MAKE YOUR PAGES 'Categories' or 'Tags'!
This wouldn't give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn't be able to.
Then go to WP Admin > Pages
Select a page to edit and go to the text editor instead of visual editor (far right hand side tab)
Then past the following short code:
[display-posts category="hair,makeup,reviews,beauty" posts_per_page="10" include_date="true" text-decoration: none date_format="F j, Y" order="DESC" include_excerpt="true" wrapper="div" image_size="large"]
<
(The shortcode collects all the posts that you have assigned certain categories in your blog posts i.e. mine was hair and beauty. So obviously change yours to ones that are appropriate. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)
this plugin sorted me out :
http://wordpress.org/extend/plugins/add-tags-and-category-to-page/
with the standard instructions :
Upload the plugin files to the /wp-content/plugins/ directory
Activate the plugin through the 'Plugins' menu in WordPress
Use the setting page of the plugin from Settings > Add Tags And Category For Page.

Resources