0

I'm trying to load a .js file from a wp plugin.

The code where I load jquery, jquery-UI and my .js file look like this, and is located inside the "main" plugin file:

//Load Java and Jquery
function load_jquery() {

    // only use this method is we're not in wp-admin
    if (!is_admin()) {

        // deregister the original version of jQuery
        wp_deregister_script('jquery');
        wp_deregister_script('jquery-ui');
        wp_deregister_script('lyox-script');

        // discover the correct protocol to use
        $protocol='http:';
        if($_SERVER['HTTPS']=='on') {
            $protocol='https:';
        }

        // register the Google CDN version
        wp_register_script('jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2');
        wp_register_script('jquery-ui', $protocol.'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', false, '1.10.3');
        wp_register_script('lyox-script', plugins_url( '/includes/script.js' , __FILE__ ), array( 'jquery', 'jquery-ui' ) );

        // add it back into the queue
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui');
        wp_enqueue_script('lyox-script');


    }

}

add_action('template_redirect', 'load_jquery');

Then inside the .js file I have the following code, where the post() function is added to a form button onclick="post();":

$(document).ready(function() {

        function post() {

        var name = $('#name').val();


             $.post('process.php', {postname:name},
                    function(data)
                        {
                        alert(data);
                        $('#result').html(data);                        
             });
    }
});

Still nothing happens when I try it out on a page. Any ideas?

1 Answer 1

1

You can do like this using admin ajax : I will not explain the functions . you can google it and learn .

Step 1: Localize some values for your script to be used in your javascript files,

get admin-ajax.php url

$author_nonce = wp_create_nonce( 'nk_author' );


    wp_localize_script( 'nk_script', 'nk_object',array( 'nk_ajax_url' => admin_url( 'admin-ajax.php' ) , 'nk_plugin_url' => plugins_url() ,'nk_author' => $author_nonce) );

step 2 : In your script you can do this .

var data = {
        action: 'nk_action', // the function that will be called in your plugin
        _ajax_nonce :   nk_object.nk_author, // nonce for security
        id : 'mydata'   //your data to be sent
        };

            //(admin ajax url , your data , callback for response )
        jQuery.post(nk_object.nk_ajax_url, data, function(response) {

               $('#nk_result').html(response);


            }


        });//end jQuery.post

step 3 : In your plugin.php file do this

<?php
add_action('wp_ajax_nk_action', 'nk_action_callback');


function nk_action_callback() {
    check_ajax_referer('nk_author');

    if(isset($_POST['id']))
    {
        $id=$_POST['id'];
            echo $id;
    }
die();//dont forget to write die
}?>

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.