douhu7807 2018-06-18 22:36
浏览 336
已采纳

在wordpress中动态更改样式表中的url路径

I posted this to the wordpress stack exchange, but I also think its a good idea to post this here

Hello I found a function that when placed in the functions.php, it injects the entire stylesheet into the head on the website:

add_action( 'wp_head', 'internal_css_print' );
function internal_css_print() {
  echo '<style>';

  include_once get_template_directory() . '/style.css';

  echo '</style>';
}

It works great, the whole stylesheet goes in the head just fine, but one minor issue is that the urls in the css are broken. For example when the following is in the css file:

background: url('images/hero-desktop.jpg');

When the stylesheet is linked normally, this returns http://localhost:8888/wp-content/themes/my-theme/images/hero-desktop.jpg-> works

But when the stylesheet is injected in the head it returns: http://localhost:8888/images/hero-desktop.jpg --> doesnt work. The path is broken.

To fix this I can do a simple search and replace in the css to change url(' to url('/wp-content/themes/my-theme/ but there has to be a better way.

So I was thinking is there something to add to the function at the top, so before spitting out the whole css file into the head, it dynamically changes the url paths from url(/images/) to /wp-content/themes/my-theme/images/. If this is possible that would be great so its just a one time setup instead of searching and replacing css on an already developed site.

Thanks!

  • 写回答

1条回答 默认 最新

  • douna1895 2018-06-18 23:12
    关注

    One option is to extend an existing stylesheet using wp_add_inline_style(). This is probably the "most correct" way to do this:

    add_action( 'wp_enqueue_scripts', 'add_custom_styles' );
    function add_custom_styles(){
        $style_handle = 'style';
        $custom_css   = 'body {
            background: url('. get_stylesheet_directory_uri() .'/images/hero-desktop.jpg);
        }';
    
        wp_add_inline_style( $style_handle, $custom_css );
    }
    

    Of course, replace style with the handle for the stylesheet you want to extend.


    Another method of doing this is to use a "PHP CSS" file like so:

    add_action( 'wp_head', 'print_internal_css' );
    function print_internal_css(){
        echo '<style>';
            include get_stylesheet_directory().'/style.css.php';
        echo '</style>';
    }
    

    style.css.php:

    body {
        background: url( <?= get_stylesheet_directory_uri() .'/images/hero-desktop.jpg'; ?> );
    }
    

    Lastly, if you just want a quick-fix for what you have, you can just run your file through a simple str_replace function, and use file_get_contents or a cURL request instead of include:

    add_action( 'wp_head', 'print_internal_css' );
    function print_internal_css(){
        echo '<style>';
            $styles = file_get_contents( get_stylesheet_directory().'/style.css' );
            echo str_replace( "url('", "url('/wp-content/themes/my-theme/", $styles );
        echo '</style>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?