WordPress replace content of a page (plugin)
I'm writing a plugin that adds a page with a tag [deposit_page]; that tag should be replaced with some PHP code.
This is what I have, but it doesn't work. Is there something I'm missing or doing wrong?
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");
I just noticed I gave the same variable name to the content that should replace the tag 开发者_JAVA技巧and the function itself. Could this be the problem?
WordPress has support for [square_bracket_shortcodes]
built in.
See: http://codex.wordpress.org/Shortcode_API
Here is your simple example:
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' );
You can paste this into your active theme's functions.php
file.
If you wanted attributes, like [my_shortcode foo=bar]
, you'd do, at the top of this function:
extract(shortcode_atts(array(
'foo' => 'default foo',
'example' => 'default example',
), $atts));
// Now you can access $foo and $example
精彩评论