如何在 WordPress 插件中导入 jQuery?

ash*_* vc -3 javascript wordpress jquery

我刚刚写了一个插件,但我不知道如何在这里将 javascript 包含到 WordPress 中。任何人都可以帮助我,我正在制作一个表格,它将存储到数据库中。

jquery(document).ready(function() {
  jquery('#type_of_moving').click(function(){
    jquery(".option_wrap").css("display", "block");
    jquery('#disable_option :not(:selected)').attr('disabled','disabled');           
    var select_type = jquery('.option_wrap :selected').val();            
    jquery('#type_of_moving').val(select_type);
  }); 
           
  jquery(document).ready(function(){
    var current = 1,current_step,next_step,steps;
    steps = jquery("fieldset").length;
    jquery(".next").click(function(){
      jquery(".previous").css("display", "block");
      current_step = jquery(this).parent();
      next_step = jquery(this).parent().next();
      next_step.show();
      current_step.hide();
    });
    jquery(".previous").click(function(){
      current_step = jquery(this).parent();
      next_step = jquery(this).parent().prev();
      next_step.show();
      current_step.hide();
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

Arg*_*ong 5

main.js例如,将您的代码放在一个文件中。将其上传到您的插件根文件夹。并将此代码放入您的主插件文件,以使 WordPress 在前端加载您的 Javascript 代码。

function wp_plugin_enqueue_scripts() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'script-name', plugin_dir_url(__FILE__) . '/main.js', array('jquery') );
}
add_action( 'wp_enqueue_scripts', 'wp_plugin_enqueue_scripts' );
Run Code Online (Sandbox Code Playgroud)

P/s:确保您的 javascript 有效。jquery不同于jQuery