Wordpress widget/plugin - content based on text of

2019-09-09 21:22发布

问题:

I'm new to the Wordpress plugin and widget APIs, but I'm sure this is possible.

I'd like to make a plugin or widget that would create links to other posts or external sites based on certain keywords/tags in the content of the given page/post.

Is this possible?

For example, if a term is in all-caps, link to the Wiktionary definition; inside a <news>..</news> pair, go to Google's news search; etc.

回答1:

This is definitely possible. Don't bother looking into the widget api for this. Look at the filter api. WordPress has an api that allows you to filter content before it's sent to the browser. In this case, you'd do something like this:

function my_super_awesome_content_filterer( $content ){
  $content = preg_replace( '#([A-Z]+)#', '<a href="wictionary.definition.address=$1">$1</a>', $content );
}

add_filter( 'the_content', 'my_super_awesome_content_filterer' );

Read more about filters here:

http://codex.wordpress.org/Plugin_API#Filters