使用bing美图作为WordPress登录页面的背景图

网站APP 6 年前 回复

,

感谢: 皇家元林 投稿 文章来源: https://hjyl.org

    //自定义登录页面背景
    function custom_login_head(){
    $str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
    if(preg_match("/<url>(.+?)</url>/ies",$str,$matches)){
    $imgurl='http://cn.bing.com'.$matches[1];
    echo'<style type="text/css">body{background: url('.$imgurl.');background-attachment:fixed;width:100%;height:100%;background-image:url('.$imgurl.');background-attachment:fixed;-moz-background-size: 100% 100%;-o-background-size: 100% 100%;-webkit-background-size: 100% 100%;background-size: 100% 100%;-moz-border-image: url('.$imgurl.') 0;background-attachment:fixed;background-repeat:no-repeat9;background-image:none9;}h1 a { background-image:url('.get_bloginfo('url').'/favicon.ico)!important;width:32px;height:32px;-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;}#loginform {background-color:rgba(251,251,251,0.3)!important;}.login label,a{color:#000!important;}</style>';
    }}
    add_action('login_head', 'custom_login_head');
    add_filter('login_headerurl', create_function(false,"return get_bloginfo('url');"));
    add_filter('login_headertitle', create_function(false,"return get_bloginfo('name');"));

转自:https://www.nixonli.com/wordpress/18355.html

2019.10.06更新日志:
因为在WordPress中,file_get_contents这个函数是不提倡使用的,他会建议你用wp自身的系统WP_Filesystem代替,但是关于WP_Filesystem百度上基本上没人介绍过,大家可以查看官方文档

所以上述代码可以更新如下:

    //自定义登录页面背景
    function custom_login_head(){
    global $wp_filesystem;
    require_once ( ABSPATH . '/wp-admin/includes/file.php' );
    WP_Filesystem();
    $str = $wp_filesystem ->get_contents('https://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
    if(preg_match("/<url>(.+?)</url>/ies",$str,$matches)){
    $imgurl='http://cn.bing.com'.$matches[1];
    echo'<style type="text/css">body{background: url('.$imgurl.');background-attachment:fixed;width:100%;height:100%;background-image:url('.$imgurl.');background-attachment:fixed;-moz-background-size: 100% 100%;-o-background-size: 100% 100%;-webkit-background-size: 100% 100%;background-size: 100% 100%;-moz-border-image: url('.$imgurl.') 0;background-attachment:fixed;background-repeat:no-repeat9;background-image:none9;}h1 a { background-image:url('.get_bloginfo('url').'/favicon.ico)!important;width:32px;height:32px;-webkit-border-radius:50px;-moz-border-radius:50px;border-radius:50px;}#loginform {background-color:rgba(251,251,251,0.3)!important;}.login label,a{color:#000!important;}</style>';
    }}
    add_action('login_head', 'custom_login_head');
    add_filter('login_headerurl', create_function(false,"return get_bloginfo('url');"));
    add_filter('login_headertitle', create_function(false,"return get_bloginfo('name');"));

为什么WP_Filesystem是内置函数,又要引入/wp-admin/includes/file.php文件,不是很懂,不过WordPress检测插件没有查到任何问题,图片地址也正常显示,姑且这样解决吧!

欢迎大家指正探讨!

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)