Tagged: Hide links
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,
MetuHello 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
RaviHello 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
The topic ‘ [Resolved] Hide any external link from guest.’ is closed to new replies.