包括外部代码到Wordpress插件

Include external code into Wordpress Plugin

本文关键字:Wordpress 插件 代码 包括外      更新时间:2023-09-26

我使用这个代码在我的插件中导入一个外部HTML文件:

<?php
   ...
   function showCalendar() {   
        include 'index.html';
   }
    add_shortcode( 'calendar', 'showCalendar' );
?>
但是在HTML中我有一些javascript代码,像这样:
<head>
    <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.11.1.js"></script>   
    <script type="text/javascript" src="jquery-ui.mycode.js"></script>      
</head>
<body>      
    <div class="box">                       
        <script>
            /*some code*/
        </script>   
    </div>
</body>

Wordpress没有执行该部分。我怎样才能解决这个问题呢?谢谢!

在。html文件中使用javascript文件的全路径

在Wordpress中你不能直接在文件中包含脚本。

您应该使用wp_enqueue_scripts包含您的脚本,或者您可以注册脚本并在以后需要时使用wp_register_scripts包含它。