WordPress替换页面内容(插件)

Chr*_*phe 1 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
    $content=str_ireplace('[deposit_page]',$deposit_page_content,$content);
return $content;
}
add_filter("the_content", "deposit_page_content");
Run Code Online (Sandbox Code Playgroud)

我只是注意到我给应该替换标签和函数本身的内容提供了相同的变量名.这可能是问题吗?

Mar*_*ith 7

WordPress支持[square_bracket_shortcodes]内置.

请参阅:http://codex.wordpress.org/Shortcode_API

这是你的简单例子:

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' );
Run Code Online (Sandbox Code Playgroud)

您可以将其粘贴到活动主题的 functions.php文件中.

如果您想要属性,例如[my_shortcode foo=bar],您可以在此函数的顶部:

extract(shortcode_atts(array(
    'foo' => 'default foo',
    'example' => 'default example',
), $atts));

// Now you can access $foo and $example
Run Code Online (Sandbox Code Playgroud)