Home Forums General Issues Accessing global $variable previously updated by the_content filter


Accessing global $variable previously updated by the_content filter

  • Note: apologies if this is a duplicate. When trying to edit my previous entry it seems to have been wiped out.

    I’m new to ACF Fields and WordPress in general, and am having some difficulty wrapping my head around how to properly access a global variable that gets updated through through some of our custom functionality attached to the_content filter. Pairing things down for the purpose of testing, in the following example I would expect (incorrectly) that the global $counter variable in the format_value filter would reference the updated value as set by the_content. I’ve tried adjusting the priorities to no avail, amongst a number of approaches such as handling the counter through a singleton pattern.


    global $counter;
    $counter = 1;
    add_filter('the_content', function( $content ) {
        global $counter;
        var_dump( $counter ); // returns 2 as expected.
        return $content;
    add_filter('acf/format_value/type=wysiwyg', function( $value, $post_id, $field ){
        global $counter;
        var_dump( $counter ); // returns 1
        return $value;
    }, 20, 3);

    Essentially what we’re attempting to accomplish is insert ads after a specific number of paragraphs. All is working fine using a number of class methods in our template to handle insertions on paragraphs in the_content. We have a counter in place to track the post-paragraph ad insertions, but I can’t access the counter to then follow with ad insertions on ACF content.

    Many thanks in advance for any assistance.

  • ACF does not call the hook the_content when formatting a wysiwig field. Instead it uses acf_the_content. Basically your first filter is never run for the wysiwyg acf field. You’d need to add your code for both hooks.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Accessing global $variable previously updated by the_content filter’ is closed to new replies.