WordPress替换页面内容(插件)
我正在写一个插件,添加一个带有标签的页面[deposit_page];应该用一些PHP代码替换该标记 这就是我所拥有的,但它不起作用。我有什么遗漏或做错了吗WordPress替换页面内容(插件),wordpress,plugins,Wordpress,Plugins,我正在写一个插件,添加一个带有标签的页面[deposit_page];应该用一些PHP代码替换该标记 这就是我所拥有的,但它不起作用。我有什么遗漏或做错了吗 function deposit_page_content($content) { $deposit_page_content = "here will go the content that should replace the tags";//end of variable deposit_page_content $c
function deposit_page_content($content) {
$deposit_page_content = "here will go the content that should replace the tags";//end of variable deposit_page_content
$content=str_ireplace('[deposit_page]',$deposit_page_content,$content);
return $content;
}
add_filter("the_content", "deposit_page_content");
我只是注意到,我给应该替换标记和函数本身的内容指定了相同的变量名。这可能是问题所在吗?WordPress支持内置的
[方括号\u短代码]
见:
以下是您的简单示例:
function deposit_page_shortcode( $atts ) {
$deposit_page_content = "here will go the content that should replace the tags";
return $deposit_page_content;
}
add_shortcode( 'deposit_page', 'deposit_page_shortcode' );
您可以将其粘贴到活动主题的functions.php
文件中
如果您需要属性,如[my_shortcode foo=bar]
,您可以在该函数的顶部执行以下操作:
extract(shortcode_atts(array(
'foo' => 'default foo',
'example' => 'default example',
), $atts));
// Now you can access $foo and $example
这段代码运行得很好(我只是将它放在functions.php中进行了测试)。你如何在你的插件中包含这个?你的插件还有其他功能吗?如果没有,我建议简单地将其粘贴到函数文件中。我这样做了,我只是将函数复制/粘贴到默认主题的functions.php中,但仍然与您使用PHP5时相同?str_ireplace是一个PHP5函数。正如我所说,我复制并粘贴到现有的可湿性粉剂安装在这里,它的作品很好。就其本身而言,在句子的中间(“test [存款”页)),几乎任何我能想到的配置。这是非常有效的代码。它在wamp上,所以是PHP5。我将在另一个wordpress安装上试用