2017-08-05 17 views
0

,我獲得/獲取基於自定義後類型ID在(WordPress的插件開發)PHP值傳遞給JavaScript文件在我的主plugin.php文件

$ wpsv_save_metadata = get_post_meta wp_postmeta數據庫文件($後> ID);

然後基於該ID,我可以使用得到的值如wpsv_video_width,wpsv_video_height等:

$ wpsv_save_metadata [ 'wpsv_video_width'] [0];

但我無法將該值(wpsv_video_width,wpsv_video_height)傳遞給我的JavaScript文件。

如何將值傳遞給我的javascript文件,這樣我就可以改變寬度(win.style.width)和高度(win.style.height)動態:

function yScrollHandler(){ 
 
    var win = document.getElementById("styleku-video-container"); 
 
    if((window.pageYOffset + window.innerHeight) >= 1000){ 
 
\t \t //win.style.webkitTransition = "right 0.7s ease-in-out 0s"; 
 
\t \t win.style.transition = "right 0.7s ease-in-out 0s"; 
 
\t \t win.style.right = "0px"; 
 
\t \t win.style.position = "fixed"; 
 
\t \t win.style.bottom = "0px"; 
 
\t \t win.style.padding = "10px"; 
 
\t \t win.style.width = "400px"; 
 
\t \t win.style.height = "295px"; 
 
    } else { 
 
\t \t win.removeAttribute("style"); 
 
    } 
 
} 
 
window.onscroll = yScrollHandler;

請幫忙。謝謝。

回答

0

我建議你使用wp_localize_script

has a neat example類似於你的問題。如果從一個單獨的網站加載你的js

+0

謝謝,它的工作原理。我可以從我的js文件中獲得價值。但是我不能在'function yScrollHandler(){win.style.width = 400; win.style.height = 225;}'我想用變量替換400和225。請幫忙。 – jojoi

+0

我剛剛意識到,這有點棘手,因爲我使用'window.onscroll = yScrollHandler;'來運行該函數。如果我使用'window.onload = yScrollHandler;'它可以得到值。任何想法如何獲得值,當使用'window.onscroll'運行函數? – jojoi

0

剛剛創建JavaScript變量第一,附上您的js文件後,使用js的你的JS文件中的全局變量。如下所示。

<?php 
    $width = $wpsv_save_metadata['wpsv_video_width'][0]; 
    $height = $wpsv_save_metadata['wpsv_video_height'][0]; 
?> 
<script> 
    var width = <?php echo $width; ?> // use width variable in your js file 
    var height = <?php echo $height; ?> // use height variable in your js file 
</script> 
<script src="your js file path"></script> 
+0

只是想你的建議,但仍然沒有運氣。注意:JavaScript不在標題上。它是在沿着JavaScript文件 – jojoi

+0

ok了立場,但你需要全局變量賦值後添加JavaScript,也不要在指定全局變量使用的document.ready。 –

+0

如果那麼它不工作只是添加的document.ready您的JS文件中一次,嘗試 –

0

替代方法。

如果你不加載你的JS到你的主題,那麼你就不能使用wp_localize_script,所以你需要一個備用的方式包括從WordPress的根文件夾中的wp-load.php文件。在你的插件文件夾中創建兩個文件:

JS-template.js

/wp-content/plugins/your-plugin/js/js-template.js

var mymessage = '[[my-message]]'; // in php use str_replace() to replace this 
console.log('mymessage', mymessage); 

GET -js.php

/wp-content/plugins/your-plugin/js/get-js.php

<?php 

// Drop this into your plugin folder and access it directly 
// https://example.com/wp-content/plugins/your-plugin/js/get-js.php 

// It will load a js file named js-template.js 

/** 
* Finds the wp root folder. 
* Navigate through the parent directories to find the wp-load.php file. 
* Stop after reaching 10 directories up, so we don't run into a 
* recursion error. 
* 
* @param $path directory to look into 
* @param $max_recursive the max number of parent directories to navigate into 
* 
* @return string|boolean Either a string of the path or false if the max recursion is reached 
*/ 
function find_parent_wp_root($path = './', $max_recursive = 10) { 

    if($max_recursive === 0) 
     return false; 

    if(file_exists(realpath($path) . '/wp-load.php')) { 
     return realpath($path) . '/'; 
    } else { 
     return find_parent_wp_root($path . '../', $max_recursive-1); 
    } 
} 

$www_root = find_parent_wp_root($path = './'); 

if(! $www_root) { 
    http_response_code(404); 
} else { 

    define('WP_USE_THEMES', false); 
    require($www_root . 'wp-blog-header.php'); 

    $js = file_get_contents('js-template.js'); 

    $js = str_replace('[[my-message]]', 'some new value', $js); 

    header('Content-Type: application/javascript'); 
    echo $js; 
} 
相關問題