creating a custom admin page
-
I need to create a new custom page with custom fields on the model of /wp-admin/user-new.php , it could be “booking-new.php”, in the top field (username) I should see an autocomposer that takes data from a specific column of the subscribers table, I named that column “Nome e cognome” (full name), this to start with…
The page I need help with: [log in to see the link]
-
If your new page will be outside of the WP paradigm, then naming it booking-new.php will be OK, but it cannot reside in /wp-admin/. If you want to stay within WP, use the add_menu_page() function. Its URL will end up being something like /wp-admin/admin.php?page=booking-new
The associated callback you create will be responsible for generating the page’s output. It can possibly also handle the form’s submission, though other mechanisms are available such as admin-ajax.php or admin-post.php.
If this booking data will all reside in a custom table there is not a strong need for staying within WP other than for consistency with the rest of your site. But if the data relates to other WP data, then you should want to stay within WP.
Yes, this booking table is related to other wp data. For the moment I’d do just a copy of /wp-admin/user-new.php, just changing the url name, and then I’d try to custom it, is there a quick system? I gave an eye on your link, I found just an example, I dont know what I have to keep and what to delete, I saw a long function:
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '', $position = null ) { global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; $menu_slug = plugin_basename( $menu_slug ); $admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title ); $hookname = get_plugin_page_hookname( $menu_slug, '' ); if ( ! empty( $callback ) && ! empty( $hookname ) && current_user_can( $capability ) ) { add_action( $hookname, $callback ); } if ( empty( $icon_url ) ) { $icon_url = 'dashicons-admin-generic'; $icon_class = 'menu-icon-generic '; } else { $icon_url = set_url_scheme( $icon_url ); $icon_class = ''; } $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); if ( null !== $position && ! is_numeric( $position ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: %s: add_menu_page() */ __( 'The seventh parameter passed to %s should be numeric representing menu position.' ), '<code>add_menu_page()</code>' ), '6.0.0' ); $position = null; } if ( null === $position || ! is_numeric( $position ) ) { $menu[] = $new_menu; } elseif ( isset( $menu[ (string) $position ] ) ) { $collision_avoider = base_convert( substr( md5( $menu_slug . $menu_title ), -4 ), 16, 10 ) * 0.00001; $position = (string) ( $position + $collision_avoider ); $menu[ $position ] = $new_menu; } else { /* * Cast menu position to a string. * * This allows for floats to be passed as the position. PHP will normally cast a float to an * integer value, this ensures the float retains its mantissa (positive fractional part). * * A string containing an integer value, eg "10", is treated as a numeric index. */ $position = (string) $position; $menu[ $position ] = $new_menu; } $_registered_pages[ $hookname ] = true; // No parent as top level. $_parent_pages[ $menu_slug ] = false; return $hookname; }That is a WP core function, there’s nothing there for you to use in your own code. Here is example code that adds an admin menu item that links to a new placeholder page.
add_action('admin_menu', 'gs_new_dealer'); function gs_new_dealer() { add_menu_page('Dealers', 'Dealers', 'read', 'list_dealer', 'gs_list_dealers' , 'dashicons-admin-home', '29.9'); } function gs_list_dealers() { echo 'Dealer Page Place holder'; exit; }The original purpose was to list car dealers in a network. You can use whatever labels you like instead. In place of the echo line you’d add whatever code you need to output content and handle any needed actions. You can use user-new.php as a rough guide but you’ll need to alter anything that is specific to actual users, such as the ‘invite_user’ action hook as one example.
Ok, this is done, now I have “Ordini” in the admin and a page such as: https://test.sacconicase.com/wp-admin/admin.php?page=lista_ordini
Next step is making a content on this page on the style of: https://ibb.co/MGcDcDL
So, how can I create a sort of order-new.php page? I dont know if it should be more on the model of “post-new.php” or “user-new.php”. For sure it’s only for internal purposes, it’s not intended to be public
As you can see each record has a date, automatically created in the moment I create the order, the name of the customer, picked in a selector from subscribers, the booked period, the resort, the name of the agency (author, this is displayed authomatically, it’s related to the booked apartment), and other custom data
Some of the page’s content can be plain HTML, just as you’d do for a static .html page. PHP comes into play for outputting the current orders in table format. You’d query for a page’s worth of orders using any filtering criteria that may have been input. Loop through the results, outputting a row’s worth of data for each iteration.
This is very similar to a post list table. You could actually use an extension of the WP_List_Table class to generate your own order list table. There is a learning curve involved with in using the class. You might decide that generating your own table from scratch to be simpler than learning to use the class.
Now I have a custom admin page https://test.sacconicase.com/wp-admin/admin.php?page=lista_ordini
How can I display on this page just a specific custom posts? The custom posts I’m speaking about should be “orders” , that is to say when I have a booking I add a line. I have also to create specific custom fields for this order page
-
This reply was modified 2 years, 1 month ago by
sacconi.
-
This reply was modified 2 years, 1 month ago by
The topic ‘creating a custom admin page’ is closed to new replies.