2014-02-26 2 views
0

Wordpress 플러그인을 만들고 일부 js 파일 작업을 시작합니다. js 파일에서 PHP를 사용하려면 PHP header("Content-type: text/javascript")을 사용하고 나머지 자바 스크립트 코드를 맨 아래에 삽입하기 시작합니다. WP_PLUGIN_URL을 사용하려고하면 정의되지 않았 음을 알립니다. 이런 식으로 WP_PLUGIN_URL을 정의하는 방법은 무엇입니까?정의되지 않은 상수 WP_PLUGIN_URL

myscript.js는 myscript.php로 나타납니다.

<?php 
header("Content-type: text/javascript"); 
$bigStarsPath = WP_PLUGIN_URL.'/horoscope-plugin/js/icons/star.png'; 
$smallStarsPath = WP_PLUGIN_URL.'/horoscope-plugin/js/icons/small.png'; 
?> 

/* JS Start Here*/ 
(function($) { 
$.fn.jRating = function(op) { 
var defaults = { 
/** String vars **/ 
bigStarsPath : '<?php echo $bigStarsPath; ?>', // path of the icon stars.png 
smallStarsPath : '<?php echo $smallStarsPath; ?>', // path of the icon small.png 
... 

플러그인 인덱스 페이지 :

function myscript() { 
wp_enqueue_script('jquery'); 
/*REGISTER ALL JS FOR SITE*/ 
wp_register_script('jRating', WP_PLUGIN_URL.'/horoscope-plugin/js/jRating.jquery.php'); 

/*REGISTER ALL CSS FOR SITE*/; 
wp_register_style('stylesheet',WP_PLUGIN_URL.'/horoscope-plugin/css/style.css'); 

/*CALL ALL CSS AND JS SCRIPT*/ 
wp_enqueue_style('stylesheet'); 
wp_enqueue_script('jRating'); 
} 
add_action('wp_enqueue_scripts','myscript'); 
당신이 뭔가를 할 수

답변

1

먼저

function mycustomjs_init() 
{ 
    wp_enqueue_script('mycustomjs', "JS_FILE_PATH"); 

    $bigStarsPath = WP_PLUGIN_URL.'/horoscope-plugin/js/icons/star.png'; 
    $smallStarsPath = WP_PLUGIN_URL.'/horoscope-plugin/js/icons/small.png'; 

    wp_localize_script('mycustomjs', 'bigStarsPath', array('url' => $bigStarsPath)); 
    wp_localize_script('mycustomjs', 'smallStarsPath', array('url' => $smallStarsPath)); 
} 
add_action('wp_print_scripts', 'mycustomjs_init'); 

JS_FILE_PATH 교체, 테마의 functions.php 파일에이 코드를 추가 당신의 js 파일 p ATH ...

당신의 js 파일에 이제

같이 정의 된 변수를 사용하여이 bigStarsPath.url & smallStarsPath.url ...

가 작동 희망 ...

+0

테마의 경우에도 나를 위해 작동하지 않습니다. 함수 또는 플러그인. 플러그인 폴더에서만 작동합니다. 감사합니다 ! – user3327714

+0

이 코드를 플러그인의 주 파일에 추가하는 것보다 플러그인에 넣으려면 ... –

관련 문제