Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • Jeff Rivett
    Participant

    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 downloadclicked

    The 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.

    Hi 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,
    Aakash

    Jeff Rivett
    Participant

    Thanks 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.

    Jeff Rivett
    Participant

    Perhaps 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?

    Jeff Rivett
    Participant

    I 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 downloadclicked

    Thanks for your assistance.

    Hi Jeff,

    Good to know it works.

    You can remove [3][e].
    That is not necessary.

    Thanks,
    Aakash

    Jeff Rivett
    Participant

    > 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,
    Jeff

    Ok.. 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.

    Jeff Rivett
    Participant

    Okay, thanks. That works as well.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Subscribe to our Newsletter

Get updates on the WordPress plugins, tips and tricks to enhance your WordPress experience. No spam. View newsletter

By using this website, you agree to our Terms