将WordPress过滤器添加到ACF WYSIWYG字段

时间:2016-06-13 08:49:26

标签: wordpress advanced-custom-fields

我目前正在使用以下过滤器来替换字符串' magic_click_link'的任何实例。在WordPress' the_content

我刚刚更新此字段以使用ACF WYSIWYG字段,但这导致过滤器停止工作。我相信我需要在过滤器内定位除the_content之外的其他内容,但我不确定需要什么。

function click_link ($b) {
    global $post;
    $this_post_id = $post->ID;
    $op_name = get_field('operator_name');
    $namenospace = make_no_space("$op_name");

    $tracking_link = '/go/'.$this_post_id.'/';

    $click_link = '<a class="claimCTA" id="operator-step1-'.$namenospace.'" href="'.$tracking_link.'" target="_blank" rel="nofollow">Click here to go to '.$op_name.'!</a>';

    $b = str_ireplace('magic_click_link',$click_link,$b);
    return $b;
}

add_filter( 'the_content', 'click_link');

1 个答案:

答案 0 :(得分:2)

过滤器Average: 876 Median: 694 90% Line: 1343 Throughput: 97.7 / s 使用函数the_content自动在任何内容输出上运行,因此默认情况下,这意味着它仅适用于标准WordPress帖子和页面内容。

要将这些过滤器应用于自定义字段内容,您还需要在输出字段内容时手动调用the_content()

例如,假设您的自定义字段名为apply_filters()。使用高级自定义字段,您可以调用:

my_extra_content

这将输出您的字段的内容,同时对其应用所有常用的过滤器。由于您的echo apply_filters("the_content", get_field("my_extra_content")); 功能已挂钩到click_link(),因此会根据您的需要处理您的自定义字段内容。

相关问题