常用的WordPress代码

February, 1st 2024 2 min read
php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
      /**
 \* 允许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  = 'image/svg+xml';
        $upload\_mimes = '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 ) {
 
            $check\_filetype  = wp\_check\_filetype( $filename, $mimes );
            $ext             = $check\_filetype;
            $type            = $check\_filetype;
            $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' );```