1

I have been setting up my plugin in the wordpress admin area. I have a form that stores information from the user. In my file input type, I have javascript function call to my custom javascript that I link to it. This is the line: <li>Category Image:<input type="file" name="category[image]" id="cat_image" onchange="javascript:ImageUpload()" /></li>. however, it returns an error that says undefined function ImageUpload(), surely the function wasn't able to be located. What I was thinking is maybe there is a wordpress function to call for this or is there I need to set up first? But I already link my custom js and it is working. The problem is how to call certain js function from my wordpress php file in the admin area? I hope you guys understand this. Thanks. Here are my codes:

plugin_file.php

 <form method="post" name="new_category" id="product_category" enctype="multipart/form-data"> 
 <ul class="add_prod">
 <li>Category Title:<input type="text" name="category[title]" id="cat_title" value="" placeholder="Title" /> </li>
 <li>Category Description:<textarea rows="4" cols="40" name="category[description]"></textarea></li>
 <li>Category Image:<input type="file" name="category[image]" id="cat_image" onchange="javascript:ImageUpload()" /></li>
 <li><input type="submit" name="submit" id="submit" value="Submit details" class="btn-submit"/></li>
 </ul>  
 </form>

js file:

function ImageUpload() {
    $("#return").show();
    $("#return").html('');
    $("#return").html('<img src="images/load2.gif" alt="Uploading...."/> wait, uploading...');
    $("#imageform").ajaxForm({
        target: '#return',
        success: function() {
            $("#return").fadeOut(10000);
        }
    }).submit();  

}

1 Answer 1

1

Make sure your js file is loaded before being called .

You can do something like this

<?php
    add_action('wp_head','js_call'); // make sure js is loaded in head

    function js_call() {  ?>
    <script>

    function ImageUpload() {
        jQuery("#return").show();
        jQuery("#return").html('');
        jQuery("#return").html('<img src="images/load2.gif" alt="Uploading...."/> wait, uploading...');
        jQuery("#imageform").ajaxForm({
            target: '#return',
            success: function() {
                jQuery("#return").fadeOut(10000);
            }
        }).submit(); 


    </script>

<?php    } ?>
Sign up to request clarification or add additional context in comments.

7 Comments

But I already loaded it with add_action( 'admin_enqueue_scripts', array('jun_product','theme_name_scripts')); What is the different with that with wp_head?
And they are a separate file. The js is separated to my plugin file. So I will just call a js function from my plugin file to my custom js file
with admin_enqueue_scripts it will load your js code for backend pages only, try this add_action( 'wp_enqueue_script', array('jun_product','theme_name_scripts')) codex.wordpress.org/Function_Reference/wp_enqueue_script
To make sure your js is loaded check your page source
I check my page source now. Yes it is loaded but still didn't work,still undefine function error return
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.