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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line