Php 短代码破坏Wordpress站点
我正在本地版本的Wordpress网站上为Wordpress创建新的短代码 在functions.php中,我添加了以下示例:Php 短代码破坏Wordpress站点,php,wordpress,shortcode,Php,Wordpress,Shortcode,我正在本地版本的Wordpress网站上为Wordpress创建新的短代码 在functions.php中,我添加了以下示例: function shortTest() { return 'Test for shortcodes '; } add_shortcode('shortTestHTML', 'shortTest'); 只添加函数是可以的,但是当我添加add_shortcode()部分时,我遇到了一个主要问题 它以某种方式破坏了一些东西,我得到了500个错误,这
function shortTest() {
return 'Test for shortcodes ';
}
add_shortcode('shortTestHTML', 'shortTest');
只添加函数是可以的,但是当我添加add_shortcode()部分时,我遇到了一个主要问题
它以某种方式破坏了一些东西,我得到了500个错误,这意味着我甚至不能在本地加载我的网站了
有什么想法吗
非常感谢
编辑:
从PHP错误日志:
[21-Jun-2011 19:02:37]PHP致命错误:在第4505行的/Users/jonas/Sites/jll/wp includes/functions.PHP中调用未定义的函数add_shortcode(),检查error.log文件(它应该在apache日志文件夹中) 它可能与add_shortcode不作为函数存在有关。1)确保您已经在某个地方包含了
shortcode.php
文件(例如,在wp load.php
或任何其他更合适的地方)
2) 确保您的安装中确实存在此文件(我认为您已经存在了此文件),否则我们将看到不同的错误
3) 您在哪里调用这个函数(我看到它是从
functions.php
调用的,但是在哪个地方)?这里可能存在的问题--functions.php
在shortcode.php
之前加载,如果在加载shortcode.php
之前使用该add\u shortcode
函数,您很可能会看到此错误。在该位置仔细检查您的代码——也许可以将调用移动到另一个位置。在functions.php中添加这个?我不知道,我的做法是在wp content/plugins文件夹中创建一个文件夹,例如shortcodetest
在这个文件夹中创建一个shortcodetest.php
文件
在该文件中,您基本上编写了代码:
<?php
/*
Plugin Name: ShortCodeTest
Plugin URI: http://www.example.net
Description: Print a test message
Version: 0.1
Author: anonymous
Author URI: http://www.example.net
*/
add_shortcode('shortcodetest', 'shortcodetest_function');
function shortcodetest_function() {
return "test of shortcode";
}
然后你以管理员身份登录,你会看到一个插件ShortCodeTest,激活它。然后你可以在你的帖子中使用短代码
请注意,这些评论很重要。。。它们显示在插件描述中。将您的短代码放入文件/wp includes/shortcodes.php
,这样您就可以确保在所有吹口哨都启动并运行时加载它。我有办法执行它们,但有点麻烦:)
您需要通过在
之间放置短代码(例如:[inline….])来稍微更改模板
下面是放在function.php末尾的函数
function parse_execute_and_echo_shortcode($_path) {
$str = file_get_contents($_path);
while (strrpos($str, "<shortcode>")) {
$beginShortcode = strrpos($str, "<shortcode>");
$endShortcode = strrpos($str, "</shortcode>");
$shortcode = substr($str, $beginShortcode + 11, $endShortcode - ($beginShortcode+11));
$shortcode = do_shortcode($shortcode);
$str = substr_replace($str, $shortcode, $beginShortcode, $endShortcode + 12);
}
echo $str;
}
function parse\u execute\u和\u echo\u短代码($\u路径){
$str=文件\u获取\u内容($\u路径);
while(strrpos($str,“”)){
$beginShortcode=strrpos($str,“”);
$endShortcode=strrpos($str,“”);
$shortcode=substr($str,$beginShortcode+11,$endShortcode-($beginShortcode+11));
$shortcode=do_shortcode($shortcode);
$str=substr\u replace($str,$shortcode,$beginShortcode,$endShortcode+12);
}
echo$str;
}
然后您可以调用函数parse\u execute\u和\u echo\u shortcode
,并为其提供包含这些短代码的文件的路径
希望能帮助有人提到函数。php
不在wp include/directory
它位于:wp content/themes//functions.php
中,php向您发出的错误消息是什么?检查您的PHP错误日志。[21-Jun-2011 19:02:37]PHP致命错误:在第4505行的/Users/jonas/Sites/jll/wp includes/functions.PHP中调用未定义函数add_shortcode()[21-Jun-2011 19:02:37]PHP致命错误:调用未定义函数add_shortcode())在第45051行的/Users/jonas/Sites/jll/wp includes/functions.php中)我使用的主题是否可以防止加载shortcode.php并用另一个include替换它?2)它存在。。。3)我在functions.php的最后一行调用它“3)我在functions.php的最后一行调用它”这就是问题所在。您真的应该稍后调用它——当加载shortcodes.php
时(例如,在wp settings.php
文件的末尾——但我不确定这种方法是否100%正确)。好的,谢谢,但是根据Wordpress的文档,您应该在functions.php中添加它。。。嗯,奇怪。。。谢谢你的帮助@Jonasl我不是说“不要添加到functions.php”——我想说的是改变调用add\u shortcode()
函数的方式。可能需要将它添加到functions.php中已经存在的函数中,这些函数将在加载所有模块时调用。我在ATM附近没有WordPress源代码,因此无法确认我的想法。这对解决问题有何帮助?您应该在wp content/themes//functions.php中创建短代码wp include/functions.php不是添加短代码的正确文件。这对我来说是正确的答案。谢谢我修改了错误的functions.php文件。这对我来说也是正确的答案,我确实将代码放在了错误的functions.php文件中!这个答案可以写得更清楚些,但对我来说,是的,这也是问题所在——错误的functions.php!谢谢!
function parse_execute_and_echo_shortcode($_path) {
$str = file_get_contents($_path);
while (strrpos($str, "<shortcode>")) {
$beginShortcode = strrpos($str, "<shortcode>");
$endShortcode = strrpos($str, "</shortcode>");
$shortcode = substr($str, $beginShortcode + 11, $endShortcode - ($beginShortcode+11));
$shortcode = do_shortcode($shortcode);
$str = substr_replace($str, $shortcode, $beginShortcode, $endShortcode + 12);
}
echo $str;
}