{"id":754,"date":"2024-10-24T11:00:26","date_gmt":"2024-10-24T03:00:26","guid":{"rendered":"https:\/\/www.cuishaofeng.com\/?p=754"},"modified":"2024-10-24T11:00:30","modified_gmt":"2024-10-24T03:00:30","slug":"%e3%80%90wordpress%e6%95%99%e7%a8%8b%e3%80%91wordpress%e5%a4%b4%e5%83%8f%e6%97%a0%e6%b3%95%e6%98%be%e7%a4%ba%e6%97%a0%e6%b3%95%e6%9b%b4%e6%8d%a2%ef%bc%8c%e5%85%8d%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/www.cuishaofeng.com\/?p=754","title":{"rendered":"\u3010WordPress\u6559\u7a0b\u3011WordPress\u5934\u50cf\u65e0\u6cd5\u663e\u793a\u65e0\u6cd5\u66f4\u6362\uff0c\u514d\u63d2\u4ef6\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u4e0a\u4f20\u5934\u50cf\u529f\u80fd"},"content":{"rendered":"<div id=\"core-ai-summary-tool\"><\/div>\n<p>\u8be5\u529f\u80fd\u4e3b\u8981\u662f\u7528\u4e8e\u514d\u63d2\u4ef6\u4e3a\u7528\u6237\u6dfb\u52a0\u5934\u50cf\uff0c\u89e3\u51b3\u5934\u50cf\u65e0\u6cd5\u663e\u793a\u6216\u65e0\u6cd5\u66f4\u6362\u81ea\u5b9a\u4e49\u5934\u50cf\u7684\u95ee\u9898<\/p>\n\n\n\n<p>\u867d\u70ed\u8fd9\u4e2a\u529f\u80fd\u4f7f\u7528\u573a\u666f\u548c\u9891\u7387\u90fd\u975e\u5e38\u4f4e\uff0c\u4f46\u5728\u6709\u65f6\u5019\u8fd8\u662f\u9700\u8981WordPress\u6765\u663e\u793a\u5934\u50cf\u7684\uff0c\u4f46\u662fwordpress\u5b98\u65b9\u7684\u5e76\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u6709\u6ce8\u518c\u8bbe\u7f6eGravatar\u5934\u50cf\u3002\u6240\u4ee5\u4fbf\u9700\u8981\u6211\u4eec\u624b\u52a8\u4e3aWordPress\u6dfb\u52a0\u540e\u53f0\u53ef\u81ea\u5b9a\u4e49\u4e0a\u4f20\u5934\u50cf\u7684\u529f\u80fd\uff0c\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"497\" src=\"https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang-1024x497.jpg\" alt=\"\" class=\"wp-image-755\" srcset=\"https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang-1024x497.jpg 1024w, https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang-300x146.jpg 300w, https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang-768x373.jpg 768w, https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang-1536x746.jpg 1536w, https:\/\/www.cuishaofeng.com\/wp-content\/uploads\/2024\/10\/shangchuantouxiang.jpg 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u81ea\u5b9a\u4e49\u5934\u50cf\n\nclass Simple_Local_Avatars {\n    private $user_id_being_edited;\n        \n    public function __construct() {\n        add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );\n        \n        add_action( 'admin_init', array( $this, 'admin_init' ) );\n        \n        add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );\n        add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );\n        \n        add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );\n        add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );\n        \n        add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );\n    }\n        \n    public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {\n        \n        if ( is_numeric($id_or_email) )\n            $user_id = (int) $id_or_email;\n        elseif ( is_string( $id_or_email ) &amp;&amp; ( $user = get_user_by( 'email', $id_or_email ) ) )\n            $user_id = $user->ID;\n        elseif ( is_object( $id_or_email ) &amp;&amp; ! empty( $id_or_email->user_id ) )\n            $user_id = (int) $id_or_email->user_id;\n        \n        if ( empty( $user_id ) )\n            return $avatar;\n        \n        $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );\n        \n        if ( empty( $local_avatars ) || empty( $local_avatars&#91;'full'] ) )\n            return $avatar;\n        \n        $size = (int) $size;\n        \n        if ( empty( $alt ) )\n            $alt = get_the_author_meta( 'display_name', $user_id );\n        \n        \/\/ generate a new size\n        if ( empty( $local_avatars&#91;$size] ) ) {\n            $upload_path = wp_upload_dir();\n            $avatar_full_path = str_replace( $upload_path&#91;'baseurl'], $upload_path&#91;'basedir'], $local_avatars&#91;'full'] );\n            $image_sized = image_resize( $avatar_full_path, $size, $size, true );      \n            \/\/ deal with original being >= to original image (or lack of sizing ability)\n            $local_avatars&#91;$size] = is_wp_error($image_sized) ? $local_avatars&#91;$size] = $local_avatars&#91;'full'] : str_replace( $upload_path&#91;'basedir'], $upload_path&#91;'baseurl'], $image_sized );\n            \/\/ save updated avatar sizes\n            update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );\n        } elseif ( substr( $local_avatars&#91;$size], 0, 4 ) != 'http' ) {\n            $local_avatars&#91;$size] = home_url( $local_avatars&#91;$size] );\n        }\n        \n        $author_class = is_author( $user_id ) ? ' current-author' : '' ;\n        $avatar = \"&lt;img alt='\" . esc_attr( $alt ) . \"' src='\" . $local_avatars&#91;$size] . \"' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' \/>\";\n        \n        return apply_filters( 'simple_local_avatar', $avatar );\n    }\n        \n    public function admin_init() {\n        \/\/load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '\/localization\/' );\n        \n        register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );\n        add_settings_field( 'simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );\n    }\n        \n    public function sanitize_options( $input ) {\n        $new_input&#91;'simple_local_avatars_caps'] = empty( $input&#91;'simple_local_avatars_caps'] ) ? 0 : 1;\n        return $new_input;\n    }\n        \n    public function avatar_settings_field( $args ) {       \n        $options = get_option('simple_local_avatars_caps');\n        \n        echo '\n            &lt;label for=\"simple_local_avatars_caps\">\n                &lt;input type=\"checkbox\" name=\"simple_local_avatars_caps\" id=\"simple_local_avatars_caps\" value=\"1\" ' . @checked( $options&#91;'simple_local_avatars_caps'], 1, false ) . ' \/>\n                ' . __('\u4ec5\u5177\u6709\u5934\u50cf\u4e0a\u4f20\u6743\u9650\u7684\u7528\u6237\u5177\u6709\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\u6743\u9650\uff08\u4f5c\u8005\u53ca\u66f4\u9ad8\u7b49\u7ea7\u89d2\u8272\uff09\u3002','simple-local-avatars') . '\n            &lt;\/label>\n        ';\n    }\n        \n    public function edit_user_profile( $profileuser ) {\n    ?>\n    &lt;h3>&lt;?php _e( '\u5934\u50cf','simple-local-avatars' ); ?>&lt;\/h3>\n        \n    &lt;table class=\"form-table\">\n        &lt;tr>\n            &lt;th>&lt;label for=\"simple-local-avatar\">&lt;?php _e('\u4e0a\u4f20\u5934\u50cf','simple-local-avatars'); ?>&lt;\/label>&lt;\/th>\n            &lt;td style=\"width: 50px;\" valign=\"top\">\n                &lt;?php echo get_avatar( $profileuser->ID ); ?>\n            &lt;\/td>\n            &lt;td>\n            &lt;?php\n                $options = get_option('simple_local_avatars_caps');\n        \n                if ( empty($options&#91;'simple_local_avatars_caps']) || current_user_can('upload_files') ) {\n                    do_action( 'simple_local_avatar_notices' );\n                    wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );\n            ?>\n                    &lt;input type=\"file\" name=\"simple-local-avatar\" id=\"simple-local-avatar\" \/>&lt;br \/>\n            &lt;?php\n                    if ( empty( $profileuser->simple_local_avatar ) )\n                        echo '&lt;span class=\"description\">' . __('\u5c1a\u672a\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u70b9\u51fb\u201c\u6d4f\u89c8\u201d\u6309\u94ae\u4e0a\u4f20\u672c\u5730\u5934\u50cf\u3002','simple-local-avatars') . '&lt;\/span>';\n                    else\n                        echo '\n                            &lt;input type=\"checkbox\" name=\"simple-local-avatar-erase\" value=\"1\" \/> ' . __('\u79fb\u9664\u672c\u5730\u5934\u50cf','simple-local-avatars') . '&lt;br \/>\n                            &lt;span class=\"description\">' . __('\u5982\u9700\u8981\u4fee\u6539\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u91cd\u65b0\u4e0a\u4f20\u65b0\u5934\u50cf\u3002\u5982\u9700\u8981\u79fb\u9664\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u9009\u4e2d\u4e0a\u65b9\u7684\u201c\u79fb\u9664\u672c\u5730\u5934\u50cf\u201d\u590d\u9009\u6846\u5e76\u66f4\u65b0\u4e2a\u4eba\u8d44\u6599\u5373\u53ef\u3002&lt;br\/>\u79fb\u9664\u672c\u5730\u5934\u50cf\u540e\uff0c\u5c06\u6062\u590d\u4f7f\u7528 Gravatar \u5934\u50cf\u3002','simple-local-avatars') . '&lt;\/span>\n                        ';     \n                } else {\n                    if ( empty( $profileuser->simple_local_avatar ) )\n                        echo '&lt;span class=\"description\">' . __('\u5c1a\u672a\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u5728 Gravatar.com \u7f51\u7ad9\u8bbe\u7f6e\u5934\u50cf\u3002','simple-local-avatars') . '&lt;\/span>';\n                    else\n                        echo '&lt;span class=\"description\">' . __('\u4f60\u6ca1\u6709\u5934\u50cf\u4e0a\u4f20\u6743\u9650\uff0c\u5982\u9700\u8981\u4fee\u6539\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u8054\u7cfb\u7ad9\u70b9\u7ba1\u7406\u5458\u3002','simple-local-avatars') . '&lt;\/span>';\n                }\n            ?>\n            &lt;\/td>\n        &lt;\/tr>\n    &lt;\/table>\n    &lt;script type=\"text\/javascript\">var form = document.getElementById('your-profile');form.encoding = 'multipart\/form-data';form.setAttribute('enctype', 'multipart\/form-data');&lt;\/script>\n    &lt;?php       \n    }\n        \n    public function edit_user_profile_update( $user_id ) {\n        if ( ! isset( $_POST&#91;'_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST&#91;'_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )            \/\/security\n            return;\n        \n        if ( ! empty( $_FILES&#91;'simple-local-avatar']&#91;'name'] ) ) {\n            $mimes = array(\n                'jpg|jpeg|jpe' => 'image\/jpeg',\n                'gif' => 'image\/gif',\n                'png' => 'image\/png',\n                'bmp' => 'image\/bmp',\n                'tif|tiff' => 'image\/tiff'\n            );\n        \n            \/\/ front end (theme my profile etc) support\n            if ( ! function_exists( 'wp_handle_upload' ) )\n                require_once( ABSPATH . 'wp-admin\/includes\/file.php' );\n        \n            $this->avatar_delete( $user_id );    \/\/ delete old images if successful\n        \n            \/\/ need to be more secure since low privelege users can upload\n            if ( strstr( $_FILES&#91;'simple-local-avatar']&#91;'name'], '.php' ) )\n                wp_die('For security reasons, the extension \".php\" cannot be in your file name.');\n        \n            $this->user_id_being_edited = $user_id; \/\/ make user_id known to unique_filename_callback function\n            $avatar = wp_handle_upload( $_FILES&#91;'simple-local-avatar'], array( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array( $this, 'unique_filename_callback' ) ) );\n        \n            if ( empty($avatar&#91;'file']) ) {     \/\/ handle failures\n                switch ( $avatar&#91;'error'] ) {\n                    case 'File type does not meet security guidelines. Try another.' :\n                        add_action( 'user_profile_update_errors', create_function('$a','$a->add(\"avatar_error\",__(\"\u8bf7\u4e0a\u4f20\u6709\u6548\u7684\u56fe\u7247\u6587\u4ef6\u3002\",\"simple-local-avatars\"));') );              \n                        break;\n                    default :\n                        add_action( 'user_profile_update_errors', create_function('$a','$a->add(\"avatar_error\",\"&lt;strong>\".__(\"\u4e0a\u4f20\u5934\u50cf\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a\",\"simple-local-avatars\").\"&lt;\/strong> ' . esc_attr( $avatar&#91;'error'] ) . '\");') );\n                }\n        \n                return;\n            }\n        \n            update_user_meta( $user_id, 'simple_local_avatar', array( 'full' => $avatar&#91;'url'] ) );      \/\/ save user information (overwriting old)\n        } elseif ( ! empty( $_POST&#91;'simple-local-avatar-erase'] ) ) {\n            $this->avatar_delete( $user_id );\n        }\n    }\n        \n    \/**\n     * remove the custom get_avatar hook for the default avatar list output on options-discussion.php\n     *\/\n    public function avatar_defaults( $avatar_defaults ) {\n        remove_action( 'get_avatar', array( $this, 'get_avatar' ) );\n        return $avatar_defaults;\n    }\n        \n    \/**\n     * delete avatars based on user_id\n     *\/\n    public function avatar_delete( $user_id ) {\n        $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );\n        $upload_path = wp_upload_dir();\n        \n        if ( is_array($old_avatars) ) {\n            foreach ($old_avatars as $old_avatar ) {\n                $old_avatar_path = str_replace( $upload_path&#91;'baseurl'], $upload_path&#91;'basedir'], $old_avatar );\n                @unlink( $old_avatar_path );   \n            }\n        }\n        \n        delete_user_meta( $user_id, 'simple_local_avatar' );\n    }\n        \n    public function unique_filename_callback( $dir, $name, $ext ) {\n        $user = get_user_by( 'id', (int) $this->user_id_being_edited );\n        $name = $base_name = sanitize_file_name( substr(md5($user->user_login),0,12) . '_avatar' );\n        $number = 1;\n        \n        while ( file_exists( $dir . \"\/$name$ext\" ) ) {\n            $name = $base_name . '_' . $number;\n            $number++;\n        }\n        \n        return $name . $ext;\n    }\n}\n        \n$simple_local_avatars = new Simple_Local_Avatars;\n        \nfunction get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {\n    global $simple_local_avatars;\n    $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );\n        \n    if ( empty ( $avatar ) )\n        $avatar = get_avatar( $id_or_email, $size, $default, $alt );\n        \n    return $avatar;\n}\n\/\/\u81ea\u5b9a\u4e49\u5934\u50cf \u7ed3\u675f\n\n<\/code><\/pre>\n\n\n\n<p>\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u52a0\u5165\u5230\u4f60\u4e3b\u9898\u7684<code>Functions.php<\/code>\u00a0\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5237\u65b0\u9875\u9762\u3002\u8fdb\u5165\u7528\u6237\u8d44\u6599\u9875\uff0c\u6ed1\u5230\u5e95\u90e8\uff0c\u70b9\u51fb\u4e0a\u4f20\u5934\u50cf\u5373\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8be5\u529f\u80fd\u4e3b\u8981\u662f\u7528\u4e8e\u514d\u63d2\u4ef6\u4e3a\u7528\u6237\u6dfb\u52a0\u5934\u50cf\uff0c\u89e3\u51b3\u5934\u50cf\u65e0\u6cd5\u663e\u793a\u6216\u65e0\u6cd5\u66f4\u6362\u81ea\u5b9a\u4e49\u5934\u50cf\u7684\u95ee\u9898 \u867d\u70ed\u8fd9\u4e2a\u529f\u80fd\u4f7f\u7528\u573a\u666f\u548c\u9891\u7387\u90fd\u975e\u5e38\u4f4e\uff0c\u4f46\u5728\u6709\u65f6\u5019\u8fd8\u662f\u9700\u8981WordPress\u6765\u663e\u793a\u5934\u50cf\u7684\uff0c\u4f46\u662fwordpress\u5b98\u65b9\u7684\u5e76\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u6709\u6ce8\u518c\u8bbe\u7f6eGravatar\u5934\u50cf&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"topic":[],"_links":{"self":[{"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/posts\/754"}],"collection":[{"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=754"}],"version-history":[{"count":1,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions\/756"}],"wp:attachment":[{"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=754"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.cuishaofeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}