1

The class is from someone else's WP plugin and I want to adapt one of the functions from that class (there are more functions).

The (edited) original code:

class some_class {
    private function get_section_url() {
        $full_url = add_query_arg( 'action', 'action-name', admin_url( 'users.php' ) );
        $relative_url = str_replace( get_site_url(), '', $full_url );
        return $relative_url;
    }
}

I want to override the above function with this:

function get_section_url() {
    $full_url = add_query_arg( 'action', 'action-name', admin_url( 'users.php' ) );
    return $full_url;
}

How to achieve this? I tried some suggested solutions (here and here), but without success.

UPDATE

The original function is called in this way (only from the same class):

public function parent_file($parent_file) {
    global $current_screen, $submenu_file, $typenow;
    if ( $current_screen->base == 'users' && request_param( 'action' ) == 'action-name' ) {
        $parent_file = 'parent.php';
        $submenu_file = $this->get_section_url();
        $typenow = 'hide-users-menu';
    }
    return $parent_file;
}

public function menu() {
    global $submenu;
    $capability = admin_capability();
    if ( current_user_can( $capability ) ) {
        $url = $this->get_section_url();
    $submenu['parent.php'][] = array( __( 'Menu Item Title' ), $capability, $url );
    }
}
  • 1
    How is that function called in the code? – czerspalace Aug 25 '15 at 19:44
  • @czerspalace I updated the question. – Iurie Malai Aug 25 '15 at 20:07
  • and how is called menu() ? – mmm Aug 25 '15 at 23:00
  • 1
    @mmm I found this: add_action('admin_menu', array($this, 'menu')); – Iurie Malai Aug 26 '15 at 18:58
1

if the modification is called in the action admin_menu you can come just after, in order to correct the menu with some code like this :

add_action("admin_menu", function () {

    $parent_file = 'parent.php';
    $full_url = add_query_arg( 'action', 'action-name', admin_url( 'users.php' ) );


    global $submenu;


    // searching the last submenu
    $last = array_pop($submenu[$parent_file]);

    // correcting the URL
    $last[2] = $full_url;

    // replacing in the array
    $submenu[$parent_file][] = $last;

}, 20);
// default priority (used by the plugin) is 10, so I choose 20 to be sur that it comes after the plugin
  • I placed your code in the functions.php, but it not works, also no errors. If this helps: the effect must be seen in the admin dashboard. – Iurie Malai Aug 28 '15 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.