• I’d like mask external links on wordpress (with domain name) and also make it available only for registered wp members. Is it possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Almost anything is possible with custom coding. Where do these links occur? Post content? Output by template code? Output can be conditionally altered based on the return value of is_user_logged_in(). How it is altered depends on where the links are coming from.

    Thread Starter dosyhost

    (@dosyhost)

    On post content.
    Some links: abc.com
    Some links: example.com
    I’d like to cover them with mysite.com
    and make it only available to members.

    Moderator bcworkz

    (@bcworkz)

    You can alter content just before it is output through the “the_content” filter. Your filter callback function can do a regexp (regular expression) search and replace for all links in content with something else, perhaps a link to a page encouraging visitors to become members. Of course only do this if is_current_user_logged_in() returns false.

    The PHP regexp search/replace function is preg_replace(). For example:
    $content = preg_replace('!(href=")(https?://.*)(")!', '$1mysite.com/recruit/$3', $content );
    This only matches href attributes that use double quotes. You can prevent links from being replaced by ensuring the href attribute uses single quotes. For example:
    <a href='https://not-replaced.com'>
    The example regexp also only replaces http and https protocol links. Links with protocols like mailto: and tel: will not be replaced. Naturally the regexp can be fine tuned to fit your specific needs.

    The sort of code that does this can be placed in functions.php of a child theme or some site specific plugin that contains custom code for the site.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Masking external links and make them only available for members?’ is closed to new replies.