Question

I'm using a str_replace to change the_content, using WordPress' default 'the_content'.

However, this does not seem to affect anything that has been submitted through the Advanced Custom Fields plugin.

My original function, that works with default WordPress editor:

function same_youtube_options($content) {
  return str_replace("rel=0&", "rel0&theme=light&autohide=1&showinfo=0&controls=1&", $content);
}
add_filter('the_content', 'same_youtube_options');

And this, adjusted for the Advanced Custom Fields:

function same_youtube_options_controls_acf($field) {
  $field = get_sub_field('iframe_url'); 
    return str_replace("rel=0&", "rel0&&autohide=1&showinfo=0&controls=1&", $field);
}

add_filter('acf/load_field/name=iframe_url', 'same_youtube_options_controls_acf');

However, the latter does not work. It simply removes the field from the page. Would anyone know how to use these filters correctly?

Was it helpful?

Solution

Try this:--

function same_youtube_options_controls_acf($value, $post_id, $field )
{
    // run the_content filter on all textarea values
    $value = apply_filters('the_content',$value);
    return $value;
}
add_filter('acf/load_value/name=iframe_url', 'same_youtube_options_controls_acf', 10, 3);

This hook will replace you custom field value with the_content filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top