如何覆盖WordPress主题的“functions.php”文件中的插件函数?

如何覆盖WordPress主题的“functions.php”文件中的插件函数?,wordpress,wordpress-theming,Wordpress,Wordpress Theming,如何覆盖主题文件夹的“functions.php”文件中的插件函数 下面是我的代码: if(!function_exists('userphoto_filter_get_avatar')){ function userphoto_filter_get_avatar($avatar, $id_or_email, $size, $default){ global $userphoto_using_avatar_fallback, $wpdb, $userphoto_preve

如何覆盖主题文件夹的“functions.php”文件中的插件函数

下面是我的代码:

if(!function_exists('userphoto_filter_get_avatar')){
    function userphoto_filter_get_avatar($avatar, $id_or_email, $size, $default){
        global $userphoto_using_avatar_fallback, $wpdb, $userphoto_prevent_override_avatar;
        if($userphoto_using_avatar_fallback)
            return $avatar;

        if(is_object($id_or_email)){
            if($id_or_email->ID)
                $id_or_email = $id_or_email->ID;
            // Comment
            else if($id_or_email->user_id)
                $id_or_email = $id_or_email->user_id;
            else if($id_or_email->comment_author_email)
                $id_or_email = $id_or_email->comment_author_email;
        }

        if(is_numeric($id_or_email))
            $userid = (int)$id_or_email;
        else if(is_string($id_or_email))
            $userid = (int)$wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . mysql_escape_string($id_or_email) . "'");

        if(!$userid)
            return $avatar;

        // Figure out which one is closest to the size that we have for the full or the thumbnail
        $full_dimension = get_option('userphoto_maximum_dimension');
        $small_dimension = get_option('userphoto_thumb_dimension');
        $userphoto_prevent_override_avatar = true;
        $img = userphoto__get_userphoto($userid, (abs($full_dimension - $size) < abs($small_dimension - $size)) ? USERPHOTO_FULL_SIZE : USERPHOTO_THUMBNAIL_SIZE, '', '', array(), '');
        $userphoto_prevent_override_avatar = false;
        if($img)
            return $img;

        return $avatar;
    }
}
当我激活插件时,它给了我一个致命的错误:

无法重新声明用户照片\u过滤器\u获取\u头像


我做错了什么?

将自定义覆盖代码添加到

只有使用其他插件或必须使用的插件才能覆盖插件中定义的。将代码添加到另一个插件是不可靠的。因此,最好使用必用插件

请注意,WordPress核心中定义的可插入函数可以通过在我们的插件或主题中使用相同名称的函数来覆盖

添加到主题的functions.php文件中的代码将在稍后执行,即在插件代码执行之后。因此,在主题文件中添加覆盖函数将触发无法重新声明错误

原因:

各种WordPress操作的执行顺序在中指定。从这里我们可以看到,简化的执行顺序是


因此,为了覆盖第二个钩子(即插件)中定义的功能,我们需要在一个钩子(即必须使用的插件)中重新定义它。看看。你在哪里添加了上述代码?共享插件文件和theme functions.php文件中的代码。这非常有用。谢谢轻松修复覆盖插件中存在的主题安装函数,而不是主题文件,因此在functions.php文件中不容易覆盖。很高兴我能提供帮助。