1
votes

I have a function that automatically adds a span around my headings so that I can style the span, eg:

<h2><span>my heading</span></h2>

This works fine in my normal Wordpress content, but content within the Advanced Custom Fields strips it out. Has anyone any ideas - have spent hours googling.

//add span into each title so can add flourish under span
add_filter('the_content', 'replace_content',1);
function replace_content($content) {
   $content = preg_replace( '/<h(\d{1,6})(.*?)>(.*?)<\/h(\d{1,6}).*?>/', '<h$1><span>$3</span></h$4>',  $content );
   return $content;
}

Many thanks to anyone who can help!!

1

1 Answers

1
votes

I haven't tried it, but ACF has the acf/load_value filter which may be what you're looking for. This hook allows you to modify the value of a field right after it is loaded from the database.

Code example from the documentation:

function my_acf_load_value( $value, $post_id, $field )
{
    // run the_content filter on all textarea values
    $value = apply_filters('the_content',$value); 

    return $value;
}

// acf/load_value - filter for every value load
add_filter('acf/load_value', 'my_acf_load_value', 10, 3);

// acf/load_value/type={$field_type} - filter for a value load based on it's field type
add_filter('acf/load_value/type=select', 'my_acf_load_value', 10, 3);

// acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
add_filter('acf/load_value/name=my_select', 'my_acf_load_value', 10, 3);

// acf/load_value/key={$field_key} - filter for a specific field based on it's name
add_filter('acf/load_value/key=field_508a263b40457', 'my_acf_load_value', 10, 3);