Support » Developing with WordPress » Creating a custom endpoint for the wp-admin only

  • I have created a custom menu item that shows a list of links to a sub page:

    
    function my_admin_menu() {
        add_menu_page(
            __( 'Statistics', 'my-textdomain' ),
            __( 'Statistics', 'my-textdomain' ),
            'manage_options',
            'my-statistics',
            'my_statistics',
            'dashicons-schedule',
        );
    }
    
    add_action( 'admin_menu', 'my_admin_menu' );
    
    function my_statistics() {
        ?>
          <h1>
        <?php esc_html_e( 'Statistics', 'my-plugin-textdomain' ); ?>
          </h1>
          <ul>
              <li><a href="?object=123">Show statistics for object 123</a></li>
              <li><a href="?object=435">Show statistics for object 435</a></li>
          </ul>
        <?php
    }
    

    How can I “register” the endpoint /wp-admin/admin.php?page=my-statistics?object=123 and correctly hook it into WordPress?

    • This topic was modified 1 month, 1 week ago by top-node.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    A second ? in an URL is invalid. Query string parameters must be separated with &. Only the separation from the main URL should use ?.
    admin.php?page=my-statistics&object=123

    You can handle custom URL parameters right in your my_statistics() function. Conditionally do something based on the existence of array key “object” in $_GET. If it’s not in the array, output the default content.

    If you don’t want to do that for some reason, the links must lead elsewhere and exactly where is somewhat limited. It will essentially become a front end request and take the user out of the back end. You can submit through /wp-admin/admin-post.php. Despite the file location, there’s no back end UI in its output.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.