WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to call a javascript function after user attaches something to post. I hooked the add_attachment hook, and processed what I want. However, how can I call a function in a Javascript loaded in the post edit page? Just printing the javascript in the function doesn't work, as I guessed.

function analyse_attachment($attachment_ID) {
    // Some awesome codes here
    $var = 5;

    //Trying to call a javascript function here
    print "<script>
        alert('I am never shown.');
        AJavaScriptFunctiononLoadedinPostPage('passing parameter $var');

// attach our function to the correct hook
add_action("add_attachment", 'analyse_attachment');
add_action("edit_attachment", 'analyse_attachment');

Maybe I need to do something with media_send_to_editor hook?

share|improve this question

did you try the image editor class hooks?

try: image_editor_save_pre

more information about the hooks changes: http://make.wordpress.org/core/2012/12/06/wp_image_editor-is-incoming/


share|improve this answer

I would like to call a javascript function after user attaches something to post.

Please check the source code of media.php file itself in SVN. We do call javascript function by default if the user initiates it, before and / or after an user attaches typically an image. That is the basic reason why we can click an already attached image in a post (inside the Editor, obviously) and a popup comes. Sometimes it get corrupted and users complain, that happens if TinyMCE files get corrupted. I hope you have understood the flow of action. You can also see the files in official trac. You will get some references and links to bugs as well.

how can I call a function in a Javascript loaded in the post edit page

After action the restriction. There is restriction of MIME types (for example - RFC 2183( //tools.ietf.org/html/rfc2183) is allowed, that is JPEG format). You can not attach an php file in the way as attachment like you do with an image. The function is restricted. You can not even attach a proprietary file like *.idraw file at all. You will get security error.

Yes your idea is actually right, There three files that currently handles the work (not limited to) :


"something to post" is not actually technically right terminology, you actually have to explain the specific need either in words or via diagram.

Please check TinyMCE's docs as well.

As WordPress project is a Free Software project and there is excellent collaboration for the modification of core files, please refer to the documents for Plugin or Theme development or participate in make.wordpress.org If you want to extend the features, thats great; directly mention the type and intention of usage. Show the full source code in any code repository for your needed usage (Plugin or Theme).

For all normally used files there are already existing default functions or Plugins (like for attaching a MP3 as podcast, we can either use the default function or install a plugin to get more features).

share|improve this answer

use $.ready jQuery(document).ready(function(){ your code }); before then alert() something

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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