-
AuthorPosts
-
June 9, 2021 at 9:31 pm #9580Jeff RivettParticipant
Using the latest Announcer Pro.
I can’t get
Visitor conditions
>Custom function
to work.Here’s the very simple Javascript I’m using, and which I’ve confirmed runs when I click anywhere on the page:
function downloadclicked(){ alert('You clicked'); return true; } document.addEventListener("click", downloadclicked);
The Announcement is set up with the following Visitor condition:
Custom function IS downloadclickedThe announcement is configured to appear on every page.
If I remove the Visitor condition, the announcement does indeed appear on every page.But when I add the Visitor condition, clicking anywhere makes the alert appear, but the announcement never appears.
I must be doing something wrong.
June 10, 2021 at 12:34 am #9581Aakash ChakravarthyKeymasterHi Jeff,
With “Visitor conditions” -> Custom function you can write your own logic to say on which condition the announcement should be shown. In your case you are always returning true. So the announcement will be shown always (after closing the alert box)
Ideally it is for users to write some complex logic to decide if the announcement has to be shown when the page is loaded like checking cookies more than what is provided etc
In your case I think you wanted to show the announcement upon clicking the page right ?
You can try below code.
1. Remove the custom function you have configured
2. Go to display -> display announcement -> schedule
3. For Schedule duration –> selecte a older duration like 2 months before (this is to keep the announcement hidden)Now paste this code.
function downloadclicked(){ var my_annoucement = document.getElementById('ancr-37').announcer; my_announcement.show(); // or .hide(); } document.addEventListener("click", downloadclicked);
Here, when the page is clicked, then announcement will be shown (via
.show()
). Every announcer bar has a property called.announcer
. In above example, we are getting that and then calling.show()
of that.Change
37
to the ID of the announcer.If you want to target *any* announcement. Then you can select the announcer element by class name. Then you need to do something like in this page and do the same like above inside a for loop.
Thanks,
AakashJune 10, 2021 at 2:48 am #9582Jeff RivettParticipantThanks for the information and suggestions.
> In your case you are always returning true. So the announcement will be shown always (after closing the alert box)
In fact the announcement is NOT shown. If I remove the Custom Function, the announcement does show up on every page (even without clicking). As soon as I enable the custom fuction, it stops appearing.
I tried your suggested code, and the announcement still isn’t shown on clicking. If I remove the schedule settings you suggested, the announcement again appears on every page, regardless of any clicking.
June 10, 2021 at 5:53 pm #9583Jeff RivettParticipantPerhaps it would help if I explained what I’m trying to accomplish.
I would like an announcement to be shown when the user clicks a download link in the main menu (which appears on every page). Preferably, the announcement would appear after the download dialog, perhaps after a brief delay.
Is there some way I can accomplish this without using Custom Function?
June 10, 2021 at 6:58 pm #9584Jeff RivettParticipantI don’t totally understand why it’s working, but I did get it working.
[1] /wp-content/themes/<theme>/functions.php:
add_action('wp_enqueue_scripts', 'jrc_enqueue_custom_js'); function jrc_enqueue_custom_js() { wp_enqueue_script('jrcustom', get_stylesheet_directory_uri().'/js/jrcustom.js'); }
[2] /wp-content/themes/<theme>/js/jrcustom.js
function downloadclicked(){} jQuery(document).ready(function(){ jQuery( ".menu-item-4190" ).click(function() { var my_announcement = document.getElementById('ancr-4186').announcer; my_announcement.show(); // or .hide(); }); });
[3] Announcement (ID=4186)
[a] Display: Immediately; Immediate; Slide
[b] Position: Bottom; Sticky
[c] Close: Display close; Keep closed NO
[d] Location rules: Show in all pages
[e] Visitor conditions: Custom function IS downloadclickedThanks for your assistance.
June 11, 2021 at 11:16 pm #9594Aakash ChakravarthyKeymasterHi Jeff,
Good to know it works.
You can remove [3][e].
That is not necessary.Thanks,
AakashJune 12, 2021 at 12:01 am #9595Jeff RivettParticipant> You can remove [3][e].
> That is not necessary.Nope. If I remove Visitor conditions: Custom function IS downloadclicked, the announcement appears on every page.
Perhaps you could try this at your end to see what I’m seeing.
Thanks,
JeffJune 12, 2021 at 12:45 am #9596Aakash ChakravarthyKeymasterOk.. I didn’t notice that you are using “immediate” display. In that it will be shown. In your case, since you have set a custom function it returns false which is hiding the announcement.
So you can either set “display” as schedule and set a older date and remove visitor condition (or) continue to use custom function as you do now.
June 12, 2021 at 11:49 pm #9603Jeff RivettParticipantOkay, thanks. That works as well.
-
AuthorPosts
- You must be logged in to reply to this topic.