Relative URLs in WordPress

前端 未结 8 1281
执念已碎
执念已碎 2020-12-04 07:29

I\'ve always found it frustrating in WordPress that images, files, links, etc. are inserted into WordPress with an absolute URL instead of relative URL. A relative url is mu

8条回答
  •  半阙折子戏
    2020-12-04 07:55

    I solved it in my site making this in functions.php

    add_action("template_redirect", "start_buffer");
    add_action("shutdown", "end_buffer", 999);
    
    function filter_buffer($buffer) {
        $buffer = replace_insecure_links($buffer);
        return $buffer;
    }
    function start_buffer(){
        ob_start("filter_buffer");
    }
    
    function end_buffer(){
        if (ob_get_length()) ob_end_flush();
    }
    
    function replace_insecure_links($str) {
    
       $str = str_replace ( array("http://www.yoursite.com/", "https://www.yoursite.com/") , array("/", "/"), $str);
    
       return apply_filters("rsssl_fixer_output", $str);
    
    }
    

    I took part of one plugin, cut it into pieces and make this. It replaced ALL links in my site (menus, css, scripts etc.) and everything was working.

提交回复
热议问题