我给插件添加样式正确吗?

时间:2015-07-13 作者:user2875165

今天我注意到两个插件(开放时间和联系人表单7)存在风格冲突。为了解决这个问题,我决定让开放时间的样式表和js只出现在管理页面上。经过一些研究,这就是我想到的。看起来不错,但还好吗?谢谢

<?
/**
*   Register Styles
*/
function op_register_styles_backend() {
wp_enqueue_style(\'jQuery-ui-timepicker\', op_baseurl() . \'/js/jQuery.ui.timepicker/jquery.ui.timepicker.css\', false, false, \'all\');
wp_enqueue_style(\'jQuery-ui-style\', \'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css\', false, false, \'all\');
wp_enqueue_style(\'opening-hours-backend\', apply_filters( \'op_backend_stylesheet\', op_baseurl() . \'/css/backend.css\' ), false, false, \'all\');

wp_register_script(\'jQuery-ui\', \'http://code.jquery.com/ui/1.10.3/jquery-ui.js\', false, null, false);
wp_register_script(\'jQuery-ui-timepicker\', op_baseurl() . \'/js/jQuery.ui.timepicker/jquery.ui.timepicker.js\', false, null, false);
wp_enqueue_script(\'jQuery-ui\');
wp_enqueue_script(\'jQuery-ui-timepicker\');
}

function op_register_styles_frontend() {
wp_register_style(\'opening-hours-frontend\', apply_filters( \'op_frontend_stylesheet\', op_baseurl().\'/css/frontend.css\' ), false, false, \'all\');
wp_enqueue_style( \'opening-hours-frontend\' );

if (file_exists( op_assets_path().\'/custom_style.css\' )) :
    wp_register_style( \'opening-hours-user-stylesheet\', op_assets_path().\'/custom_style.css\', false, false, all );
    wp_enqueue_style( \'opening-hours-user-stylesheet\' );
endif;
}

add_action(\'wp_enqueue_scripts\', \'op_register_styles_frontend\');

/**
 *  Create Opening Hours Instance
 */
$wp_opening_hours   = new OpeningHours;

/**
*   Register Backend Options Pages
*/
 function op_register_options_pages() {
  // Top level menu item
$opening_hours = add_menu_page(
    apply_filters( \'op_menu_title_opening_hours\', __(\'Opening Hours\', op_textdomain()) ),
    apply_filters( \'op_menu_title_opening_hours\', __(\'Opening Hours\', op_textdomain()) ),
    apply_filters( \'op_min_user_capability\', \'manage_options\' ),
    \'opening-hours\',
    \'op_setup_page\'
);
add_action( \'admin_print_styles-\' . $opening_hours, \'op_register_styles_backend\' );

// Holidays Page
$holidays = add_submenu_page(
    \'opening-hours\',
    apply_filters( \'op_menu_title_holidays\', op__(\'Holidays\') ),
    apply_filters( \'op_menu_title_holidays\', op__(\'Holidays\') ),
    apply_filters( \'op_min_user_capability\', \'manage_options\' ),
    \'opening-hours-holidays\',
    \'op_holidays_page\'
);
add_action( \'admin_print_styles-\' . $holidays, \'op_register_styles_backend\' );

// Special Openings Page
$special_openings = add_submenu_page(
    \'opening-hours\',
    apply_filters( \'op_menu_title_special_openings\', op__(\'Special Openings\') ),
    apply_filters( \'op_menu_title_special_openings\', op__(\'Special Openings\') ),
    apply_filters( \'op_min_user_capability\', \'manage_options\' ),
    \'opening-hours-special-openings\',
    \'op_special_openings_page\'
);
add_action( \'admin_print_styles-\' . $special_openings, \'op_register_styles_backend\' );

// Settings Page
$settings = add_submenu_page (
    \'opening-hours\',
    apply_filters( \'op_menu_title_settings\', op__(\'Settings\') ),
    apply_filters( \'op_menu_title_settings\', op__(\'Settings\') ),
    apply_filters( \'op_min_user_capability\', \'manage_options\' ),
    \'opening-hours-settings\',
    \'op_settings_page\'
);
   add_action( \'admin_print_styles-\' . $settings, \'op_register_styles_backend\' );
}

function op_setup_page() {
global $wp_opening_hours;
// include template
require_once    op_basepath() . \'/templates/setup-page.php\';
}

function op_holidays_page() {
global $wp_opening_hours;
// include template
require_once    op_basepath() . \'/templates/holidays-page.php\';
}

function op_special_openings_page() {
global $wp_opening_hours;
// include template
require_once    op_basepath() . \'/templates/special-openings-page.php\';
}

function op_settings_page() {
global $wp_opening_hours;
// include template
require_once    op_basepath() . \'/templates/settings-page.php\';
}

function op_admin_styles() {
/*
* Call backend styles ONLY if on an admin page.
*/
add_action(\'admin_init\', \'op_register_styles_backend\');
}

function manage_options() {
    /* Output admin pages. */
}

add_action (\'admin_menu\', \'op_register_options_pages\');
?>

1 个回复
最合适的回答,由SO网友:AddWeb Solution Pvt Ltd 整理而成

是的,这是正确的方法。

我们中的许多人使用样式来改变网站的外观,并使用脚本来增强功能。然而,需要注意的是,将这些脚本添加到WordPress的方式与这些文件的内容一样重要。我们需要使用WordPress排队功能,而不是将它们放到页眉或页脚文件中。

查看WordPress Codex中包含的脚本列表或直接指向功能参考/wp enqueue style.

如果您对此有任何疑问/担忧,请告知我。

结束