破坏 WordPress 网站的短代码

发布于 2024-11-16 18:41:38 字数 513 浏览 0 评论 0原文

我正在我的本地版本的 WordPress 网站上为 WordPress 创建新的短代码。

在functions.php中,我添加了例如:

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()

I am creating new shortcodes for Wordpress on my local version of a Wordpress website.

In functions.php, I am adding for example:

function shortTest() {  
    return 'Test for shortcodes ';  
} 

add_shortcode('shortTestHTML', 'shortTest');  

Adding the function only is OK, but when I add the add_shortcode() portion, I get a major issue.

It breaks something somehow and I get 500 errors, meaning I can't even load my website locally anymore.

Any thoughts???

Thanks so much!

EDIT:

From PHP Error Log:
[21-Jun-2011 19:02:37] PHP Fatal error: Call to undefined function add_shortcode() in /Users/jonas/Sites/jll/wp-includes/functions.php on line 4505

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

や三分注定 2024-11-23 18:41:38

1) 确保您已在某处包含 shortcodes.php 文件(例如,在 wp-load.php 或任何其他更合适的位置)。

require_once( ABSPATH . '/wp-includes/shortcodes.php' );

2)确保您的安装中确实存在此文件(我认为您有,否则我们会看到不同的错误)。

3)你从哪里调用这个函数(我看到它是从functions.php调用的,但是在哪里)?这里可能存在的问题 - functions.phpshortcodes.php 之前加载,并且如果您在 shortcodes 之前使用该 add_shortcode 函数.php 加载后您很可能会看到此错误。仔细检查该位置的代码 - 也许将对 add_shortcode 的调用移至另一个位置。

1) Make sure that you have included shortcodes.php file somewhere (for example, in wp-load.php or whatever other more appropriate place may be).

require_once( ABSPATH . '/wp-includes/shortcodes.php' );

2) Make sure that this file does exist on your installation (which I think you have otherwise we would see a different error).

3) Where do you call this function from (I see it is called from functions.php, but which place)? Possible problem here -- functions.php is loaded prior to shortcodes.php, and if you do use that add_shortcode function before shortcodes.php is loaded you most likely will see this error. Double check your code in that place -- maybe move the call to add_shortcode to another place.

九八野马 2024-11-23 18:41:38

提到的functions.php不在wp-include/目录中。
它位于:wp-content/themes//functions.php

mentioned functions.php is not in wp-include/ directory.
it is in: wp-content/themes/<your-theme-name>/functions.php

蓬勃野心 2024-11-23 18:41:38

你说在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,激活它。然后您可以在帖子中使用短代码。

请注意,注释很重要......它们显示在插件描述中。

adding this in functions.php you say? Well I don't know about that, the way I did it was create a folder inside the wp-content/plugins folder, e.g. shortcodetest.

Inside this folder create a shortcodetest.php file.

in that file you basically write your code:

<?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";
}

?>

Then you login as admin, you will see a plugin ShortCodeTest, activate it. Then you can use the shortcode in your posts.

Note that the comments are important... they show up in the plugin description.

莫多说 2024-11-23 18:41:38

我有一种方法来执行它们,但有点复杂:)

您需要通过将短代码(例如:[inline ....])放在 then

这是放置在 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;
}

然后,您可以调用函数 parse_execute_and_echo_shortcode 并为其提供包含短代码的文件的路径。

希望可以帮助某人

I got a way to execute them but it's a little triky :)

You need to change a little bit your template by putting your shortcode (for example: [inline ....]) between <shortcode></shortcode> then

Here is the function to place at the end of your 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;
}

Then you can call function parse_execute_and_echo_shortcode and giving it the path to your file containing the shortcodes.

Hope that can help someone

離殇 2024-11-23 18:41:38

检查您的 error.log 文件(它应该位于您的 apache 日志文件夹中)。

这可能与 add_shortcode 不作为函数存在有关。

Check your error.log file (it should be in your apache log folder).

It probably has to do with add_shortcode not existing as a function.

蓝眼泪 2024-11-23 18:41:38

将您的短代码放入文件 /wp-includes/shortcodes.php 中,这样您就可以确保在所有吹哨声启动并运行时加载它。

Put your shortcode in the file /wp-includes/shortcodes.php this way you make sure it will be loaded when all the blows and whistles are up and running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文