jquery - How to create javascript function in custom Wordpress plugin -


i have create custom plugin listing candidate using wp_list_table. have show fields information in popup , on click of href. below step, including javascript, it's not working.

  1. in plugin file included js file as:

    include wp_content_dir."/plugins/candidate_cv_listing/includes.php"; 

    calling js function this:

    <a href="#"  onclick ="show_details('.$item['id'].');" >click here </a>' 
  2. in includes.php following code had written:

         if( $hook != 'candidate_cv_listing.php' )      return;    // core      wp_enqueue_script('jquery');    //echo '<script type="text/javascript"     src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">   </script>'; // registering js file wp_register_script('can_list',plugin_dir_url( __file__ ).'cand.js'); wp_enqueue_script('can_list',plugin_dir_url( __file__ ).'cand.js'); } add_action('admin_enqueue_scripts', 'candidates_gen_js'); ?> 
  3. now in cand.js file writting js code as::

    function show_details(id){   //alert(id);   datastring = 'id='+id;   $.ajax({     url: "<?php echo plugins_url();?>/candidate_cv_listing/list_detail.php",      type: "post",     data: datastring,     success: function(result){       $.fancybox(result);     }   }); } 

please let me know correct method of including js plugin, since plugin used in admin-end , how should make call function. right giving me:

referenceerror: show_details not defined 

in wordpress, wp_enqueue_script gets used output script page. wp_register_script gets used if need register script can used anywhere need used (on page).

if need script used elsewhere, should register first, enqueue afterwards anywhere need it.

if register script first, need call first parameter gave when registered script wp_register_script , use in wp_enqueue_script part.

example:

// register script, dependant on jquery wp_register_script('can_list',plugin_dir_url( __file__ ).'cand.js', array('jquery')); // enqueue script output onto page: wp_enqueue_script('can_list'); 

than able use: wp_enqueue_script('can_list'); anywhere else need load at.

if need output script in spot only, simple this:

// script output on page, loading jquery first. wp_enqueue_script('can_list',plugin_dir_url( __file__ ).'cand.js', array('jquery')); 

let me know if need further information on this.

you put these in function called on either hook: admin_enqueue_scripts (if needed in admin section), and/or wp_enqueue_scripts (if needed on front-end section).

honestly, think issue more related echo of html , php onclick handler of $item['id'].

for outputting php, should use print or echo. prefer using echo feel better performance of site. ofcourse, there millions of other methods output html, etc., 2 used , important situation.

for example:

<a href="#" onclick ="show_details('.$item['id'].');" >click here </a>'

should this:

<?php echo '<a href="#" onclick="show_details('.$item['id'].');">click here</a>'; ?>

or this

<a href="#" onclick="show_details(<?php echo $item['id']; ?>);">click here</a>


Comments