首页 > 解决方案 > 一个插件覆盖了我使用functions.php设置的属性值

问题描述

我必须遵循代码:

function my_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {
    // Stuff that sets background
    $avatar = "<img src='{$avatar}' alt='{$alt}' style='{$background}' class='avatar test avatar-{$size} photo' height='{$size}' width='{$size}' />";
    return $avatar;
}
add_filter( 'get_avatar' , 'my_custom_avatar' , 10, 5);

上面的代码style在大多数情况下应用带有背景 URL 的属性,因此它可以正常工作。

但是,在论坛插件的一页上,该style属性设置为width: 64px; height: 64px;. 看起来我的style属性值被插件用于的其他过滤器覆盖get_avatar

有什么方法可以确保我的过滤器添加的代码不会被插件覆盖?

我尝试使用以下方法提高优先级:

 add_filter( 'get_avatar' , 'my_custom_avatar' , 99, 5);
 add_filter( 'get_avatar' , 'my_custom_avatar' , 999, 5);
 add_filter( 'get_avatar' , 'my_custom_avatar' , 9999999, 5);

然而,他们都没有工作。

如何防止插件覆盖我的值?

谢谢。

标签: phpwordpress

解决方案


您可以使用 删除插件创建的过滤器remove_filter,例如,如果插件如下:

function pluginMethod($args) {}
add_filter('get_avatar', 'pluginMethod', 10, 5);

你可以做:

remove_filter('get_avatar', 'pluginMethod');

请参阅:https ://codex.wordpress.org/Function_Reference/remove_filter

希望这对您有所帮助。


推荐阅读