删除对WordPress和简单本地头像的Gravatar.com支持


10

目前,我在多个博客上使用“简单本地头像”,使人们可以上传自己的自定义头像。

据我所知,“简单本地头像”用于get_avatar检查是否选择了自定义头像,如果未默认,则默认为Gravatar.com。

如果未选择自定义头像,是否可以以某种方式禁用Gravatar.com,完全禁用Gravatar.com并仅返回一些本地托管图像?

我希望可以在此处发布,但这将是“简单本地头像”文件

<?php
/*
Plugin Name: Simple Local Avatars
Plugin URI: http://www.get10up.com/plugins/simple-local-avatars-wordpress/
Description: Adds an avatar upload field to user profiles if the current user has media permissions. Generates requested sizes on demand just like Gravatar! Simple and lightweight.
Version: 1.2.3
Author: Jake Goldman (10up), Oomph Inc
Author URI: http://www.get10up.com
*/

// add field to user profiles
class simple_local_avatars {
    function simple_local_avatars() {
        add_filter('get_avatar', array($this, 'get_avatar'), 10, 5);
        add_action('admin_init', array($this, 'admin_init'));
        add_action('show_user_profile', array($this, 'edit_user_profile'));
        add_action('edit_user_profile', array($this, 'edit_user_profile'));
        add_action('personal_options_update', array($this, 'edit_user_profile_update'));
        add_action('edit_user_profile_update', array($this, 'edit_user_profile_update'));
        add_filter('avatar_defaults', array($this, 'avatar_defaults'));
    }

    function get_avatar($avatar = '', $id_or_email, $size = '96', $default = '', $alt = false) {
        if (is_numeric($id_or_email))
            $user_id = (int) $id_or_email;
        elseif (is_string($id_or_email)) {
            if ($user = get_user_by_email($id_or_email))
                $user_id = $user->ID;
        } elseif (is_object($id_or_email) && !empty($id_or_email->user_id))
            $user_id = (int) $id_or_email->user_id;
        if (!empty($user_id))
            $local_avatars = get_user_meta($user_id, 'simple_local_avatar', true);
        if (!isset($local_avatars) || empty($local_avatars) || !isset($local_avatars['full'])) {
            if (!empty($avatar)) // if called by filter
                return $avatar;
            remove_filter('get_avatar', 'get_simple_local_avatar');
            $avatar = get_avatar($id_or_email, $size, $default);
            add_filter('get_avatar', 'get_simple_local_avatar', 10, 5);
            return $avatar;
        }
        if (!is_numeric($size)) // ensure valid size
            $size = '96';
        if (empty($alt))
            $alt = get_the_author_meta('display_name', $user_id);
        // generate a new size
        if (empty($local_avatars[$size])) {
            $upload_path = wp_upload_dir();
            $avatar_full_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full']);
            $image_sized = image_resize($avatar_full_path, $size, $size, true);
            if (is_wp_error($image_sized)) // deal with original being >= to original image (or lack of sizing ability)
                $local_avatars[$size] = $local_avatars['full'];
            else
                $local_avatars[$size] = str_replace($upload_path['basedir'], $upload_path['baseurl'], $image_sized);
            update_user_meta($user_id, 'simple_local_avatar', $local_avatars);
        } elseif (substr($local_avatars[$size], 0, 4) != 'http')
            $local_avatars[$size] = site_url($local_avatars[$size]);
        $author_class = is_author($user_id) ? ' current-author' : '';
        $avatar = "<img alt='" . esc_attr($alt) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
        return $avatar;
    }

    function admin_init() {
        load_plugin_textdomain('simple-local-avatars', false, dirname(plugin_basename(__FILE__)) . '/localization/');
        register_setting('discussion', 'simple_local_avatars_caps', array($this, 'sanitize_options'));
        add_settings_field('simple-local-avatars-caps', __('Local Avatar Permissions', 'simple-local-avatars'), array($this, 'avatar_settings_field'), 'discussion', 'avatars');
    }

    function sanitize_options($input) {
        $new_input['simple_local_avatars_caps'] = empty($input['simple_local_avatars_caps']) ? 0 : 1;
        return $new_input;
    }

    function avatar_settings_field($args) {
        $options = get_option('simple_local_avatars_caps');
        echo '
            <label for="simple_local_avatars_caps">
                <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked($options['simple_local_avatars_caps'], 1, false) . ' />
                ' . __('Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars') . '
            </label>
        ';
    }

    function edit_user_profile($profileuser) {
        ?>
        <h3><?php _e('Avatar', 'simple-local-avatars'); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="simple-local-avatar"><?php _e('Upload Avatar', 'simple-local-avatars'); ?></label></th>
                <td style="width: 50px;" valign="top">
                    <?php echo get_avatar($profileuser->ID); ?>
                </td>
                <td>
                    <?php
                    $options = get_option('simple_local_avatars_caps');
                    if (empty($options['simple_local_avatars_caps']) || current_user_can('upload_files')) {
                        do_action('simple_local_avatar_notices');
                        wp_nonce_field('simple_local_avatar_nonce', '_simple_local_avatar_nonce', false);
                    ?>
                    <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />
                    <?php
                    if (empty($profileuser->simple_local_avatar))
                        echo '<span class="description">' . __('No local avatar is set. Use the upload field to add a local avatar.', 'simple-local-avatars') . '</span>';
                    else
                        echo '
                            <input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar', 'simple-local-avatars') . '<br />
                            <span class="description">' . __('Replace the local avatar by uploading a new avatar, or erase the local avatar (falling back to a gravatar) by checking the delete option.', 'simple-local-avatars') . '</span>
                        ';
                    } else {
                        if (empty($profileuser->simple_local_avatar))
                            echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.', 'simple-local-avatars') . '</span>';
                        else
                            echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'simple-local-avatars') . '</span>';
                    }
                    ?>
                </td>
            </tr>
        </table>
        <script type="text/javascript">
            var form = document.getElementById('your-profile');
            form.encoding = 'multipart/form-data';
            form.setAttribute('enctype', 'multipart/form-data');
        </script>
        <?php
    }

    function edit_user_profile_update($user_id) {
        if (!wp_verify_nonce($_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce')) //security
            return;
        if (!empty($_FILES['simple-local-avatar']['name'])) {
            $mimes = array(
                'jpg|jpeg|jpe' => 'image/jpeg',
                'gif' => 'image/gif',
                'png' => 'image/png',
                'bmp' => 'image/bmp',
                'tif|tiff' => 'image/tiff'
            );
            $avatar = wp_handle_upload($_FILES['simple-local-avatar'], array('mimes' => $mimes, 'test_form' => false));
            if (empty($avatar['file'])) // handle failures
                {
                switch ($avatar['error']) {
                    case 'File type does not meet security guidelines. Try another.':
                        add_action('user_profile_update_errors', create_function('$a', '$a->add("avatar_error",__("Please upload a valid image file for the avatar.","simple-local-avatars"));'));
                        break;
                    default:
                        add_action('user_profile_update_errors', create_function('$a', '$a->add("avatar_error","<strong>".__("There was an error uploading the avatar:","simple-local-avatars")."</strong> ' . esc_attr($avatar['error']) . '");'));
                }
                return;
            }
            $this->avatar_delete($user_id); // delete old images if successful
            update_user_meta($user_id, 'simple_local_avatar', array('full' => $avatar['url'])); // save user information (overwriting old)
        } elseif (isset($_POST['simple-local-avatar-erase']) && $_POST['simple-local-avatar-erase'] == 1)
            $this->avatar_delete($user_id);
    }

    // remove the custom get_avatar hook for the default avatar list output on options-discussion.php
    function avatar_defaults($avatar_defaults) {
        remove_action('get_avatar', array($this, 'get_avatar'));
        return $avatar_defaults;
    }

    // delete avatars based on user_id
    function avatar_delete($user_id) {
        $old_avatars = get_user_meta($user_id, 'simple_local_avatar', true);
        $upload_path = wp_upload_dir();
        if (is_array($old_avatars)) {
            foreach ($old_avatars as $old_avatar) {
                $old_avatar_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $old_avatar);
                @unlink($old_avatar_path);
            }
        }
        delete_user_meta($user_id, 'simple_local_avatar');
    }
}

$simple_local_avatars = new simple_local_avatars;
if (!function_exists('get_simple_local_avatar')):
/*
 * more efficient to call simple local avatar directly in theme and avoid gravatar setup
 * 
 * @param int|string|object $id_or_email A user ID, email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternate text to use in image tag. Defaults to blank
 * @return string <img> tag for the user's avatar
 */
    function get_simple_local_avatar($id_or_email, $size = '96', $default = '', $alt = false) {
        global $simple_local_avatars;
        return $simple_local_avatars->get_avatar('', $id_or_email, $size, $default, $alt);
    }
endif;

// on uninstallation, remove the custom field from the users and delete the local avatars
register_uninstall_hook(__FILE__, 'simple_local_avatars_uninstall');

function simple_local_avatars_uninstall() {
    $simple_local_avatars = new simple_local_avatars;
    $users = get_users_of_blog();
    foreach ($users as $user)
        $simple_local_avatars->avatar_delete($user->user_id);
    delete_option('simple_local_avatars_caps');
}

Answers:


9

如果您想要一个超轻量级的解决方案,并且不介意花一些代码,请将其放在您的functions.php;中。

function __default_local_avatar()
{
    // this assumes default_avatar.png is in wp-content/themes/active-theme/images
    return get_bloginfo('template_directory') . '/images/default_avatar.png';
}
add_filter( 'pre_option_avatar_default', '__default_local_avatar' );

另外,如果您想要更多的果汁,并且能够在admin中管理所有内容,请查看添加新的默认头像

更新:我编写了一个快速插件,可与“简单本地头像”一起使用。

<?php

/**
 * Plugin Name: Disable Default Avatars
 * Plugin URI: http://wordpress.stackexchange.com/questions/17413
 * Description: To be used alongside <a href="http://www.get10up.com/plugins/simple-local-avatars-wordpress/">Simple Local Avatars</a>, disabling all default avatars and falling back to a single image. Use the filter <code>local_default_avatar</code> to set the path of the image.
 * Version: 1.0
 * Author: TheDeadMedic
 * Author URI: http://wordpress.stackexchange.com/users/1685/thedeadmedic
 */

if ( !function_exists( 'get_avatar' ) ) :
/**
 * Retrieve the avatar for a user who provided a user ID or email address.
 *
 * @since 2.5
 * @param int|string|object $id_or_email A user ID,  email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternate text to use in image tag. Defaults to blank
 * @return string <img> tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
    if ( ! get_option('show_avatars') )
        return false;

    static $default_url; // use static vars for a little caching
    if ( !isset( $default_url ) )
        $default_url = apply_filters( 'local_default_avatar', get_template_directory_uri() . '/images/default_avatar.png' );

    if ( false === $alt)
        $safe_alt = '';
    else
        $safe_alt = esc_attr( $alt );

    if ( !is_numeric( $size ) )
        $size = '96';

    $avatar = "<img alt='{$safe_alt}' src='{$default_url}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
    return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
}
endif;

function __limit_default_avatars_setting( $default )
{
    return 'local_default';
}
add_filter( 'pre_option_avatar_default', '__limit_default_avatars_setting' );

if ( is_admin() ) :
function __limit_default_avatars( $defaults )
{
    return array( 'local_default' => get_bloginfo( 'name' ) . ' Default' );
}
add_filter( 'avatar_defaults', '__limit_default_avatars' );
endif;

?>

插件说明应该是不言自明的,但实际上默认的头像是http://wordpress-theme-path/images/default_avatar.pngfunctions如果存在于主题的.php中,则可以对其进行过滤;

function __my_theme_default_avatar( $url )
{
    return 'http://somewhere/else.jpg';
}
add_filter( 'local_default_avatar', '__my_theme_default_avatar' );

几乎:)这将设置新的默认头像,而不是ie。这个神秘的人。是否也可以完全禁用Gravatar支持,并且只能在您的头像和“简单本地头像”之间进行选择?

检查我修改后的答案。
TheDeadMedic

嗨,DeadMedic,简而言之,您真棒(:非常感谢!

4

通过将其添加到主题function.php文件中,我成功地用我自己的本地托管头像替换了所有gravitar.com头像:

define('DEFAULT_AVATAR_URL', get_template_directory_uri() . '/images/user.png');

function no_gravatars( $avatar ) {
    return preg_replace( "/http.*?gravatar\.com[^\']*/", DEFAULT_AVATAR_URL, $avatar );
}
add_filter( 'get_avatar', 'no_gravatars' );

假设您user.png在主题的images子目录中调用了本地默认头像。

discussion settings管理面板中,确保选中“显示头像”。

我尚未使用“简单本地头像”对此进行测试,但它与“ 基本用户头像”(这是“简单本地头像”的轻量级分支)一起使用时效果很好。

提示:如果您正在设计自己的默认头像,建议将其设置为300x300像素,这样当WP将其显示为“ 150x150”时,它将在“视网膜”屏幕上看起来不错。然后,可以在tinyPNG.com上压缩图像,从而大大减少图像的文件大小。


0

完全删除化身。将此代码放在您使用的主题中的functions.php中。

add_action( 'init', 'qmas_remove_avatars' );
if (!function_exists('qmas_remove_avatars')) {
    function qmas_remove_avatars() {
        add_filter( 'bp_core_fetch_avatar', function () { return ''; } );
        add_filter( 'get_avatar', function () { return ''; } );
        add_filter( 'bp_get_signup_avatar', function () { return ''; } );
    }
}

您能解释一下它如何有所帮助吗?
bravokeyl

将IMG标签替换为空字符串。同时在头像网址中返回一个空字符串。保留默认的Wordpress分配
Bryan Contreras's

NOTA我不太会英语,有时我会使用翻译。
布莱恩·孔特雷拉斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.