Make WordPress Core

Opened 12 years ago

Last modified 7 weeks ago

#27888 assigned feature request

Feature request: `get_current_admin_url()` and `get_current_admin_hook()`

Reported by: denis-de-bernardy's profile Denis-de-Bernardy Owned by: lucatume's profile lucatume
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.9
Component: Administration Keywords: good-first-bug has-patch has-unit-tests
Focuses: administration Cc:

Description

It would be sweet if to be able to get the current page's url using some kind of API. And its hook, for that matter. For instance:

    public function get_current_admin_page_url()
    {
        if (!is_admin()) {
            return false;
        }

        global $pagenow;
        global $typenow;
        global $taxnow;
        global $plugin_page;

        $url = $pagenow;
        if (!empty($plugin_page)) {
            $url .= '?page='.$plugin_page;
        }
        elseif (!empty($typenow)) {
            $url .= '?post_type='.$typenow;
        }
        elseif (!empty($taxnow)) {
            $url .= '?taxonomy='.$taxnow;
        }

        return $url;
    }

And something similar for get_current_admin_hook().

Attachments (5)

27888.diff (559 bytes) - added by mwtsn 10 years ago.
Patch to add a get_current_admin_page_url() function into wp-admin/includes/misc.php. This is my first patch, so be gentle with me when feeding back.
27888.2.diff (722 bytes) - added by mwtsn 10 years ago.
Patch to add a get_current_admin_hookl() function into wp-admin/includes/misc.php. This is only my second patch, so feedback appreciated.
27888.3.patch (896 bytes) - added by sudar 9 years ago.
27888.4.patch (3.7 KB) - added by lucatume 9 years ago.
27888.4.patch (tests)
27888.5.patch (4.2 KB) - added by lucatume 9 years ago.
27888.5.patch (tests refinement)

Download all attachments as: .zip

Change History (25)

#1 @jdgrimes
12 years ago

Somewhat related: #27737

#2 @chriscct7
10 years ago

  • Keywords needs-patch needs-unit-tests added

#3 follow-up: @swissspidy
10 years ago

  • Keywords good-first-bug added

This ticket was mentioned in Slack in #core by tahteche. View the logs.


10 years ago

#5 @rockwell15
10 years ago

I could be misunderstanding, but doesn't global $hook_suffix cover the page's hook?

#6 in reply to: ↑ 3 ; follow-up: @mrahmadawais
10 years ago

Replying to swissspidy:

I am up for it. Could you tell me where would it go? Inside pluggable.php ?

#7 in reply to: ↑ 6 ; follow-up: @swissspidy
10 years ago

Replying to mrahmadawais:

Replying to swissspidy:

I am up for it. Could you tell me where would it go? Inside pluggable.php ?

pluggable.php is only for "pluggable" functions (wrapped in if ( ! function_exists( ... ) ) blocks), which is considered a bad practice.

I'm not that familiar with the file structure inside wp-admin, but I'd put it inside wp-admin/includes/misc.php for now.

Note: Not wp-includes, because the suggested functions are only needed in the admin.

#8 in reply to: ↑ 7 @mrahmadawais
10 years ago

Replying to swissspidy:

Replying to mrahmadawais:

Replying to swissspidy:

Got it, will contribute a patch at the weekend.

#9 @brgweb
10 years ago

@mrahmadawais are you working on it?

@mwtsn
10 years ago

Patch to add a get_current_admin_page_url() function into wp-admin/includes/misc.php. This is my first patch, so be gentle with me when feeding back.

@mwtsn
10 years ago

Patch to add a get_current_admin_hookl() function into wp-admin/includes/misc.php. This is only my second patch, so feedback appreciated.

#10 @mwtsn
10 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

I should note that 27888.2.diff contains both patches, including the one from 27888.diff.

This ticket was mentioned in Slack in #core by mwtsn. View the logs.


10 years ago

#12 @jorbin
10 years ago

What are the use cases for these functions?

#13 @PerS
9 years ago

@jorbin, sample use case: I borrowed this function and use it in admin bar menu, allowing me to return to the page I was on when returning from the customizer, eg:

<?php
function add_admin_bar_customizer_url( $wp_admin_bar ) {
        global $post;
        $return_url = ( is_admin() ) ? $this->_get_current_admin_page_url() :  get_permalink( $post->ID );

        $args = array(
                'id' => 'plugin-customizer-link',
                'title' => __( 'Plugin Customizer', 'plugin-customizer' ),
                'href' => $this->_get_customizer_url( $return_url, 'plugin_settings_panel' ),
        );

        $wp_admin_bar->add_node( $args );
}

#14 @sudar
9 years ago

@mwtsn

Thanks for the patch.

I have enhanced your patch by adding phpdoc blocks to the functions.

Also I am wondering if we really need get_current_admin_hook, since right now it is just a wrapper to current_filter function.

@sudar
9 years ago

#15 @lucatume
9 years ago

Hi there,

I've created a patch that adds tests to cover the two new functions, let me know if it helps. The patch only applies to the the tests/phpunit/tests/admin/includeMisc.php file.

@lucatume
9 years ago

27888.4.patch (tests)

#16 @lucatume
9 years ago

Adding a further patch that refactors the previously existing tests to use data providers, again this patch will only apply to the tests/phpunit/tests/admin/includeMisc.php file.

@lucatume
9 years ago

27888.5.patch (tests refinement)

#17 @DrewAPicture
9 years ago

  • Owner set to lucatume
  • Status changed from new to assigned

Assigning ownership to mark the good-first-bug as "claimed".

This ticket was mentioned in Slack in #core by sergey. View the logs.


6 years ago

This ticket was mentioned in PR #8266 on WordPress/wordpress-develop by @sukhendu2002.


13 months ago
#19

  • Keywords has-unit-tests added; needs-unit-tests removed

#20 @ozgursar
7 weeks ago

  • Keywords needs-testing removed

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/8266

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 143.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-One 2.7
  • MU Plugins: None activated
  • Plugins:
    • Code Snippets 3.9.4
    • Test Reports 1.2.1

Steps to Reproduce

  1. Using Code Snippets plugin or to the functions.php of the theme add the following snippet to view the current admin page url and admin hook
// Add to admin bar
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
    if ( ! is_admin() ) {
        return;
    }

    $current_url = get_current_admin_page_url();
    $current_hook = get_current_admin_hook();

    // Parent menu item
    $wp_admin_bar->add_node( array(
        'id'    => 'debug-info',
        'title' => '🔍 Debug Info',
        'href'  => '#',
    ) );

    // Current page URL
    $wp_admin_bar->add_node( array(
        'parent' => 'debug-info',
        'id'     => 'current-page-url',
        'title'  => '<strong>Page URL:</strong> ' . esc_html( $current_url ),
        'href'   => '#',
    ) );

    // Current hook
    $wp_admin_bar->add_node( array(
        'parent' => 'debug-info',
        'id'     => 'current-hook',
        'title'  => '<strong>Hook:</strong> ' . esc_html( $current_hook ),
        'href'   => '#',
    ) );
}, 100 );

// Add some styling to make it more visible
add_action( 'admin_head', function() {
    ?>
    <style>
        #wp-admin-bar-debug-info > .ab-item {
            background: #2271b1 !important;
            color: white !important;
        }
        #wp-admin-bar-current-page-url .ab-item,
        #wp-admin-bar-current-hook .ab-item {
            font-family: monospace;
            font-size: 12px;
        }
    </style>
    <?php
} );

Actual Results

  1. ✅ get_current_admin_page_url() and get_current_admin_hook() return the expected output.

Additional Notes

  • I had to write the PHP code above to be able to test the functionality as no specific use case was provided for the test.

Supplemental Artifacts

https://i.imgur.com/5Y5Vu0h.png

Video:
https://files.catbox.moe/q87hhq.mp4

Note: See TracTickets for help on using tickets.