8

How can I add

<a href="javascript:function foo(){alert('hi');}" title="alert">
    Drag to your bookmarks bar
</a>

to my WordPress post?

I built a bookmarklet and I want to pass it through my blog, but WordPress is stripping out the javascript from my post when I save it.

6 Answers 6

2

Javascript in WordPress Single Pages and Posts

<script type="text/javascript">
<!--
// your code
//--></script>

Source: https://codex.wordpress.org/Using_Javascript

Sign up to request clarification or add additional context in comments.

Comments

0

I have no issues embedding that code in the HTML editor on my wordpress site. There is a problem with your javascript code- that defines a function, but never calls it. I have

<a href="javascript:alert('hi');" title="alert">Drag to your bookmarks bar</a>

in a post, and I get the alert when click, as well as the bookmarklet.

5 Comments

I have WP 3.2.1 installed and that link is turned into javascript:void(0); by WP
Interesting. I'll go through my settings again, but I didn't see anything to remove js in posts. Do you have any plugins that might be causing this?
No, not that I can see. All of our plugins are related to social media.
Are you admin, or just contributor?
Take a look at codex.wordpress.org/Using_Javascript#Javascript_in_Posts and plugins.trac.wordpress.org/wiki/TextControl but otherwise, I'm not seeing any settings... maybe something in your theme? I doubt that, but it's a possibility.
0

The issue is likely to be caused at the browser end. Chrome's XSS was the problem for me and I solved it by adding the line header ("X-XSS-Protection: 0"); to wp-blog-header.php in the root folder of my installation. This may not be ideal as it applies across the whole site. It would probably be better to add it where it would only apply to the post/page which needs to render the bookmarklet.

Comments

0

This is old, but still relevant with Version 4.9.5 for wordpress, so I answer with my solution for this:

Wordpress filters out any Javascript you use in your Posts or pages, that is why your code gets lost. I did the following steps to add a "javascript:" Link:

  1. Add the Link you want to your post, use "#" as the href and add a id to the tag (in Text-Mode of the Editor):
<a href="#" id="idOfYourLink">This is my JS Link</a>
  1. Install a Custom Javascript Plugin. I used Simple Custom CSS and JS
  2. Add the Javascript you want with help from the plugin:
jQuery(document).ready(function( $ ){
  function yourFunction() {
      alert("It works");
  }

  jQuery('#idOfYourLink').on("click", yourFunction);
});

The important part is adding the on-Handler to the Link you want to use. Now the Javascript is loaded right after the page got loaded. And a click on the link will call the function yourFunction

Comments

0

I guess that wordpress editor is used to work with textual data/content. So, to add your js you can find plugin for adding custom js. Also you can add a Custom field to the posts. enter image description here

Let it be "custom_js" - the name of a field that would contain js code. And then edit theme post temlate, adding "echo" of custom_js.

<?php
//single.php
echo get_post_meta( get_the_ID(), 'custom_js');
?>

Comments

0

In the Really Simple theme (good for hard coding web page layout for simplicity), in page.php, this line sends out main page content, including the title: get_template_part( 'template-parts/content', 'page' ); It also strips out javascript in the main page content. If you can find a good way to send back the title alone, comment this line out, and replace it with: echo get_post_field( 'post_content', get_the_ID(), 'raw' ); This does not filter out such javascript.

As for including the title (in h1 html tags), get_template_part( 'template-parts/content', 'title' ); works, but not quite right. It turns the title into a link.

Further looking into this matter, I found in the post_template.php file: function the_content( $more_link_text = null, $strip_teaser = false ). In this function, in my case, on lines 256 and 257: $content = apply_filters( 'the_content', $content ); and $content = str_replace( ']]>', ']]&gt;', $content );. I commented out both of these lines. That fixed it. <script> tags and javascript content is no longer filtered out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.