开发者

Wordpress function returns white screen in plugin

I am brand new at writing Wordpress plugins, so to start I am trying to create a simple one that just modifies a string. I wrote the script a while ago, and know that it functions. But to use it for Wordpress I want to apply it to the post titles. When I replaced the string with the function "get_the_title()" it returns a white screen. I stripped it down to:

function display_title() {
echo get_the_title();
}

add_action(开发者_运维技巧'the_title', 'display_title');

This still returns a white screen. So I figure it must be the "get_the_title()" function. Can anybody explain to me why this doesn't work, and maybe a different way to retrieve the title string?


As John says the_title is a filter rather than an action hook, although your function will be called regardless of whether you register it using add_filter or add_action.

Your problem is that with filters your function is expected to return a value (normally a modified version of the argument passed). So, to modify the title using this filter you should do something like this:

function display_title($title) {
    $title .= '!'; // Do something with the title string here
    return $title;
}

add_filter('the_title', 'display_title');


Well, for one thing, 'the_title' isn't an action, it's a filter. So that function is never firing. So it's not the fault of that function, it is probably something else. I would suggest reading up on the plugin api and learning the difference between actions and filters. Filters are specifically designed to do what you want in a simple way:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜