我对此很陌生(了解 WP Guts),我想更好地了解 Hooks 和 Filters,但我无法从 Codex 中得到它。
我做了一个简单的测试,
这个想法是重写 get_title() 方法,以便从标题中删除“受保护:”语句,如果页面受到保护,有一个 protected_title_format 过滤器,我想使用它......
post-template.php中的那一行指定:
$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
对于我可以从 CODEX 获得的内容,我需要删除该过滤器并添加我自己的过滤器,例如
remove_action('protected_title_format');
apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));
使用,当然像
// Removing action
function remove_title_action() {
remove_action('protected_title_format','get_the_title',3);
}
add_action('init','remove_title_action');
// Adding custom function
add_action('protected_title_format','fancy_title', 3, 4);
function fancy_title($id = 0) {
$post = &get_post($id);
$title = $post->post_title;
echo "I'm the king of the world!... >" . $title . "< & >" . $post . "<";
if ( !is_admin() ) {
if ( !empty($post->post_password) ) {
$protected_title_format = apply_filters('protected_title_format', __('MY OWN PAGE Protected: %s'));
$title = sprintf($protected_title_format, $title);
}
}
return apply_filters( 'the_title', $title, $post->ID );
}
我可以得到输出的回声,但我没有得到 $id(为此,没有 $title 或 $post),这个方法是 get_title() 的副本,除了受保护的部分字符串之外,它会删除所有内容。
任何人都可以向我解释这是如何工作的吗?谢谢
PS我想学习,这是这个问题的想法,不是有人告诉我“嘿,去post-template.php并改变它”,因为那样我会问“我更新WP时怎么样...... ?” !