Support » Fixing WordPress » PHP Deprecated notice when updating to PHP 8

  • New Notice when updating to PHP 8 for all shortcodes – WordPress 5.8.3

    Required parameter $shortcode_tag follows optional parameter $content in .../Shortcodes/Stats.php

    Looks like PHP 8 wants the parameter with the default value last but the WP Code Reference states the $callback 3 parameters should be in the order they are in the code below. Will removing the default value for $content affect functionality?

    <?php namespace Shareholders\Shortcodes;
    
    class Stats implements ShortcodeConfig {
    
        public function register_hooks() {
            add_shortcode('stat', [$this, 'render_stats_container']);
        }
    
        public function render_stats_container($atts, $content = null, $shortcode_tag) {
            $shortcode = shortcode_atts([
                'container_class' => '',
                'stat_class' => '',
            ], $atts);
    
            ob_start();
            ?>
            <div class="<?php echo $shortcode['container_class']; ?>">
                <div class="<?php echo $shortcode['stat_class']; ?>">
                    <?php echo do_shortcode(apply_filters('the_content', $content)); ?>
                </div>
            </div>
            <?php
            return ob_get_clean();
        }
    }
    • This topic was modified 5 months, 2 weeks ago by fromthe80s.
    • This topic was modified 5 months, 2 weeks ago by fromthe80s.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi there!

    Is that from a plugin or a theme? Have you tried reaching out to the developer to get more information as well?

    Your shortcode callback only needs 2 params $atts and $content, remove 3rd param that you are using.
    and if you need 3rd param then define your function like this

    public function render_stats_container( array $atts, string $content = '', $shortcode_tag)

    • This reply was modified 5 months, 2 weeks ago by Vijay Hardaha.
    MK

    (@mkarimzada)

    This is a reported bug in PHP 8. You can read more here: https://bugs.php.net/bug.php?id=53399 also https://github.com/php/php-src/pull/5067

    PHP 8 warns about using optional parameters before required ones. Technically having optional parameters before required ones make all optional params required as well.

    I couldn’t find information about add_shortcode callback’s required or optional parameters. however, a work around would be passing default values to all optional params. See below:

    public function render_stats_container($atts = null, $content = null, $shortcode_tag) {
        $shortcode = shortcode_atts([
            'container_class' => '',
            'stat_class' => '',
        ], $atts);
    
        ob_start();
        ?>
        <div class="<?php echo $shortcode['container_class']; ?>">
            <div class="<?php echo $shortcode['stat_class']; ?>">
                <?php echo do_shortcode(apply_filters('the_content', $content)); ?>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }

    I hope this helps.

    Thread Starter fromthe80s

    (@fromthe80s)

    @jcastaneda this is not a plugin or paid theme, no one to reach out to. Custom code from an inherited site.

    @mkarimzada Yes very helpful. The last comment in the 2nd link, implies that I can drop the null without implications.

    This should be true for my initial code example as $content is echoed to the page.

    I have several other shortcodes with ($atts, $content = null, $shortcode_tag) where $content is not used in the function. Are there implications to drop the null in these cases?

    • This reply was modified 5 months, 2 weeks ago by fromthe80s.
    • This reply was modified 5 months, 2 weeks ago by fromthe80s.
    MK

    (@mkarimzada)

    @fromthe80s If you look at same issue/pull request, the first commit (Warning for required params after optional ones) is not merged: https://github.com/php/php-src/pull/5067/commits. Only deprecation notice is merged.

    Let’s get back to wordpress shortcode API, Looking at the source code https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/shortcodes.php#L57 it’s not clear which one of these params are required. You might want to create an issue on this.

    To answer your question about $content, it’s used to distinguish between self-closing and enclosing shortcodes. Read more: https://developer.wordpress.org/plugins/shortcodes/enclosing-shortcodes/#processing-enclosed-content.

    I hope this makes sense.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.