BuddyDev

Search

[Resolved] Hide any external link from guest.

Tagged: 

  • Participant
    Level: Initiated
    Posts: 2
    Metu on #7653

    Hi there,
    I am trying to hide all external links from guests. It will say for instant “You must be logged in to see the link”. I am using both buddypress and bbpress.
    I found a code snippet. But I don’t know how to modify it for my case.

    <?php
    function hide_links($post_content) {
    if ( in_category( array( ‘YOUR CAT ID’, ‘YOUR CAT ID’) )) {
    global $wpdb;

    return ($post_content = !is_user_logged_in()?preg_replace(‘/<a(.*?)>(.*?)<\/a>/m’, ‘<div style=”color:red;font-size:12px;”> YOUR TEXT</div>’,$post_content):$post_content);
    }

    }
    add_filter(‘the_content’, ‘hide_links’)

    ?>

    Can anyone help me please?

    Regards,
    Metu

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #7659

    Hello Metu,

    Thank you for posting here. Please use the following code in your active theme ‘functions.php’ file.

    
    function bd_hide_external_link( $content ) {
    
    	if ( is_user_logged_in() ) {
    		return $content;
    	}
    
    	$content = preg_replace_callback('/<a\s[^>]*?href=([\'"])(.+?)\1\s?[^>]*>([^<]+)<\/a>/im', function ($matches){
                    // replace the domain name with your domain
    		if ( strpos( $matches[2], 'facebook.com' ) !== false ){
    			return $matches[0];
    		}
    		// for external url
    		return '<a href="#">Login Please to see the link</a>';
    
    	}, $content );
    
    	return $content;
    }
    add_filter( 'the_content', 'bd_hide_external_link' );
    
    

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 2
    Metu on #7666

    Thank you so much Ravi for your kind reply. This snippet is working fine for blog posts. But it is not working for bbpress. Is it possible to modify the code accordingly?

  • Participant
    Level: Initiated
    Posts: 2
    ledoan on #38722

    I too it
    please

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38742

    Hello Metu, Ledoan,

    Thank you for asking. Please try use the following code:

    
    add_filter( 'bbp_get_topic_content', 'bd_hide_external_link' );
    add_filter( 'bbp_get_reply_content', 'bd_hide_external_link' );
    
    

    It will hide links from topic and reply content. Please do let me know if works or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 2
    ledoan on #38760

    Thanks Keymaster, I did it, thank you very much

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #38782

    Hello,

    Thank you for the acknowledgment. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Hide any external link from guest.’ is closed to new replies.

This topic is: resolved