WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#27888 assigned feature request

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

Reported by: Denis-de-Bernardy Owned by: lucatume
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.9
Component: Administration Keywords: needs-unit-tests good-first-bug has-patch needs-testing
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 16 months 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 16 months 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 14 months ago.
27888.4.patch (3.7 KB) - added by lucatume 12 months ago.
27888.4.patch (tests)
27888.5.patch (4.2 KB) - added by lucatume 12 months ago.
27888.5.patch (tests refinement)

Download all attachments as: .zip

Change History (22)

#1 @jdgrimes
4 years ago

Somewhat related: #27737

#2 @chriscct7
2 years ago

  • Keywords needs-patch needs-unit-tests added

#3 follow-up: @swissspidy
23 months ago

  • Keywords good-first-bug added

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


22 months ago

#5 @rockwell15
22 months ago

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

#6 in reply to: ↑ 3 ; follow-up: @mrahmadawais
21 months 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
21 months 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
21 months ago

Replying to swissspidy:

Replying to mrahmadawais:

Replying to swissspidy:

Got it, will contribute a patch at the weekend.

#9 @brgweb
20 months ago

@mrahmadawais are you working on it?

@mwtsn
16 months 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
16 months 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
16 months 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.


16 months ago

#12 @jorbin
15 months ago

What are the use cases for these functions?

#13 @PerS
14 months 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
14 months 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
14 months ago

#15 @lucatume
12 months 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
12 months ago

27888.4.patch (tests)

#16 @lucatume
12 months 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
12 months ago

27888.5.patch (tests refinement)

#17 @DrewAPicture
5 months ago

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

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

Note: See TracTickets for help on using tickets.