javascript - Integrate JS application into wordpress plugin -
for running js-application need open in browser index.html wich consist many links styles , others scripts
<script src="js/lib/jquery-2.1.1.min.js"></script> <script src="js/lib/bootstrap.min.js"></script> <script src="js/lib/bootstrap-select.min.js"></script> i want make plugin thats wrapping js-application wordpress.
<?php /* plugin name: plugin description: plugin author: me version: 1.0 */ function showapp(){ $file = readfile("app/index.html", true); echo $file; } add_action('init', 'showapp'); the problem occurs in loading styles , dependent js files.
update
<?php /* plugin name: plugin description: plugin author: me version: 1.0 */ function showapp(){ $path = 'd:\xampp\htdocs\wp\wp-content\plugins\app\js\app'; if (($_server['request_uri']) == "/wp/2015/05/14/mypage/") { if ($handle = opendir($path)) { echo "directory handle: $handle\n"; echo "entries:\n"; while (false !== ($entry = readdir($handle))) { echo "$entry\n"; enqueue_and_register_my_scripts($entry, $path); } closedir($handle); $file = readfile('d:\xampp\htdocs\wp\wp-content\plugins\app\index.html'); echo $file; die(); } } } function enqueue_and_register_my_scripts($script, $path){ wp_register_script($script, $path . $script); wp_enqueue_script ($script, $path . $script); } add_action('init', 'showapp'); this didn't work.
register style firsts, idea load files conditionally based on current page (which app). need make custom page. page-{name}.php
example
// use wp_enqueue_scripts action hook both enqueue , register scripts add_action( 'wp_enqueue_scripts', 'enqueue_and_register_my_scripts' ); function enqueue_and_register_my_scripts(){ // use `get_stylesheet_directroy_uri() if script inside theme or child theme. wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/js/my-script.js' ); // let's enqueue script used on specific page of site if ( is_page( 'careers' ) ){ // enqueue script has both jquery (automatically registered wordpress) // , my-script (registered earlier) dependencies. wp_enqueue_script( 'my-careers-script', get_stylesheet_directory_uri() . '/js/my-careers-script.js', array( 'jquery', 'my-script' ) ); } } there approach call functions return path of files. if located within theme folder. use get_stylesheet_directroy_uri()
otherwise if purely on different path would need make function path. that's not efficient if using wordpress better stick within abundant functionality.
edit: skipped plugin when reading bad can use reference. https://codex.wordpress.org/function_reference/plugins_url
Comments
Post a Comment