Make WordPress Core

Opened 10 years ago

Last modified 5 years 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: 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 8 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 8 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 8 years ago.
27888.4.patch (3.7 KB) - added by lucatume 8 years ago.
27888.4.patch (tests)
27888.5.patch (4.2 KB) - added by lucatume 8 years ago.
27888.5.patch (tests refinement)

Download all attachments as: .zip

Change History (23)

#1 @jdgrimes
10 years ago

Somewhat related: #27737

#2 @chriscct7
9 years ago

  • Keywords needs-patch needs-unit-tests added

#3 follow-up: @swissspidy
9 years ago

  • Keywords good-first-bug added

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


9 years ago

#5 @rockwell15
9 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
8 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
8 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
8 years ago

Replying to swissspidy:

Replying to mrahmadawais:

Replying to swissspidy:

Got it, will contribute a patch at the weekend.

#9 @brgweb
8 years ago

@mrahmadawais are you working on it?

@mwtsn
8 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
8 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
8 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.


8 years ago

#12 @jorbin
8 years ago

What are the use cases for these functions?

#13 @PerS
8 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
8 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
8 years ago

#15 @lucatume
8 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
8 years ago

27888.4.patch (tests)

#16 @lucatume
8 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
8 years ago

27888.5.patch (tests refinement)

#17 @DrewAPicture
7 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.


5 years ago

Note: See TracTickets for help on using tickets.