该类来自其他人的WP插件,我想改编该类中的一个函数(还有更多函数)。
(编辑后的)原始代码:
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;
}
}
我想用以下内容覆盖上述函数:
function get_section_url() {
$full_url = add_query_arg( \'action\', \'action-name\', admin_url( \'users.php\' ) );
return $full_url;
}
如何做到这一点?我尝试了一些建议的解决方案(
here 和
here), 但没有成功。
UPDATE
以这种方式调用原始函数(仅来自同一类):
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 );
}
}
SO网友:mmm
如果在操作中调用修改admin_menu
您可以紧跟其后,以便使用以下代码更正菜单:
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