常用的WordPress代码

作者: LAOXU, 更新日期: 2024-02-01

允许svg格式文件上传

/**
 * Allow SVG uploads for administrator users.
 *
 * @param array $upload_mimes Allowed mime types.
 *
 * @return mixed
 */
add_filter(
    'upload_mimes',
    function ( $upload_mimes ) {
        // By default, only administrator users are allowed to add SVGs.
        // To enable more user types edit or comment the lines below but beware of
        // the security risks if you allow any user to upload SVG files.
        if ( ! current_user_can( 'administrator' ) ) {
            return $upload_mimes;
        }
 
        $upload_mimes['svg']  = 'image/svg+xml';
        $upload_mimes['svgz'] = 'image/svg+xml';
 
        return $upload_mimes;
    }
);
 
/**
 * Add SVG files mime check.
 *
 * @param array        $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
 * @param string       $file Full path to the file.
 * @param string       $filename The name of the file (may differ from $file due to $file being in a tmp directory).
 * @param string[]     $mimes Array of mime types keyed by their file extension regex.
 * @param string|false $real_mime The actual mime type or false if the type cannot be determined.
 */
add_filter(
    'wp_check_filetype_and_ext',
    function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
 
        if ( ! $wp_check_filetype_and_ext['type'] ) {
 
            $check_filetype  = wp_check_filetype( $filename, $mimes );
            $ext             = $check_filetype['ext'];
            $type            = $check_filetype['type'];
            $proper_filename = $filename;
 
            if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
                $ext  = false;
                $type = false;
            }
 
            $wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
        }
 
        return $wp_check_filetype_and_ext;
 
    },
    10,
    5
);

删除前端代码中的WordPress版本号

add_filter('the_generator', '__return_empty_string');

禁用自动更新后的邮件通知

// Disable auto-update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
 
// Disable auto-update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );
 
// Disable auto-update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );

停用xml-rpc

add_filter( 'xmlrpc_enabled', '__return_false' );

修改摘要长度

add_filter(
    'excerpt_length',
    function ( $length ) {
        // 把40改成需要显示的字数即可,注意是word数,不是letter数
        return 40;
    },
    500
);

停止自动更新

// 停止WordPress本身的自动更新
add_filter( 'auto_update_core', '__return_false' );
// 停止插件自动更新
add_filter( 'auto_update_plugin', '__return_false' );
// 停止主题自动更新
add_filter( 'auto_update_theme', '__return_false' );

关于我

一个熟悉WordPress建站推广的外贸人。

在自己折腾的过程中,把经验积累记录下来,主要包括网站主机、后端、前端、内容营销、Google Ads、网站数据分析、加速优化等,纯佛系输出。

如果我的文章刚好对你有用,那不胜荣幸;如果你在WP建站过程中遇到了一些技术问题,或许我可以解决其中的80-90%。read more...

关注公众号,获取最新文章推送。

210 studio wechat