dongteng2534 2016-12-24 13:24
浏览 77

WordPress wp_enque_style的行为与<link href = ...>不同

I am having a trouble trying to use a JavaScript code for upload files (http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/) in WordPress.

Following the recommendations of WordPress, I have created a plugin that enqueues the scripts and the styles required by the JavaScript code. The appearance of the script is:

function add_mini_ajax_file_upload_scrips() {
wp_enqueue_style('uploadBoxStyleFont', "http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" );
wp_enqueue_style('uploadBoxStyle', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/css/style.css" );
wp_enqueue_script('uploadBoxJquerymin', "https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
wp_enqueue_script('uploadBoxJqueryknob', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.knob.js");
wp_enqueue_script('uploadBoxJquerywidget', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.ui.widget.js");
wp_enqueue_script('uploadBoxJqueryiframe', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.iframe-transport.js");
wp_enqueue_script('uploadBoxJqueryupload', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.fileupload.js");
wp_enqueue_script('uploadBoxScript', "http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/script.js");
}
add_action( 'wp_enqueue_scripts', 'add_mini_ajax_file_upload_scrips' );

In this manner, the WordPress page looks as: enter image description here

As yo can see, a button with the text "Examinar ..." appears very uggly below the "Browse" button.

However, if I paste the following code directly on the WordPress Page:

<link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />
<link href="http://localhost/wordpress/assets/MiniAJAX_FileUploader/css/style.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.knob.js"></script>
<script src="http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.ui.widget.js"></script>
<script src="http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.iframe-transport.js"></script>
<script src="http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/jquery.fileupload.js"></script>
<script src="http://localhost/wordpress/assets/MiniAJAX_FileUploader/js/script.js"></script>

Then the page looks: enter image description here

Obviously, I want the appearance of the second way but WordPress recommends the first way to add JavaScript and styles.

Someone can tell me what is happening and how can I get the correct appearance (removing the uggly "Examinar" button)?

Thank you very much.

  • 写回答

2条回答 默认 最新

  • duanoucuo7045 2016-12-24 13:53
    关注

    Examine the order in which scripts/styles are loaded. Most likely, they are loaded in different order.

    WordPress assigns a unique id to each enqueued style-sheet or script as either unique-id-css or unique-id-js. You can see it in the generated <link> or <script> tag if you look in page source.

    You can use those ids (without the -css or -js part) to modify the order in which styles or scripts are loaded by passing the id in the array of dependencies ($deps) of wp_enqueue_script or wp_enqueue_style.

    You should stick to the WP way as it will avoid loading a resource multiple times which could cause hard to trace errors, especially for js scripts.

    For example, If you load them directly and another plugin/widget/theme script loads jquery, it will be loaded twice in your page, causing conflicts.

    Also please note jquery is already provided by WP, you don't need to load it from external source. Just specify it as dependence for any jquery dependant script.

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧