• I’m trying to add a new link to my links manager, but when I put something like “javascript:someFunction()” in the web address field, the manager automatically disregards what I entered. I’ve figured out that the manager only allows you to enter strings with a valid protocol (ie. ‘http’, ‘https’, ‘ftp, etc…). I was able to get pass that by editing the “esc_url” function in the “formatting.php” file. This allowed me to save the link with the desired script entered in the web address field, but now when I try and use that link, wordpress inserts a ‘#’ in for ‘href’ attribute instead of my script. Anyone have any ideas on how to circumvent this issue?
    Thanks,
    Anthony

Viewing 3 replies - 1 through 3 (of 3 total)
    • 1. You shouldn’t be editing core files if at all possible.
    • 2. # being inserted is normal, as the link expects

      <a href="#" onclick="javascript:someFunction()">Your Link</a>

      The # just means you’re not taken away from the current page, and your function is instead executed on clicking

    • 3. You could simply add an empty link

      <a href="#" id="yourid" class"yourclass">Your Link</a>

      and apply a class/ID to it, then use Javascript to target the class/ID and execute that function.

    Thread Starter Anthony Hamill

    (@pardnerogmailcom)

    Thank you for the help! That is very much appreciated.
    Basically what I’m trying to accomplish is, I’ve got a set of links, but one is unique and uses javascript, whereas the others are just you run-of-the-mill external links. And like to display them all in a widget. Is there anyway to assign a unique class/id to certain links?
    Thanks again for the help!
    – Anthonyy

    hmmm…

    Are you building this list of links manually in a widget?

    If you are auto-generating this list, it may be harder. depending on your familiarity with wordpress/php/javascript, anything is possible.

    If you perhaps used jQuery, you could for example use the first child selector, to apply a specific class to the first link. Then in jQuery you could specify the function/action when a particular link with that class is clicked as you described before. You could use first child as I described, or last child, or nth child, if you know exactly what order your links will appear in.

    Hope this at least helps point you in the right direction.

    Simple jQuery example:

    <script>
        $("div a:first-child").addClass("myfunction");
        $('.myfunction').click(function(e) {
            e.preventDefault(); //stop link default action
            // add code for myfunction
        });
    </script>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use Javascript in Link – Link Manager’ is closed to new replies.