简体   繁体   English

如何在 wordpress functions.php 中覆盖插件类函数?

[英]How can i override plugin class function in wordpress functions.php?

I want to basically override a plugin class function in my Wordpress functions.php.我想基本上覆盖我的 Wordpress functions.php 中的插件类函数。 I want to give access to the editor for the plugin settings.我想授予对插件设置编辑器的访问权限。 is there any way?有什么办法吗? because it has a constructor function.因为它有构造函数。 will be very thankful.将非常感谢。

Plugin code插件代码

class BulkImport
{

    function __construct()
    {

        add_action("admin_menu", array($this, 'menu'));

    }

    function menu()
    {
        add_submenu_page('edit.php?post_type=wpdmpro', __( "Bulk Import ‹ Download Manager" , "download-manager" ), __( "Bulk Import" , "download-manager" ), WPDM_MENU_ACCESS_CAP, 'importable-files', array($this, 'UI'));
    }
}

I want to replace the WPDM_MENU_ACCESS_CAP to edit_pages我想将WPDM_MENU_ACCESS_CAP替换为edit_pages

Thanks.谢谢。

Short answer, have you tried using an Admin Menu editor plugin such as Admin Menu Editor ?简短的回答,您是否尝试过使用管理菜单编辑器插件,例如Admin Menu Editor I don't have experience with it and thus can't vouch for it, but it seems well reviewed and managed.我没有这方面的经验,因此不能保证它,但它似乎得到了很好的审查和管理。


Edit : If they are doing a capability check on the UI method, as well as the admin_menu hook, you may just be better off using the WP_Role::add_cap() to add the WPDM_MENU_ACCESS_CAP to the editor role.编辑:如果他们正在对UI方法以及admin_menu挂钩进行功能检查,则最好使用WP_Role::add_cap()WPDM_MENU_ACCESS_CAP添加到editor角色。 You would have to make sure that capability doesn't give them access to other things they shouldn't though.您必须确保该功能不会让他们访问他们不应该访问的其他东西。 Based on the name of the constant, it sounds like it would be fine, but you'd need to do a cursory glance through the plugin to make sure.根据常量的名称,听起来没问题,但您需要粗略地浏览一下插件以确保。


Long answer: with WordPress, generally if things are done correctly, you rarely need to "override" plugin classes, class methods, or functions (though, sometimes you do).长答案:使用 WordPress,通常如果操作正确,您很少需要“覆盖”插件类、类方法或函数(尽管有时您会这样做)。 One of the great things about WordPress is its Hooks system, specifically its Action Hooks . WordPress 的一优点是它的Hooks系统,特别是它的Action Hooks

When a plugin usesadd_action() and omits the third argument ( $priority ) gets set to 10 by default.当插件使用add_action()并省略第三个参数 ( $priority ) 时,默认设置为10 The way priority works is that lower numbers get run on that hook first, so take the following:优先级的工作方式是首先在该挂钩上运行较低的数字,因此请执行以下操作:

add_action( 'some_hook', 'some_other_function' );
add_action( 'some_hook', 'some_function', 3 );
add_action( 'some_hook', 'func', 999 );

Despite the order these were written in, when some_hook runs on that request, some_function will run first (priority 3), then some_other_function (default priority of 10), and lastly func will run, with a priority of 999.尽管它们是按顺序编写的,当some_hook在该请求上运行时, some_function将首先运行(优先级 3),然后some_other_function (默认优先级为 10),最后func将运行,优先级为 999。

What this is nice for, is that func has access to modify or undo anything set in some_function and some_other_function .这有什么some_other_function ,是func可以修改或撤消some_functionsome_other_function设置的任何some_other_function

Now your plugin has this line add_action("admin_menu", array($this, 'menu'));现在你的插件有这一行add_action("admin_menu", array($this, 'menu')); , which adds its own menu method to the admin_menu hook with a priority of 10 . ,它将自己的menu方法添加到admin_menu挂钩,优先级为10 This means you can write your own function and hook it to the admin_menu hook with a lower (higher number) priority, such as 11 or 99 .这意味着您可以编写自己的函数并将其挂钩到具有较低(较高数字)优先级的admin_menu挂钩,例如1199 In that function, you can use the remove_submenu_page() function to remove the page added by the plugin, and then use the add_menu_page() function to add it back in, but change any of the parameters!在该函数中,您可以使用remove_submenu_page()函数删除插件添加的页面,然后使用add_menu_page()函数将其添加回来,但更改任何参数! For instance:例如:

add_action( 'admin_menu', 'change_BulkImport_submenu', 11 );
function change_BulkImport_submenu(){
    remove_submenu_page( 'edit.php?post_type=wpdmpro', 'importable-files' );
    add_submenu_page('edit.php?post_type=wpdmpro', __( "Bulk Import ‹ Download Manager" , "download-manager" ), __( "Bulk Import" , "download-manager" ), 'edit_pages', 'importable-files', array('BulkImport', 'UI') );
}

Here's where it can be a bit complicated depending on how the plugin you're working with.在此处,它可以是一个有点复杂,这取决于你如何正在使用的插件。 in the last argument for add_submenu_page , you'll need to grab the globally instantiated class method, call it as a static method, or instantiate a new instance of the class and use the method there.add_submenu_page的最后一个参数中,您需要获取全局实例化的类方法,将其作为静态方法调用,或者实例化类的新实例并在那里使用该方法。 I took a stab at what may be in there, but without seeing the plugin, I can't tell.我试了一下那里可能有什么,但没有看到插件,我无法判断。 For an example, I have a plugin where I have to mess with Elegant Theme's "Bloom" plugin, and in order to do it I call global $et_bloom and then remove_action( 'wp_footer', [$et_bloom, 'display_flyin'] );例如,我有一个插件,我必须弄乱 Elegant Theme 的“Bloom”插件,为了做到这一点,我调用global $et_bloom然后remove_action( 'wp_footer', [$et_bloom, 'display_flyin'] ); on the hook that there's a conflicy.在挂钩上,有一个冲突。

You'll see posts about modifying the global $menu, $submenu (or $GLOBALS['submenu'] ), but those only work if you're restricting permissions, not relaxing them.您将看到有关修改global $menu, $submenu (或$GLOBALS['submenu'] )的帖子,但这些仅在您限制权限时才有效,而不是放宽权限。 This is because very early in the add_submenu_page() it checks the return value forcurrent_user_can($capability) and if the current user doesn't have it, it will be added to the global $_wp_submenu_nopriv and then the function returns false right there (so the item doesn't get added).这是因为在add_submenu_page()早期,它检查current_user_can($capability)的返回值,如果当前用户没有它,它将被添加到全局$_wp_submenu_nopriv ,然后函数在那里返回false (所以该项目不会被添加)。

In general, I prefer the unhook/rehook method if possible, because it leaves a clearer trail of what you're doing, and why, and it works in both directions (restricting/relaxing).一般来说,如果可能的话,我更喜欢 unhook/rehook 方法,因为它会更清楚地了解你在做什么,为什么,并且它在两个方向上都有效(限制/放松)。 But, in the admin_menu hook, you have access to the global $menu and global $submenu variables, which you can modify before they get rendered to the page, but after the permissions have already been determined, it can get real ugly relaxing permissions on this hook, though restricting is nice and simple, here's a quick example:但是,在admin_menu钩子中,您可以访问global $menuglobal $submenu变量,您可以在它们呈现到页面之前对其进行修改,但是权限已经确定之后,它可以获得真正丑陋的放松权限这个钩子,虽然限制很好很简单,但这里有一个简单的例子:

add_action( 'admin_menu', function(){
    global $menu, $submenu;

    // Is the submenu I want in the page I want?
    if( isset($submenu['my-top-level-slug']) ){ 
        // Yes, we have an array of arrays (each submenu item = array)  
        foreach( $submenu['my-top-level-slug'] as $index => $submenu_array ){
            //var_dump( $index );
            // Search these arrays for my secondary slug
            $search = array_search('my-secondary-slug', $submenu_array );

            // This will return the submenu index if we found it, false if not
            if( $search != false ){
                // We found it! It's in this $index. Modify the [1]st element (permissions)
                $submenu['my-top-level-slug'][$index][1] = 'manage_options';

                // Stop processing this loop, we found what we came for.
                break;
            }
        }
    }
}, 11 );

More generally (but thanks to @XHynx's specific solution):更一般的(但感谢@XHynx 的特定解决方案):

functions.php函数.php

remove_action("admin_menu", 'menu');
add_action("admin_menu", 'my_menu');

function my_menu(){
    /*copy the original function here and modify it */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM