繁体   English   中英

打破Wordpress网站的简码

[英]Shortcodes breaking Wordpress Site

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

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的调用add_shortcode另一个位置。

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

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

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

我有一种执行它们的方法,但是有点奇怪:)

您需要通过将您的短代码(例如:[inline ....])放在<shortcode></shortcode>之间,来对模板进行一些更改,然后

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

希望可以帮助某人

/wp-includes/shortcodes.php这种方式将您的简码放入文件/wp-includes/shortcodes.php ,以确保在所有打击和哨声/wp-includes/shortcodes.php并开始运行时将其加载。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM