什么可能使 WordPress add_shortcode 停止工作?
我已经在 WordPress 上安装了 WordPress 多次,通过 SVN 并替换文件夹......数据库始终保持不变。突然,来自 SVN 的新副本无法在两台不同的机器上使用来自 wp 调查和测验工具 的以下代码工作:
function wpsqt_main_site_quiz_page($atts) {
extract( shortcode_atts( array(
'name' => false
), $atts) );
if ( !$name ){
require_once WPSQT_DIR.'/pages/general/error.php';
}
require_once WPSQT_DIR.'/includes/site/quiz.php';
ob_start();
wpsqt_site_quiz_show($name);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode( 'wpsqt_page' , 'wpsqt_main_site_quiz_page' );// Deprecated and will be removed
add_shortcode( 'wpsqt_quiz' , 'wpsqt_main_site_quiz_page' );
如果我使用 echo
来查看代码在哪里到达时,add_shotcode 已到达,而函数内部未到达,并且页面仅显示以下内容:
[wpsqt_quiz name="test"]
而不是将其替换为预期的 quiz.php
。
现在我刚刚删除了数据库,重新安装了 WordPress 和插件,当然一切都工作正常。如果我得到的是 SVN 版本,它没有全部修改(它只有 1 个插件 - Magic Fields - 和一个自定义主题),删除该插件并再次安装它,它仍然不起作用!
这里可能出了什么问题?使 add_shortcode 工作需要什么?
I've installed WordPress on top of wordpress many times, through SVN and replacing folders... The database remained always the same. Suddenly a fresh copy from SVN can't work in two different machines with the following code, from wp survey and quiz tool:
function wpsqt_main_site_quiz_page($atts) {
extract( shortcode_atts( array(
'name' => false
), $atts) );
if ( !$name ){
require_once WPSQT_DIR.'/pages/general/error.php';
}
require_once WPSQT_DIR.'/includes/site/quiz.php';
ob_start();
wpsqt_site_quiz_show($name);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode( 'wpsqt_page' , 'wpsqt_main_site_quiz_page' );// Deprecated and will be removed
add_shortcode( 'wpsqt_quiz' , 'wpsqt_main_site_quiz_page' );
If I use echo
to see where the code is being reached, add_shotcode is being reached while inside the function is not and the page just displays this:
[wpsqt_quiz name="test"]
Instead of replacing it with the expected quiz.php
.
Now I just deleted the database got a fresh install of wordpress and the plugin, and of course everything worked fine. If I get the SVN version, which isn't all that modified (it just got 1 plugin - Magic Fields - and a customized theme), remove the plugin and install it again, it still doesn't work!
What could be going wrong here? What is everything needed to make add_shortcode work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题从昨天起就一直困扰着我。终于找到原因了,(现在)显然是在定制模板上。
标头包含对
query_posts
的调用,据说每个页面加载只能调用一次。然后是wp_reset_query
来救援。但是等等!似乎这两个函数都已被弃用,都不应该使用!相反,我们应该始终使用 WP_query 对象。所以,这是可行的,但它是错误的:
并且这是正确且正确的方法:
如果没有这个,页面本身上的后续query_posts将无法正确加载,因此
[wpsqt_quiz name="test"]
其中(在页面帖子中)永远不会被调用。另外,似乎
[wpsqt_quiz name="test"]
无法添加到模板页面。就这样。
That problem had been bugging me since yesterday. Finally found out the reason, (now) obviously on the customized template.
The header included a call to
query_posts
, which supposedly can only be called once per page load. Then there comeswp_reset_query
to rescue. But wait! Seems like both of those functions are deprecated and neither should be used! Instead, we should always use WP_query object.So, this works but it's wrong:
and this is the right and proper way:
Without that, the subsequent query_posts on the page itself are not properly loaded and thus the
[wpsqt_quiz name="test"]
inside them (in the page post) is never called.Also, it seems like
[wpsqt_quiz name="test"]
can't be added to the template page.That's all.