Change all occurrences of “http” to “https” on a wordpress page

后端 未结 8 1504
心在旅途
心在旅途 2021-02-04 19:33

I am in the process of implementing SSL on some of my wordpress-powered site\'s pages. Currently I\'m getting mixed content warnings on the secured pages - my custom theme inclu

相关标签:
8条回答
  • 2021-02-04 20:29

    I agree with the other posters who suggest that there are better ways to do what you are after. With that said, it sounds like you're in a bind, so let me offer a crack at it. (BTW, hat tip and a +1 vote to the protocol relative URL; I didn't know about that!)

    Anyway, I assume what you are after is in <a> tags, but it should be easy to extrapolate this to others:

    if (document.location.protocol === 'https:') {
        $('a').each(function() {
            var href = $(this).attr('href');
            if (href.indexOf('http:') > -1) {
                href = href.replace('http:', 'https:');
                $(this).attr('href', href);
            }
        });
    }
    

    With this help offered, I would encourage you to see if there's a safer / more practical way to do what you are trying to do. I will also mention that this approach will likely only work for links; modifying CSS and script references after the page loads will certainly backfire and not get you the result you want.

    Notice the ":" in "document.location.protocol === 'https:'".

    0 讨论(0)
  • 2021-02-04 20:35

    It is better to change the legacy URLs in database level, IMHO. To replace all http:// occurrences with protocol-agnostic //, run these SQLs:

    UPDATE wp_posts 
    SET    post_content = ( Replace (post_content, 'src="http://', 'src="//') )
    WHERE  Instr(post_content, 'jpeg') > 0 
            OR Instr(post_content, 'jpg') > 0 
            OR Instr(post_content, 'gif') > 0 
            OR Instr(post_content, 'png') > 0;
    

    For single-quoted occurrences:

    UPDATE wp_posts 
    SET   post_content = ( Replace (post_content, "src='http://", "src='//") )
    WHERE  Instr(post_content, 'jpeg') > 0 
            OR Instr(post_content, 'jpg') > 0 
            OR Instr(post_content, 'gif') > 0 
            OR Instr(post_content, 'png') > 0;
    

    For more, check here

    0 讨论(0)
提交回复
热议问题