简体   繁体   English

js文件未包含在wordpress的主题中

[英]js file is not including in a theme in wordpress

I am new to wordpress. 我是Wordpress的新手。 I have created a plugin, it is working fine. 我已经创建了一个插件,它工作正常。 Now I have changed my theme of wordpress ( created my own theme ). 现在,我更改了wordpress的主题(创建了自己的主题)。 The problem is that the js file in the plugin is not including when the theme is my theme. 问题是当主题是我的主题时,插件中的js文件不包括在内。 The js file is including in other themes. js文件包含在其他主题中。 what should I do to solve this problem. 我应该怎么做才能解决这个问题。

I have used the below code in plugins first page to load the Jquery 我已经在插件首页中使用以下代码来加载Jquery

add_action('init', 'register_script');
function register_script() {
    wp_register_script( 'custom_jquery', plugins_url('/js/bootstrap.js', __FILE__), array('jquery'), '2.5.1' );
    wp_register_script( 'custom_jquery1', plugins_url('/js/jquery.min.js', __FILE__), array('jquery'), '2.5.1' );
    wp_register_script( 'cust_pluginjs1', plugins_url('/js/pluginjs.js', __FILE__), array('jquery'), '2.5.1' );
    wp_register_style( 'new_style', plugins_url('/css/style.css', __FILE__), false, '1.0.0', 'all');
    wp_register_style( 'new_style1', plugins_url('/css/bootstrap.css', __FILE__), false, '1.0.0', 'all');
    wp_register_style( 'new_style2', plugins_url('/css/font-awesome.css', __FILE__), false, '1.0.0', 'all');
    wp_localize_script( 'cust_pluginjs1', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

}
add_action('wp_enqueue_scripts', 'enqueue_style');

function enqueue_style(){
   wp_enqueue_script('custom_jquery');
   wp_enqueue_script('custom_jquery1');
   wp_enqueue_script('cust_pluginjs1');
   wp_enqueue_style( 'new_style' );
   wp_enqueue_style( 'new_style1' );
    wp_enqueue_style( 'new_style2' );
}



The Jquery used in the .js file is .js文件中使用的Jquery是

function domainsort(type)
{
  // jQuery.post('http://localhost/wptest/wp-admin/admin-ajax.php',data )
    jQuery.ajax({
                type: 'POST',
                url: myAjax.ajaxurl,
                data: {
                    action: 'sortfn',
                    sortId: type,

                },

                success:function(data){
                    if(data){
                     jQuery(".tablenpage").html(data);
                    }

                },
                error: function(MLHttpRequest, textStatus, errorThrown){
                    alert(errorThrown);
                }
            });
}

You are totally registering/enqueueing wrong here. 您完全在这里注册/排队错误。 You are registering your scripts in a function which you hook to init , and then you enqueue them in another function and hooking that to wp_enqueue_scripts 您正在将脚本注册到挂钩到init的函数中,然后将它们排队到另一个函数中,并将其挂钩到wp_enqueue_scripts

You should have your wp_register_script() and wp_enqueue_script() in the same function, and that function should then be hooked to wp_enqueue_scripts 您应该将wp_register_script()wp_enqueue_script()放在同一函数中,然后将该函数挂接到wp_enqueue_scripts

EDIT 编辑

You must never use the init hook to enqueue scripts or styles. 您绝不能使用init钩子来排队脚本或样式。 The proper hook to use is wp_enqueue_scripts 正确使用的钩子是wp_enqueue_scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM