내 WordPress 플러그인에 CSS 및 jQuery를 포함하는 방법은 무엇입니까?
내 WordPress 플러그인에 CSS 및 jQuery를 포함하는 방법은 무엇입니까?
스타일 wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
그런 다음 사용하십시오 : wp_enqueue_style('namespace');CSS를로드하려는 곳.
스크립트는 위와 같지만 jquery를로드하는 더 빠른 방법은로드하려는 페이지의 init에로드 된 대기열을 사용하는 것입니다. wp_enqueue_script('jquery');
물론 jquery에 Google 저장소를 사용하고 싶지 않다면.
스크립트가 의존하는 jquery 라이브러리를 조건부로로드 할 수도 있습니다.
wp_enqueue_script('namespaceformyscript', 'http://locationofscript.com/myscript.js', array('jquery'));
2017 년 9 월 업데이트
나는 얼마 전에이 대답을 썼다. 스크립트와 스타일을 대기열에 넣을 수있는 가장 좋은 위치는 wp_enqueue_scripts후크 안에 있음을 분명히해야합니다 . 예를 들면 다음과 같습니다.
add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');
function callback_for_setting_up_scripts() {
wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
wp_enqueue_style( 'namespace' );
wp_enqueue_script( 'namespaceformyscript', 'http://locationofscript.com/myscript.js', array( 'jquery' ) );
}
이 wp_enqueue_scripts작업은 "프런트 엔드"를 설정합니다. admin_enqueue_scripts백엔드에 대한 작업 (wp-admin 내의 모든 위치) 및 login_enqueue_scripts로그인 페이지에 대한 작업을 사용할 수 있습니다 .
init()플러그인 의 함수에 넣으십시오 .
function your_namespace() {
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
}
add_action( 'admin_init','your_namespace');
나에게 절대적인 imho 인 (나를 위해) 최고의 솔루션을 찾기까지 시간이 좀 걸렸습니다.
건배
플러그인에 CSS 및 jQuery를 포함하는 것은 간단합니다.
// register jquery and style on initialization
add_action('init', 'register_script');
function register_script() {
wp_register_script( 'custom_jquery', plugins_url('/js/custom-jquery.js', __FILE__), array('jquery'), '2.5.1' );
wp_register_style( 'new_style', plugins_url('/css/new-style.css', __FILE__), false, '1.0.0', 'all');
}
// use the registered jquery and style above
add_action('wp_enqueue_scripts', 'enqueue_style');
function enqueue_style(){
wp_enqueue_script('custom_jquery');
wp_enqueue_style( 'new_style' );
}
나는이 사이트에서이 훌륭한 것을 발견 했다 WordPress에 jQuery와 CSS를 포함하는 방법 – WordPress Way
도움이되기를 바랍니다.
http://codex.wordpress.org/Function_Reference/wp_enqueue_script 참조
예
<?php
function my_init_method() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
}
add_action('init', 'my_init_method');
?>
@pixeline의 답변에 추가하기 위해 (간단한 의견을 추가하려고 시도했지만 사이트는 50 명성이 필요하다고 말했습니다).
관리자 섹션에 대한 플러그인을 작성하는 경우 다음을 사용해야합니다.
add_action('admin_enqueue_scripts', "add_my_css_and_my_js_files");
The admin_enqueueu_scripts is the correct hook for the admin section, use wp_enqueue_scripts for the front end.
Accepted answer is incomplete. You should use the right hook: wp_enqueue_scripts
Example:
function add_my_css_and_my_js_files(){
wp_enqueue_script('your-script-name', $this->urlpath . '/your-script-filename.js', array('jquery'), '1.2.3', true);
wp_enqueue_style( 'your-stylesheet-name', plugins_url('/css/new-style.css', __FILE__), false, '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");
First you need to register the style and css using wp_register_script() and wp_register_style() functions
//registering javascript and css
wp_register_script ( 'mysample', plugins_url ( 'js/myjs.js', __FILE__ ) );
wp_register_style ( 'mysample', plugins_url ( 'css/mystyle.css', __FILE__ ) );
After this you can call the wp_enqueue_script() and wp_enqueue_style() functions for loading the js and css in required page
wp_enqueue_script('mysample');
wp_enqueue_style('mysample');
I fount a nice example here http://wiki.workassis.com/wordpress-create-advanced-custom-plugin-using-oop/
Very Simple:
Adding JS/CSS in the Front End:
function enqueue_related_pages_scripts_and_styles(){
wp_enqueue_style('related-styles', plugins_url('/css/bootstrap.min.css', __FILE__));
wp_enqueue_script('releated-script', plugins_url( '/js/custom.js' , __FILE__ ), array('jquery','jquery-ui-droppable','jquery-ui-draggable', 'jquery-ui-sortable'));
}
add_action('wp_enqueue_scripts','enqueue_related_pages_scripts_and_styles');
Adding JS/CSS in WP Admin Area:
function enqueue_related_pages_scripts_and_styles(){
wp_enqueue_style('related-pages-admin-styles', get_stylesheet_directory_uri() . '/admin-related-pages-styles.css');
wp_enqueue_script('releated-pages-admin-script', plugins_url( '/js/custom.js' , __FILE__ ), array('jquery','jquery-ui-droppable','jquery-ui-draggable', 'jquery-ui-sortable'));
}
add_action('admin_enqueue_scripts','enqueue_related_pages_scripts_and_styles');
You can use the following function to enqueue script or style from plugin.
function my_enqueued_assets() {
wp_enqueue_script('my-js-file', plugin_dir_url(__FILE__) . '/js/script.js', '', time());
wp_enqueue_style('my-css-file', plugin_dir_url(__FILE__) . '/css/style.css', '', time());
}
add_action('wp_enqueue_scripts', 'my_enqueued_assets');
You can add scripts and css in back end and front end with this following code: This is simple class and the functions are called in object oriented way.
class AtiBlogTest {
function register(){
//for backend
add_action( 'admin_enqueue_scripts', array($this,'backendEnqueue'));
//for frontend
add_action( 'wp_enqueue_scripts', array($this,'frontendEnqueue'));
}
function backendEnqueue(){
wp_enqueue_style( 'AtiBlogTestStyle', plugins_url( '/assets/css/admin_mystyle.css', __FILE__ ));
wp_enqueue_script( 'AtiBlogTestScript', plugins_url( '/assets/js/admin_myscript.js', __FILE__ ));
}
function frontendEnqueue(){
wp_enqueue_style( 'AtiBlogTestStyle', plugins_url( '/assets/css/front_mystyle.css', __FILE__ ));
wp_enqueue_script( 'AtiBlogTestScript', plugins_url( '/assets/js/front_myscript.js', __FILE__ ));
}
}
if(class_exists('AtiBlogTest')){
$atiblogtest=new AtiBlogTest();
$atiblogtest->register();
}
참고URL : https://stackoverflow.com/questions/3760222/how-to-include-css-and-jquery-in-my-wordpress-plugin
'Program Club' 카테고리의 다른 글
| Python의 문자열에서 숫자가 아닌 모든 문자 ( "."제외)를 제거합니다. (0) | 2020.12.06 |
|---|---|
| 런타임에 UIBarButtonItem에 대한 대상 및 작업을 설정하는 방법 (0) | 2020.12.06 |
| MultiAutoCompleteTextView와 AutoCompleteTextView의 차이점 (0) | 2020.12.06 |
| 월 및 연도 별 SQL 그룹화 (0) | 2020.12.06 |
| MVVM이 EventArgs를 명령 매개 변수로 전달 (0) | 2020.12.06 |