1

Can anyone here give me a code to create a click-to-information link? The user clicks on a link and a message appears.

I have a javascript code that does it, but its destroyed when the page is viewed in visual mode. It seems it can't be used on wordpress pages, unless there is some kind of additional code, which, being a wordpress newbie, I don't know.

Th idea is simple. Click on a link. An information box appears. The user clicks Okay. The infobox closes.

Here is the old Javascript, just for comparison.

</FONT></SPAN></B><A HREF="#" onClick="alert('This is a message.')"><SPAN
STYLE="Font-Size : 10pt"><FONT FACE="Tahoma">This is the link to click.</FONT></SPAN></A><B><SPAN STYLE="Font-Size : 10pt">
1
  • The old JS script did not appear after I submitted this question. Sorry. Commented Apr 1, 2017 at 20:56

1 Answer 1

2

By default scripts are stripped out when in visual view.

So there are a couple of options:

  1. Never use visual mode again (let's be honest, that's not going to happen ;) )
  2. Create a shortcode that will do the alert for you.

In your functions.php file add the following

/**
 * [alert_link url="https://example.com" message="This is the message" text="This is the link to click"]
 *
 * @param $atts
 * @param null $content
 *
 * @return string
 */
function my_alert_shortcode( $atts, $content = null ) {
    // Set up your defaults and read from the shortcode parameters you enter in the page.
    $atts = shortcode_atts(
        array(
            $url = '#', // This is the default URL for your shortcode should you leave that parameter blank
            $message = 'This is the message', // This would be the default message should you leave that parameter blank
            $text = 'This is the link to click', // This would be the default text that is made an anchor
        ), $atts, 'alert_link' );

    // Now format your output to include the parameters from above.
    $output = '<a class="my-alert-link" href="' . $url . '" onClick="alert(\'' . $message . '\')">' . $text . '</a>';

    return $output; // Return
}
add_shortcode( 'alert_link', 'my_alert_shortcode' ); / This registers your shortcode for use.

Once in place you'd just enter the shortcode like this and replace the default values as required:

[alert_link url="https://example.com" message="This is the message" text="This is the link to click"]

Shortcodes are a super helpful way to do things like add script or extra functionality without having to program it into the editor (which is inherently unsafe, which is why scripts get stripped).

Check out the WordPress shortcode documentation here: https://codex.wordpress.org/Shortcode_API

A quick note too on the HTML you're using. While valid, FONT tags and spans used to set font-size, family, etc. are pretty darn old and should be replaced with CSS if possible.

Checkout HTML 5 Web Developer guide for semantics etc. https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.