2

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

4

6 回答 6

6

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

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

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

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

于 2011-06-21T20:45:27.713 回答
4

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

于 2014-08-27T15:53:10.107 回答
2

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

请注意,评论很重要......它们出现在插件描述中。

于 2011-06-21T22:03:01.310 回答
1

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

<shortcode></shortcode>您需要通过将您的简码(例如:[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_execute_and_echo_shortcode并为其提供包含短代码的文件的路径。

希望可以帮助某人

于 2013-05-30T11:15:50.827 回答
0

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

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

于 2011-06-21T19:49:39.650 回答
0

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

于 2012-04-27T10:54:42.803 回答