Jump to content

Announcement visible to logged in users only?


purathal

Recommended Posts

Hello,

How to make an announcement page private to registered members only instead of making it publicly accessible for all?   This condition can also apply for all announcements, not just one.

I don't see any option to do this within GUI, any workaround using custom Hooks?

As an example, the announcement page URL looks like this: https://domain.com/whmcs/index.php/announcements/myannouncement.html

Is there a way check to see if the the URL string has the keyword "announcements" and redirect to login page if the user is already not logged?

Many Thanks.

 

 

Link to comment
Share on other sites

17 hours ago, purathal said:

How to make an announcement page private to registered members only instead of making it publicly accessible for all?   This condition can also apply for all announcements, not just one.

it depends on how thorough you want to be.... you could remove the links from the navbars and sidebars using hooks; you could redirect visitors to an announcement page to another page unless they are logged in; you could remove the announcements from the homepage.

18 hours ago, purathal said:

Is there a way check to see if the the URL string has the keyword "announcements" and redirect to login page if the user is already not logged?

you wouldn't need to do that - that third hook link I posted will redirect anyone visiting any announcement page, who isn't logged in, to another page.... looking at it again though, you might need to add your domain to the URL used in the hook as you could run into relative path redirect issues if you don't.

Link to comment
Share on other sites

Thank you! The redirection works. However, I still have a couple of issues that you might be able to help me with...

1) I still don't see the "Announcement" link/button show up on the top navigation bar for logged on users. It is hidden from the non-logged on users as expected. There are couple other hooks in place so I am not sure if they are all conflicting with each other.

Below are three different hooks I got so far...  Pleaes let me know what I need to do to make the "Announcement" button show up on the top nav bar. Maybe something to do with the header template file?

 

<?php


use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
    if (!is_null($primaryNavbar->getChild('Support'))) {
        $primaryNavbar->getChild('Support')->removeChild('Announcements');
        $primaryNavbar->getChild('Support')->removeChild('Network Status');
        $primaryNavbar->getChild('Support')->removeChild('Knowledgebase');
        $primaryNavbar->getChild('Support')->removeChild('Downloads');
    }
});

?>

<?php
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{

    if (!is_null($primaryNavbar->getChild('Network Status'))) {
        $primaryNavbar->removeChild('Network Status');
    }
	 if (!is_null($primaryNavbar->getChild('Announcements'))) {
       $primaryNavbar->removeChild('Announcements');
    }

	 if (!is_null($primaryNavbar->getChild('Knowledgebase'))) {
        $primaryNavbar->removeChild('Knowledgebase');
    }
	 if (!is_null($primaryNavbar->getChild('Contact Us'))) {
        $primaryNavbar->removeChild('Contact Us');
    }
	
});
?>

*******And, finally this below one from your previous post.*******


<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
   if (!is_null($primaryNavbar->getChild('Network Status'))) {
       $primaryNavbar->removeChild('Network Status');
   }

   if (!is_null($primaryNavbar->getChild('Announcements'))) {
       $primaryNavbar->removeChild('Announcements');
   }
});


 

 

2) I published a test announcement and I see it listed under the "Recent News" box shown on the homepage of the Client Area. I hover over and it shows me the URL in the form of https://whmcs.domain.com/index.php/announcements/3/Test-Announcement.html . Clicking on it sure enough shows me the actual announcement and with your above hook visiting the above URL redirects to the login page. Now, the issue is if I click on the actual "Announcement" button anywhere within the Client Area it takes me to the page with the URL https://whmcs.domain.com/announcements.php and it shows the text "There are no announcements to display."  

 I am a bit confused with the above behavior. Why can't see the announcements when simply visiting the URL https://whmcs.domain.com/announcements.php instead of what appears to be a SEO friendly URL above?

 

Thank you again.

Link to comment
Share on other sites

18 hours ago, purathal said:

1) I still don't see the "Announcement" link/button show up on the top navigation bar for logged on users. It is hidden from the non-logged on users as expected. There are couple other hooks in place so I am not sure if they are all conflicting with each other.

from that hook code you posted, the first of them is removing the Announcements navbar links for logged in users - get rid of that and  the Announcements link should be visible again for logged in users...

4tCgeR2.png

18 hours ago, purathal said:

Below are three different hooks I got so far...  Please let me know what I need to do to make the "Announcement" button show up on the top nav bar. Maybe something to do with the header template file?

if you want to remove the announcement navbar link for those not logged in, the entire navbar hook should be...

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->getChild('Announcements'))) {
		$primaryNavbar->removeChild('Announcements');
	}
});

you won't need the rest of your hook code.

19 hours ago, purathal said:

2) I published a test announcement and I see it listed under the "Recent News" box shown on the homepage of the Client Area. I hover over and it shows me the URL in the form of https://whmcs.domain.com/index.php/announcements/3/Test-Announcement.html . Clicking on it sure enough shows me the actual announcement and with your above hook visiting the above URL redirects to the login page.

if you're seeing the "Recent News" homepage panel in the client area, then you must be logged in as a client - so clicking on any announcement link shouldn't redirect you, that redirection should only occur for users who are not logged in.

19 hours ago, purathal said:

Now, the issue is if I click on the actual "Announcement" button anywhere within the Client Area it takes me to the page with the URL https://whmcs.domain.com/announcements.php and it shows the text "There are no announcements to display."  

when you say "Announcement" button, where do you mean? if you have an announcement published in the db, then it should be listed on the above page for those that are logged in.... anyone not logged in should be invited to do so.

19 hours ago, purathal said:

I am a bit confused with the above behavior. Why can't see the announcements when simply visiting the URL https://whmcs.domain.com/announcements.php instead of what appears to be a SEO friendly URL above?

you should be able to - go back to basics, remove any of these hooks you've added for the announcements, then add the above navbar hook; test the announcements page (should work for everyone); then add the redirection hook in and see what happens when you visit the page... ultimately both these hooks should be in the same file, but that won't matter whilst you're testing.

Link to comment
Share on other sites

Hi,

I removed all the hooks. I will revisit the hooks part after we sort out the below issue.

I did the following:  WHMCS Admin Panel -> Support -> Announcements -> Add New Announcement -> I create a test announcement and set it to published -> Save Changes.

I then log onto the WHMCS Client Area (using a test client account) - > The above Announcement I posted now shows up under the "Recent News" box located within the home page panel. Under the Client Area if I visit the URL 
https://whmcs.domain.com/index.php/announcements I can view the announcements I posted. However, if I visit the URL https://whmcs.domain.com/announcements.php it simply shows me the text "There are no announcements to display.".

So, my question is why the above behavior?  Because, if I enable the "Announcements" buttons (it doesn't matter whether for "logged on users" or "not logged on users") that button's hyperlink is always https://whmcs.domain.com/announcements.php. So, do I have to setup a a URL rewrite where visiting/clicking the above URL will automatically will redirect to https://whmcs.domain.com/index.php/announcements  page so users can actually see the posted announcements?

Currently the under Admin Panel -> General Settings -> Friendly URLs -> currently set to use "Friendly index.php"

I understand the above setting would cause the behavior https://whmcs.domain.com/index.php/announcements  format but I am just little puzzled to understand why https://whmcs.domain.com/announcements.php  shows no announcements and all the "Announcements" button by default link to https://whmcs.domain.com/announcements.php . So, without a redirect to https://whmcs.domain.com/index.php/announcements  it seem to be useless.

 

 

Link to comment
Share on other sites

Hi @purathal

18 hours ago, purathal said:

I removed all the hooks. I will revisit the hooks part after we sort out the below issue.

good move. thanks.png

19 hours ago, purathal said:

I did the following:  WHMCS Admin Panel -> Support -> Announcements -> Add New Announcement -> I create a test announcement and set it to published -> Save Changes.

so far, so good.

19 hours ago, purathal said:

I then log onto the WHMCS Client Area (using a test client account) - > The above Announcement I posted now shows up under the "Recent News" box located within the home page panel. Under the Client Area if I visit the URL 
https://whmcs.domain.com/index.php/announcements I can view the announcements I posted. However, if I visit the URL https://whmcs.domain.com/announcements.php it simply shows me the text "There are no announcements to display.".

it shouldn't do that. headshake.gif

19 hours ago, purathal said:

So, my question is why the above behavior?

three thoughts - it's either an older version of WHMCS that had Friendly URL issues; it's a custom or outdated theme (e.g not Six) or... hell i've forgotten the third - anyway, rule out the other two and I might remember what the third one was.

19 hours ago, purathal said:

Because, if I enable the "Announcements" buttons (it doesn't matter whether for "logged on users" or "not logged on users") that button's hyperlink is always https://whmcs.domain.com/announcements.php.

with Six, the sidebar and navbar links when using Friendly Index, will point to index.php/announcements and not announcements.php - I might need to see a screenshot of these buttons as i'm not sure if you're using an old custom template that has buttons that point to announcements.php (which would have been the page to use years ago - and technically still is), or if you're describing the sidebars as buttons.

19 hours ago, purathal said:

So, do I have to setup a a URL rewrite where visiting/clicking the above URL will automatically will redirect to https://whmcs.domain.com/index.php/announcements  page so users can actually see the posted announcements? 

if it's a custom template, then you should be able to edit it - if it's Six, or a theme based on six, and these are sidebar/navbar links, then they can be redirected using a hook.... though that said, they shouldn't need to be because if I go to announcements.php in the browser, it goes to the Announcements page without issue and shows a list of announcements.

19 hours ago, purathal said:

Currently the under Admin Panel -> General Settings -> Friendly URLs -> currently set to use "Friendly index.php"

have you tried switching to either of the other options in the Friendly URL settings ?

19 hours ago, purathal said:

I understand the above setting would cause the behavior https://whmcs.domain.com/index.php/announcements  format but I am just little puzzled to understand why https://whmcs.domain.com/announcements.php  shows no announcements and all the "Announcements" button by default link to https://whmcs.domain.com/announcements.php . So, without a redirect to https://whmcs.domain.com/index.php/announcements  it seem to be useless.

as I said, it shouldn't do that - as an illustration, let's use a WHMCS site, chosen at random from a Google search, that uses index.php/announcements... if you follow each link, they should both show the announcements (there is only one)...

that's what should be occurring on your site.

Link to comment
Share on other sites

The main site is WordPress based and we paid WHMCS Integration service (the actual WHMCS people) to do the WHMCS client area integration/customization.  Under "General Settings -> Template" it is currently selected to use a custom theme name. I don't know if the custom template was based on "Six" template or not. Anyways, as a test switching to "Six" template within the admin panel now, also produces the same behavior as described before with announcements under the Client Area.

I am referring to "Announcements" buttons anywhere within the Client Area. As an example, attached screenshot might also help (screenshot pulled from the actual sample site/url link you provided but the idea is the same under our site too).

Not sure what to look for or edit under the custom template file to fix this issue. Alternatively, please let me know how to setup the hook to redirect the links.

Something new - How can I get the "Announcement" button to show up on the top bar? Like the second attached screenshot. Currently mine is hidden under Top Bar ->  Support -> Announcement.

Thank you.

 

 

 

2019-07-28 00_38_44-Announcements.png

2019-07-28 00_43_23-Announcements.png

Link to comment
Share on other sites

5 hours ago, purathal said:

The main site is WordPress based and we paid WHMCS Integration service (the actual WHMCS people) to do the WHMCS client area integration/customization. 

oh dear. 😞

i'm tempted to say to go back to them and ask if anything they've done is causing this...

5 hours ago, purathal said:

Under "General Settings -> Template" it is currently selected to use a custom theme name. I don't know if the custom template was based on "Six" template or not. Anyways, as a test switching to "Six" template within the admin panel now, also produces the same behavior as described before with announcements under the Client Area.

did you try changing the Friendly URL settings as I suggested ?

5 hours ago, purathal said:

Not sure what to look for or edit under the custom template file to fix this issue. Alternatively, please let me know how to setup the hook to redirect the links.

the URLs used in those sidebars/navbars are determined, or should normally be, by the Friendly URL settings - they can be changed with a hook, but i'm reluctant to do so until we find the reason why the URLs are wrong... which version of WHMCS are you using ???

you could use the hook in the thread below, remove the parts you don't need (most of it) and just keep the announcements redirection - but as I said, if I were you, i'd find out the cause of the issue rather than papering over the cracks with a hook.

5 hours ago, purathal said:

Something new - How can I get the "Announcement" button to show up on the top bar? Like the second attached screenshot. Currently mine is hidden under Top Bar ->  Support -> Announcement.

when you're logged in as a client? that's perfectly normal... effectively, you'd have to use another hook to delete where the link is currently and add it again as a new link on the navbar... i've probably posted that hook as well previously... but i'd really suggest you focus on why the core URL of these announcements links is wrong before you start trying to fix it with hooks.

Link to comment
Share on other sites

My WHMCS version is 7.7.1.  I changed the Friendly URLs and it doesn't make any difference with  https://whmcs.domain.com/announcements.php  URL as that particular link always shows the text "There are no announcements to display."

Though it may not be the best course of action, I am OK with setting up a URL redirect for to fix the above issue either through .htaccess and/or through a hook. Please let me know how you recommend that I go about it and how to do it, where the above link should redirect users to https://whmcs.domain.com/index.php/announcements  

For the last part, yes I was referring to the WHMCS client area. Please let me know the hook to delete the "announcements" from the current location (listed under "Support" drop-down ) and add it as a new link on the top navbar.

 

 

Link to comment
Share on other sites

7 hours ago, purathal said:

My WHMCS version is 7.7.1.  I changed the Friendly URLs and it doesn't make any difference with  https://whmcs.domain.com/announcements.php  URL as that particular link always shows the text "There are no announcements to display."

that would worry me, but for a quiet life, let's just assume it's a quirk of your install. 🙂

7 hours ago, purathal said:

Though it may not be the best course of action, I am OK with setting up a URL redirect for to fix the above issue either through .htaccess and/or through a hook.

go with a hook - htaccess can be more trouble than it's worth... especially with FU in the mix.

7 hours ago, purathal said:

Please let me know how you recommend that I go about it and how to do it, where the above link should redirect users to https://whmcs.domain.com/index.php/announcements  

the previous hook I linked to will do that...

<?php

# Menu/Sidebar Link Changes Hook for CJF077
# Written by brian!

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar)
{
	if (!is_null($secondarySidebar->getChild('Support'))) {
		$secondarySidebar->getChild('Support')
			->getChild('Announcements')
			->setURI('index.php/announcements');
	}
});

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->getChild('Announcements'))) {
		$primaryNavbar->removeChild('Announcements');
	}

	if (!is_null($primaryNavbar->getChild('Support'))) {
		$primaryNavbar->getChild('Support')
			->removeChild('Announcements');
	}
	
  	$client = Menu::context('client'); 
	if (!is_null($client)) {
		$primaryNavbar->addChild('New Link', array(
			'uri' => 'index.php/announcements',
			'label' => Lang::trans('announcementstitle'),
			'order' => 40));
	}

});
7 hours ago, purathal said:

Please let me know the hook to delete the "announcements" from the current location (listed under "Support" drop-down ) and add it as a new link on the top navbar.

the above hook....

  1. changes link in sidebar
  2. removes Announcements link from navbar for non-logged in users
  3. removes Announcements link from support navbar for logged in users
  4. adds Announcements link to navbar for logged in users... if you need to change it's position on the navbar, just change the 'order' value in the hook.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated