如何将插件目录路径返回到<script> </script>
而不是硬编码路径?
这是自定义页面。php:
<?php get_header(); ?>
<script type="text/javascript" src="http://local.wordpress.test/wp-content/plugins/path-to-file/script.js"></script>
<?php get_footer(); ?>
如何将插件目录路径返回到<script> </script>
而不是硬编码路径?
这是自定义页面。php:
<?php get_header(); ?>
<script type="text/javascript" src="http://local.wordpress.test/wp-content/plugins/path-to-file/script.js"></script>
<?php get_footer(); ?>
看看这个:https://wordpress.stackexchange.com/a/119084/121955
plugins_url( "path/to/file", __FILE__ );
已编辑:<script src="<?php echo plugins_url( "path/to/file", __FILE__ ); ?>"></script>
要使插件url在javascript中可用,请执行以下操作:
/**
*register the javascript
*/
wp_register_script( \'some_handle\', plugins_url( "plugin-name/path-to-file/script.js") );
/**
*localize the plugin url.
*someObjectName.pluginsUrl then can be used to return
*the plugin url to the javascript
*/
wp_localize_script(\'some_handle\', \'someObjectName\', array(
\'pluginsUrl\' => plugins_url( "plugin-name/path-to-file/script.js"),
));
可以通过以下方式从Javascript返回插件url:<script type="text/javascript">
var url = someObjectName.pluginsUrl;
alert( url );
</script>
下面是我开发的一个插件的示例。
function insert_scripts()
{
wp_enqueue_script(\'jquery\', \'<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>\', \'1.0.0\', true);
wp_enqueue_script( \'test1\', plugin_dir_url( __FILE__ ) . \'js/test1.js\', array(\'jquery\'), \'1.0.0\', true );
wp_enqueue_script( \'test2\', plugin_dir_url( __FILE__ ) . \'js/test2.js\', array(\'jquery\'), \'1.0.0\', true );
}
add_action( \'wp_enqueue_scripts\', \'xwp_insert_scripts\' );
根据上面的代码,脚本应该加载到头部。然而,如果进行了修改,他们可以在其他地方加载。此功能需要添加到函数中。php文件。注意:如果将其添加到核心主题函数中。php文件更新后将对其进行修改。最好是创建一个子主题并使用自定义函数。php文件。<script type="text/javascript" src="<?php echo plugins_url( "plugin-name/path-to-file/script.js"); ?>"></script>
当我试图编辑菜单时,它出现了HTTP错误500然后我设置define(\'WP_DEBUG\', true); 在wp配置中。php尝试保存菜单后,会出现以下错误:注意:在/www/web/keenker/public\\u html/wp-content/themes/keenker/functions中使用了未定义的常量REQUEST\\u-URI-假定为“REQUEST\\u-URI”。php第73行棘手的是,我在函数中的第73行。php是返回修剪($标题);以及整个功能。php文件,我甚至没有RE