• I wanna redirect my users to a custom page when they click on their display name and I have this code

    $('#display_name_button_id').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        window.location = 'link-to-custom-page.html';
    });

    And I am a newbie. Anyone please tell me what to do with the code to make it work.

Viewing 5 replies - 1 through 5 (of 5 total)
  • With most jQuery code like that you’ll need to wrap it in an action call to execute the code when the page has loaded enough to have all of the elemnts available. The most common problem, that I’m thinking your script also suffers from, is that the code tries to attach the handler function to a non-existant element, so it never actually gets attached so can’t be executed.

    You need something like this…

    jQuery (document).ready (
        // Your code goes in here!!
    );
    Thread Starter piko00001

    (@piko00001)

    Thanks for you reply. Can you please tell me in which file of wordpress shall I put it?

    Normally it would go in a Javascript file in your theme or plugin, and you’d use wp_enqueue_script() to add it to the <head> area. There’s other ways of doing it, but doing it that way is about the easiest way and makes sure that it’s always loaded.

    Thread Starter piko00001

    (@piko00001)

    Shall I create a new .js file and paste the code there. And what should be the name that file and at what format shall i paste the code. Please explain me the procedure. It would be a great help.

    You should create a new JS file if you don’t have an existing one that you can add to. You can name it anything that you want. As for format… you only need it as plain text with a .js file extension.

    The process is all explained here.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Help me with jquery code.’ is closed to new replies.