Twitter bootstrap 如何在Wordpress插件开发中包含引导?
我正在开发插件,我想在我的插件中包括引导。最好的方式是什么?我找不到任何与此相关的东西,到目前为止,我已经找到了几个引导插件,我不需要,我需要在我的插件中包含一些来自引导的js和css元素。我是新手 我也尝试过类似的东西,比如包含在WP主题中,但它不起作用 如果你需要更多的信息,请问我,我会提供给你 我尝试在主文件index.php中包含类似于WP主题的内容,如下所示:Twitter bootstrap 如何在Wordpress插件开发中包含引导?,twitter-bootstrap,include,wordpress,twitter-bootstrap-3,Twitter Bootstrap,Include,Wordpress,Twitter Bootstrap 3,我正在开发插件,我想在我的插件中包括引导。最好的方式是什么?我找不到任何与此相关的东西,到目前为止,我已经找到了几个引导插件,我不需要,我需要在我的插件中包含一些来自引导的js和css元素。我是新手 我也尝试过类似的东西,比如包含在WP主题中,但它不起作用 如果你需要更多的信息,请问我,我会提供给你 我尝试在主文件index.php中包含类似于WP主题的内容,如下所示: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.1
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
每次加载页面时,您都可以使用“wp\u head”操作将代码添加到站点的标题中。在你的插件中加入如下内容:
add_action('wp_head','head_code');
function head_code()
{
$output = '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>';
$output .= '<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>';
$output .= '<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>';
echo $output;
}
add_action('wp_head','head_code');
函数头_代码()
{
$output='';
$output.='';
$output.='';
echo$输出;
}
您需要为js使用wp\u register\u script()
,然后使用wp\u enqueue\u script()
您需要为css使用
wp\u register\u style()
,然后使用wp\u enqueue\u style()
请参见下面的js示例
wp_register_script('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js');
wp_enqueue_script('prefix_bootstrap');
现在对css做同样的处理
wp_register_style('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
wp_enqueue_style('prefix_bootstrap');
最好将它们放在一个可重用的方法中,以防使用ajax。这样,就不需要在ajax方法中重新列出每个脚本或样式表
function prefix_enqueue()
{
// JS
wp_register_script('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js');
wp_enqueue_script('prefix_bootstrap');
// CSS
wp_register_style('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
wp_enqueue_style('prefix_bootstrap');
}
从长远来看,“前缀”的使用将有助于防止冲突,并为您节省一些麻烦。在插件主PHP文件中使用下面的代码。“wp_打印样式”动作挂钩包括CSS样式。“wp_print_scripts”操作挂钩包括java脚本。使用这两个钩子在插件中包含引导Javascript和样式表
add_action( 'wp_print_styles', 'add_my_plugin_stylesheet' );
function add_my_plugin_stylesheet() {
wp_register_style('mypluginstylesheet', '/wp-content/plugins/postgrid/style.css');
wp_register_style('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
wp_enqueue_style('mypluginstylesheet');
wp_enqueue_style('prefix_bootstrap');
}
}大家好,欢迎来到stackoverflow。如果你能多分享一点你尝试过的东西以及失败的原因,这会有所帮助。根据你的插件,可能值得通过一个可能的@JasonNathan副本引用boostrap的副本。我已经更新了这个问题,我无法在coment中复制代码。当我激活插件时,我需要在插件内部使用引导元素,使它已经存在。我不想依赖于其他插件,或者如果客户端在这个站点上有引导。我想询问者是想让引导包含在插件的管理区域中,这就是为什么当他/她试图将其包含在index.php文件中时,它不起作用。因此,需要使用admin_enqueue_scripts操作在管理区域中包含脚本和样式。jquery也是标准WP安装的一部分。我想这一个bootstrap.min.css也是需要的。“这样你就可以随时调用函数。”@MartinPfeffer我不确定“随时需要”调用函数会存在什么设计缺陷。也许这可以解释为需要在同一页面加载中多次调用该函数,但我只是指在所有情况下可能都不需要引导,如果你不“需要”它,就不要调用该函数。也许正是“公正”让我产生了怀疑的感觉。顺便说一句:prefix_enqueue()与其说是一个函数,不如说是一个方法:)…不过,你的答案是对的,值得投票。@MartinPfeffer如果你在method vs function参数中是正确的,我就在那里。我会纠正这个说法。
add_action('wp_print_scripts','add_my_plugin_js');
function add_my_plugin_js(){
wp_register_script('prefix_bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js');
wp_enqueue_script('prefix_bootstrap');