Viewing 19 posts - 1 through 19 (of 19 total)
  • Author
    Posts
  • #1290633

    On the main menu of our site, I’m trying to add a message to one of the links saying they’re leaving the site and going to an external link. I guess this would be a leaving notice popup for external links. How would I add this?

    #1290677

    I’m trying to do something like this, but on a specific link on the main menu.

    #1290850

    Hi John,

    You can use the same code posted on that thread.
    Then go to Appearance > Menus > select Main Menu.
    Click Screen Options on the upper right part portion of the page, then check CSS Classes.
    Expand the menu item you’d like this to apply, then put external-link to CSS Classes (optional) and save.
    Hope this helps.

    Best regards,
    Nikko

    • This reply was modified 3 years, 8 months ago by Nikko.
    #1290950

    Where do I type the message, in the PHP?

    Would I use this or the other PHP mentioned?

    function add_custom_script(){
    ?>
    <script>
    jQuery(window).load(function(){
    	jQuery('a.external-link, .external-link a').click(function() {
    		alert("You're going to a better place.");
    	});
    });
    </script>
    <?php
    }
    add_action('wp_head', 'add_custom_script');
    #1290999

    Hi,

    No, actually just change this part of the code:

    jQuery('a.external-link, .external-link a').click(function() {

    to this:

    jQuery('.external-link a').click(function() {

    The whole code would be:

    function add_custom_script(){
    ?>
    <script>
    jQuery(window).load(function(){
    	jQuery('.external-link a').click(function() {
    		alert("You're going to a better place.");
    	});
    });
    </script>
    <?php
    }
    add_action('wp_head', 'add_custom_script');

    Just add this at the bottom of your child theme’s functions.php file.

    Best regards,
    Nikko

    #1291002

    And also adding external-link/strong> to CSS Classes (optional)?

    #1291013

    Hi John,

    I made a typo, it should only be: external-link

    Best regards,
    Nikko

    #1291123

    I was able to add this message, but how do I add a ‘Cancel’ button to this if someone accidentally clicks it?

    #1291237

    Hi John,

    Can you try to replace:

    alert("You're going to a better place.");

    with:

    confirm("You're going to a better place.");

    Best regards,
    Nikko

    #1297231

    Sorry for replying so late. That kind of worked. It has the ‘cancel’ and ‘ok’ buttons on the message, but if you click cancel, it still takes you to another page, you don’t stay on the site.

    #1297252

    Hi John,

    No worries.
    The cancel button in confirm should just return null and won’t do anything.
    Here’s some link that might be helpful: https://javascript.info/alert-prompt-confirm
    Can you give us steps on how we can replicate this on our end?

    Best regards,
    Nikko

    #1298861

    Click ‘Shop’ in the main navigation and once that notice comes up, click ‘Cancel’. It just continues to the other page anyway. It doesn’t cancel going to that link.

    #1298887

    first : one thing to mention – on new jQuery 3.5.1 – some of the functions are deprecated.
    You had to use.on('load', function() and .on('click', function ()
    and use instead return confirm ()

    so code will be:
    i tested it without $(window).on( 'load' , (function(){
    if it does not work – try then with it ( do not forget the closing brackets )

    maybe this will be enough for you to show:

    function add_custom_script(){
    ?>
    <script>
    (function($){
    	$('.external-link a').on('click', function () {
    		return confirm('Are you sure?');
    	});
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_script');
    • This reply was modified 3 years, 7 months ago by Guenni007.
    #1299017

    So instead of

    function add_custom_script(){
    ?>
    <script>
    jQuery(window).load(function(){
    	jQuery('.external-link a').click(function() {
    		confirm("You're going to a better place.");
    	});
    });
    </script>
    <?php
    }
    add_action('wp_head', 'add_custom_script');

    it would be

    function add_custom_script(){
    ?>
    <script>
    (function($){
    	$('.external-link a').on('click', function () {
    		return confirm('Are you sure?');
    	});
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_script');

    Is that right or am I adding that in addition?

    #1299027

    yes – and it costs only a few minutes to test it yourself – no hidden self-destruct button inside the code ;)

    #1299029

    This worked, thank you!

    #1299279

    Hi John,

    Great, I’m glad that you got it working. Please let us know if you should need any further help on the topic or if we can close it.

    Best regards,
    Rikard

    #1299395

    You can close it. Thanks!

    #1299407

    Hi John,

    We’re glad to hear that :)
    Thanks for using Enfold and have a great day!

    Best regards,
    Nikko

Viewing 19 posts - 1 through 19 (of 19 total)
  • The topic ‘Adding Leaving Notice Message to external link’ is closed to new replies.