• Resolved mahyulan

    (@mahyulan)


    I am using a code snippet, that was provided on WPBeginners by WPCode. But the snippet not working anymore after upgrading to PHP8.2
    So the plugin correctly indicates an error (on the line printed in bold?!), but I hope someone can help why the code is not valid anymore.

    The code is to create a shortcut to display todays date:

    function wpb_date_today($atts, $content = null) {
    extract( shortcode_atts( array(
    ‘format’ => ”
    ), $atts ) );

    if ($atts[‘format’] == ”) {
    $date_time .= date(get_option(‘date_format’));
    } else {
    $date_time .= date($atts[‘format’]);
    }
    return $date_time;
    }
    add_shortcode(‘date-today’,’wpb_date_today’);

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @mahyulan,

    From what I can see the only issue with your code seems to be that your browser changed the quotes from ‘ to

    I recommend manually editing your code in WPCode and making sure your code only uses ‘ or ” where applicable.

    On the WPBeginner site you can also double click on the code and copy-paste that way – it should keep the correct quotes that way.

    Plugin Author Mircea Sandu

    (@gripgrip)

    If you can also share the error message that you are getting in WPCode it would be useful to pinpoint what is happening.

    Thread Starter mahyulan

    (@mahyulan)

    I checked the single quotes in the code and they are the “streight” ones, do that should be OK.
    The error message that I get is:
    “Uncaught TypeError: Cannot access offset of type string on string”

    Plugin Author Mircea Sandu

    (@gripgrip)

    Ok, thank you, we need to update that code – it’s throwing warnings for older PHP versions too.

    Please replace it with the code below and we’ll make sure to update the code on WPBeginner too.

    function wpb_date_today( $atts, $content = null ) {
    	$atts = shortcode_atts( array(
    		'format' => '',
    	), $atts );
    
    	$date_time = '';
    
    	if ( $atts['format'] == '' ) {
    		$date_time .= date( get_option( 'date_format' ) );
    	} else {
    		$date_time .= date( $atts['format'] );
    	}
    
    	return $date_time;
    }
    
    add_shortcode( 'date-today', 'wpb_date_today' );
    Thread Starter mahyulan

    (@mahyulan)

    Hello Mircea,

    Yes, now it is working again ??
    Many thanks for helping me out (and others of course that are using the code from WPBeginner)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Code snippet wrong after upgrading PHP7.4 to PHP8.2’ is closed to new replies.