Wordpress widget/plugin - content based on text of post(s)/page(s) visible?

為{幸葍}努か 提交于 2019-12-12 03:43:37

问题


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



来源:https://stackoverflow.com/questions/3424510/wordpress-widget-plugin-content-based-on-text-of-posts-pages-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!