【问题标题】:Shortcodes breaking Wordpress Site破坏 Wordpress 网站的简码
【发布时间】:2011-06-21 19:24:22
【问题描述】:

我正在本地版本的 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()

【问题讨论】:

  • PHP 向您抛出的错误信息是什么?检查您的 PHP 错误日志。
  • [21-Jun-2011 19:02:37] PHP 致命错误:在线调用 /Users/jonas/Sites/jll/wp-includes/functions.php 中的未定义函数 add_shortcode() 4505

标签: php wordpress shortcode


【解决方案1】:

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

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

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

3) 你从哪里调用这个函数(我看到它是从functions.php 调用的,但是在哪个地方)?这里可能存在问题——functions.phpshortcodes.php 之前加载,如果你在shortcodes.php 加载之前使用了add_shortcode 函数,你很可能会看到这个错误。在那个地方仔细检查你的代码——也许把对add_shortcode的调用移到另一个地方。

【讨论】:

  • 1) 我使用的主题能否阻止加载 shortcodes.php 并将其替换为另一个包含?
  • 2)它存在......和3)我在functions.php的最后一行调用它
  • "3) 我在functions.php的最后一行调用它 这就是问题所在。你真的应该稍后再调用它——当shortcodes.php被加载时(例如,在wp-settings.php文件的末尾——但我不能100%确定这种方法有多正确)。
  • 好的,谢谢,但是根据 Wordpress 的文档,你应该在 functions.php 中添加这个......嗯,很奇怪......谢谢你的帮助!
  • @jonasll 我不是说“不要添加到functions.php”——我想说的是改变你调用/调用add_shortcode()函数的方式。也许它需要添加到functions.php中一些已经存在的函数中,这些函数将在所有模块加载时被调用。我在 ATM 附近没有 WordPress 资源,所以无法证实我的想法。
【解决方案2】:

提到functions.php不在wp-include/ directory中。
它在:wp-content/themes/<your-theme-name>/functions.php

【讨论】:

  • 这对解决问题有何帮助?
  • 您应该在 wp-content/themes//functions.php 中创建简码 wp-include/functions.php 不是添加简码的正确文件。
  • 这对我来说是正确的答案。谢谢!我修改了错误的functions.php文件。
  • 这对我来说也是正确的答案,我确实把我的代码放在了错误的 functions.php 文件中!
  • 这个答案可以写得更清楚,但是对我来说也是问题 - 错误的functions.php!所以谢谢!
【解决方案3】:

你说在functions.php中添加这个?好吧,我不知道,我这样做的方式是在 wp-content/plugins 文件夹中创建一个文件夹,例如简码测试。

在此文件夹中创建一个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,激活它。然后您可以在帖子中使用短代码。

请注意,cmets 很重要……它们会显示在插件说明中。

【讨论】:

    【解决方案4】:

    我有办法执行它们,但有点棘手:)

    您需要通过将您的简码(例如:[inline ....])放在&lt;shortcode&gt;&lt;/shortcode&gt; 之间来更改您的模板,然后

    这是放在你的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_execute_and_echo_shortcode 并为其提供包含短代码的文件的路径。

    希望能帮助到别人

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • [21-Jun-2011 19:02:37] PHP 致命错误:在线调用 /Users/jonas/Sites/jll/wp-includes/functions.php 中的未定义函数 add_shortcode() 4505
      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2019-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-01
        相关资源
        最近更新 更多