Notifications
Clear all

Script [Solved] Hide external links from guests

14 Posts
3 Users
1 Reactions
1,751 Views
Posts: 39
Topic starter
(@stevenminix)
Trusted Member
Joined: 5 years ago

I know that wpforo on it's face does not provide the ability to hide external links from guests even though this feature is a well known popular feature used in forums all over the internet.

However FOR ME it is a game changing feature that I absolutely require. I just need a developer to give me the proper coding and where to place it in the plugins files. 

Here is the code I used to use but I have not been able to get it to work with wpforo function.php files.

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' );

So PLEASE I beg you to give me the info on what code to write and where to place it. I know this is possible.  

So PLEASE I beg you to give me the info on what code to write and where to place it. I know this is possible.

13 Replies
1 Reply
Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10587
Posted by: @stevenminix

I know that wpforo on it's face does not provide the ability to hide external links from guests even though this feature is a well known popular feature used in forums all over the internet.

However FOR ME it is a game changing feature that I absolutely require. I just need a developer to give me the proper coding and where to place it in the plugins files. 

Here is the code I used to use but I have not been able to get it to work with wpforo function.php files.

So PLEASE I beg you to give me the info on what code to write and where to place it. I know this is possible.

Update!

Here is a full solution to hide links for guests:

https://wpforo.com/community/general-discussions/hide-links-for-guests/#post-51489

Robert
Posts: 10587
Admin
(@robert)
Support Team
Joined: 9 years ago

Hi @stevenminix,

Your function may affect some content processes in wpForo posts content, for example, shortocdes, URLs controlling, image displaying. wpForo has dozens of processes on posts content. So if in future you got some problem with wpForo, first remove your function and test it again.

You can add your function to wpForo post content using wpForo posts content 'wpforo_content' hook:

add_filter( 'wpforo_content', 'bd_hide_external_link' );

Don't forget delete wpForo cache in Dashboard > Forums > Dashboard admin page before checking it.

9 Replies
(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @robert

Hi @stevenminix,

Your function may affect some content processes in wpForo posts content, for example, shortocdes, URLs controlling, image displaying. wpForo has dozens of processes on posts content. So if in future you got some problem with wpForo, first remove your function and test it again.

You can add your function to wpForo post content using wpForo posts content 'wpforo_content' hook:

add_filter( 'wpforo_content', 'bd_hide_external_link' );

Don't forget delete wpForo cache in Dashboard > Forums > Dashboard admin page before checking it.

Thank you so much for your reply. I am still confused though. Can you explain in more detail how I need to change my code and where exactly to place it, like what PHP file and does it need to be a specific line?

 

Did you mean just paste the code as i showed you only changing the last part where it says "add_filter and just changing it to "wpforo_content" because if so I tried to do that in the wpforo functions.php and it did not work.

 

Thank you for your help again

Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10587

@stevenminix,

If the function is in your theme functions.php, then you just need to insert it in wpForo Content hook using this code. Only this line of code should be added under your code:

add_filter( 'wpforo_content', 'bd_hide_external_link' );
(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @robert

@stevenminix,

If the function is in your theme functions.php, then you just need to insert it in wpForo Content hook using this code. Only this line of code should be added under your code:

add_filter( 'wpforo_content', 'bd_hide_external_link' );

Do you mean to place it in wpf-hooks.php?

 

so you are saying ad the full code to themes function.php and then add just the last part to wpf-hooks.php?

 

sorry for the confusion...

Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10587

@stevenminix

No, never add any code in wpForo core files.

All custom codes should be added in functions.php of your active theme.

BTW, where have you added your function above?

You should already know where to add that code.

Easily Add Custom Code in WordPress (without Breaking Your Site)

(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @robert

@stevenminix

No, never add any code in wpForo core files.

All custom codes should be added in functions.php of your active theme.

BTW, where have you added your function above?

You should already know where to add that code.

Easily Add Custom Code in WordPress (without Breaking Your Site)

Okay here is all i have done so far, BTW the links are still visible to guests in wpforo so this has not worked...

I added THIS code to my THEMES 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( 'wpforo_content', 'bd_hide_external_link' );

 

That is all I have done. please tell me what else I need to do to make it work because it is not working yet. thanks

Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10587

@stevenminix,

Ok all is correct.

Above, I asked you to delete your website cache and then delete wpForo cache clicking the [Delete all caches] button in Dashboard > Forums > Dashboard admin page.

(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @robert

@stevenminix,

Ok all is correct.

Above, I asked you to delete your website cache and then delete wpForo cache clicking the [Delete all caches] button in Dashboard > Forums > Dashboard admin page.

so I deleted the forum cache and my websites cache and still not working. Am I doing anything wrong? I am so desperate for this to work. BTW I even checked on my phone browser which i never use to make sure it is not something with my browser cache.

Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10587

@stevenminix,

I'm sorry but I have no other idea for you. Maybe the function doesn't work well or you still have a cache issue.

(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @robert

@stevenminix,

I'm sorry but I have no other idea for you. Maybe the function doesn't work well or you still have a cache issue.

The function works as it should with

add_filter( 'the_content', 'bd_hide_external_link' );

which hides all external links for guests in wordpress blog posts and pages(but not wpforo external links)

but when swapped with add_filter( 'wpforo_content', 'bd_hide_external_link' );

The function ceases to work at all anywhere,
Posts: 993
Moderator
(@martin)
Support Team
Joined: 8 years ago

@stevenminix,

Please leave a direct link to the post where I can see an external link. Also, leave some screenshot of the code you've instead in current active WordPress theme functions.php . 

I also recommend change this hooks:

add_filter( 'wpforo_content', 'bd_hide_external_link' );

to this:

add_filter( 'wpforo_content_after', 'bd_hide_external_link', 10, 2 );

 

And please change your function first line from this:

function bd_hide_external_link( $content ) {

to this:

function bd_hide_external_link( $content, $post = array() ) {
1 Reply
(@stevenminix)
Joined: 5 years ago

Trusted Member
Posts: 39
Posted by: @martin

@stevenminix,

Please leave a direct link to the post where I can see an external link. Also, leave some screenshot of the code you've instead in current active WordPress theme functions.php . 

I also recommend change this hooks:

add_filter( 'wpforo_content', 'bd_hide_external_link' );

to this:

add_filter( 'wpforo_content_after', 'bd_hide_external_link', 10, 2 );

 

And please change your function first line from this:

function bd_hide_external_link( $content ) {

to this:

function bd_hide_external_link( $content, $post = array() ) {

WOW! thank you so much your changes worked!