在WordPress 外掛程式中整合CSS 和jQuery:詳細指南
開發WordPress 外掛程式時,有必要包含自訂CSS 樣式和jQuery樣式和jQuery樣式和jQuery樣式和jQuery樣式表庫,用於增強插件的視覺外觀和功能。以下是如何實現此目的的綜合指南:
包含CSS
要在您的插件中包含CSS:
註冊你的樣式表使用wp_register_style():
wp_register_style('my-plugin-style', 'http://example.com/my-plugin-style.css');
使用wp_enqueue_style() 對已註冊的樣式表進行排隊:
wp_enqueue_style('my-plugin-style'); // Loads the stylesheet whenever needed
包括jQuery
包含外掛程式中的jQuery:
使用wp_script_is() 檢查🎜>
if (!wp_script_is('jquery')) { // jQuery is not loaded, proceed to load it }
wp_enqueue_script('jquery'); // Loads jQuery from WordPress's default repository
條件載入
wp_enqueue_script('my-plugin-script', 'http://example.com/my-plugin-script.js', array('jquery'));
條件載入
您可以僅在需要時通過將依賴項傳遞給wp_enqueue_script() 有條件地加載jQuery 或其他腳本:
add_action('wp_enqueue_scripts', 'callback_for_enqueuing_assets'); function callback_for_enqueuing_assets() { // Register and enqueue your assets here }
以上是如何將 CSS 和 jQuery 整合到您的 WordPress 外掛中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!