AdvancedCustomFields / AdvancedCustomFields/acf

ACF acf_the_content Filter Causes Fatal Error When convert_smilies is Disabled in Theme

Open
#940 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

The convert_smilies filter in ACF’s acf_the_content hook causes a fatal error when a count() function is called on a non-countable variable. This happens because convert_smilies attempts to count an array or object that sometimes returns as false. This issue occurs even if convert_smilies is disabled at the theme level.

To Reproduce

Steps to reproduce the behavior:

Add ACF fields with content processed by the acf_the_content filter.
Disable the convert_smilies filter in the theme by adding remove_filter( 'the_content', 'convert_smilies' ); to functions.php.
View any post or page where acf_the_content is called, and observe the PHP fatal error related to count() in convert_smilies.
Expected behavior
ACF should respect the filter removal applied by the theme, or the add_filters function should verify if filters like convert_smilies are already disabled before adding them to acf_the_content. This would prevent fatal errors when filters are already removed or unnecessary.

Error caused:

PHP Fatal error:  Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in /var/www/html/wp-includes/formatting.php:3495
Stack trace:
#0 /var/www/html/wp-includes/class-wp-hook.php(324): convert_smilies()
#1 /var/www/html/wp-includes/plugin.php(205): WP_Hook->apply_filters()
#2 /var/www/html/wp-includes/post-template.php(256): apply_filters()
#3 /var/www/html/wp-content/themes/healingWaves/single.php(116): the_content()
#4 /var/www/html/wp-includes/template-loader.php(106): include('...')
#5 /var/www/html/wp-blog-header.php(19): require_once('...')
#6 /var/www/html/index.php(17): require('...')
#7 {main}
  thrown in /var/www/html/wp-includes/formatting.php on line 3495
Screenshots or Video

Not applicable, as the issue results in a fatal error.

Code
function add_filters() {
    add_filter( 'acf_the_content', 'capital_P_dangit', 11 );
    add_filter( 'acf_the_content', 'wptexturize' );
    add_filter( 'acf_the_content', 'convert_smilies', 20 );
    add_filter( 'acf_the_content', 'wpautop' );
    add_filter( 'acf_the_content', 'shortcode_unautop' );
    add_filter( 'acf_the_content', 'do_shortcode', 11 );

    if ( isset( $GLOBALS['wp_embed'] ) ) {
        add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
        add_filter( 'acf_the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
    }
}
Proposed solution
function add_filters() {
    // Determine the correct image tag filter for compatibility
    $wp_filter_content_tags = function_exists('wp_filter_content_tags') ? 'wp_filter_content_tags' : 'wp_make_content_images_responsive';

    // Mimic filters added to "the_content" in "wp-includes/default-filters.php"
    add_filter('acf_the_content', 'capital_P_dangit', 11);

    // Apply wptexturize if it’s available and not disabled in theme settings
    if (function_exists('wptexturize') && !has_filter('acf_the_content', 'wptexturize')) {
        add_filter('acf_the_content', 'wptexturize');
    }

    // Conditionally add convert_smilies only if it’s not disabled in theme settings
    if (function_exists('convert_smilies') && !has_filter('acf_the_content', 'convert_smilies')) {
        add_filter('acf_the_content', 'convert_smilies', 20);
    }

    // Add wpautop, shortcode_unautop, and other filters with similar checks
    if (function_exists('wpautop') && !has_filter('acf_the_content', 'wpautop')) {
        add_filter('acf_the_content', 'wpautop');
    }
    
    if (function_exists('shortcode_unautop') && !has_filter('acf_the_content', 'shortcode_unautop')) {
        add_filter('acf_the_content', 'shortcode_unautop');
    }

    // Apply the image tag filter if defined
    if (!has_filter('acf_the_content', $wp_filter_content_tags)) {
        add_filter('acf_the_content', $wp_filter_content_tags);
    }

    // Add do_shortcode for executing shortcodes in ACF content
    if (function_exists('do_shortcode') && !has_filter('acf_the_content', 'do_shortcode')) {
        add_filter('acf_the_content', 'do_shortcode', 11);
    }

    // Embed support checks
    if (isset($GLOBALS['wp_embed'])) {
        add_filter('acf_the_content', array($GLOBALS['wp_embed'], 'run_shortcode'), 8);
        add_filter('acf_the_content', array($GLOBALS['wp_embed'], 'autoembed'), 8);
    }
}

Version Information:
  • WordPress Version: 6.6.2
  • PHP Version: 8.1.23
  • ACF Version: ACF PRO 6.3.9
  • Browser: N/A (server-side error)
Additional context

The convert_smilies filter can cause fatal errors if it’s enabled by ACF but explicitly disabled by the theme, as it attempts to count() a non-countable value. A suggested fix is to add conditional checks in add_filters to respect removed filters and check their availability before adding them to acf_the_content.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the add_filters() implementation and trace how the acf_the_content filters are registered, especially convert_smilies, alongside the reported WordPress 6.6.2 and PHP 8.1.23 setup. Reproduce the fatal error after removing convert_smilies from the_content, then verify that ACF no longer causes the error and that the intended filters still process ACF content.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend, content
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.