WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to modify all href and src links? (6 posts)

  1. imartianguy
    Member
    Posted 9 months ago #

    I need a functionality where when a specific condition 'x' is met, ALL the href and src links on a page modify such that:
    http://example.com/wp-content/themes/twentytwelve/style.css
    http://example.com/wp-content/uploads/2013/06/MartianGuy_icon.jpg
    http://example.com/what-we-do/
    etc should become:
    http://test.example.com/wp-content/themes/twentytwelve/style.css
    http://test.example.com/wp-content/uploads/2013/06/MartianGuy_icon.jpg
    http://test.example.com/what-we-do/

    So basically the domain should change to a subdomain.

    I am trying to do it using DOMDocument function but am not sure exactly how. Please help.

  2. Shaun Scovil
    Member
    Posted 9 months ago #

    You could just do a redirect, like this:

    function my_custom_redirect() {
        if ( is_page( 'test' ) ) {
            global $wp;
            $current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
            $new_url = str_replace( 'example.com', 'test.example.com', $current_url );
            header( "Location: {$new_url}" );
            exit;
        }
    }
    add_action( 'wp', 'my_custom_redirect' );

    In this example, the conditional tag checks if the current page is a page with a slug of test and, if true, replaces example.com with test.example.com and redirects.

    Would that work in your case? It would certainly be more efficient than doing a preg_replace and changing all href and src attributes...

    CREDIT: The method for retrieving the current URL was shamelessly yoinked from this blog post by Konstantin Kovshenin.

  3. imartianguy
    Member
    Posted 9 months ago #

    Thanks for the response.

    If I got this right, it would only work if the url is something like http://example.com/test or http://example.com/user/test/ ? And if that's the case, I am afraid it wouldn't work for me.

    So if you could help me with the preg_replace method, I'll be greatful.

  4. Shaun Scovil
    Member
    Posted 9 months ago #

    The method above could work in any case where the URL you want and the current URL are identical, except for the sub-domain. You would just need to change the conditional tag in the if statement.

    There really isn't an easy or good way to preg_replace every href and src value. You'd have to write a function to change all of the enqueued script and style URLs; another to filter the content; and possibly several others. It would be a total hack.

  5. Shaun Scovil
    Member
    Posted 9 months ago #

    Here is an alternative version of that function, which uses wp_redirect:

    function my_custom_redirect() {
        if ( is_page( 'test' ) ) {
            global $wp;
            $current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
            wp_redirect( preg_replace( 'example.com', 'test.example.com', $current_url ), 301 );
            exit;
        }
    }
    add_action( 'wp', 'my_custom_redirect' );
  6. imartianguy
    Member
    Posted 9 months ago #

    Hey, I was away. I just looked at your last response. This should do what I want. I'll give it a go today. Thanks again!

Reply

You must log in to post.

About this Topic