1

This snippet used to work fine but with the update to PHP 8.0 it now breaks the site when a WC order is switched to this custom status

//Add custom bulk action in dropdown

add_filter( 'bulk_actions-edit-shop_order', 'register_bulk_action_printed' ); 

function register_bulk_action_printed( $bulk_actions ) {

    $bulk_actions['mark_as_printed'] = 'Change status to Printed';  
    return $bulk_actions;
}

//Bulk action handler

add_action( 'admin_action_mark_as_printed', 'bulk_process_status_printed' );

function bulk_process_status_printed() {
    
    if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
        return;
        
    foreach( $_REQUEST['post'] as $order_id ) {
    
        $order = new WC_Order( $order_id );
        $order_note = 'That\'s what happened by bulk edit:';
        $order->update_status( 'wc-printed', $order_note, true ); 
        
    }
    
    $location = add_query_arg( array(
        'post_type' => 'shop_order',
        'marked_as_printed' => 1, // marked_as_printed=1 is just the $_GET variable for notices
        'changed' => count( $_REQUEST['post'] ), // number of changed orders
        'ids' => join( $_REQUEST['post'], ',' ),
        'post_status' => 'all'
    ), 'edit.php' );

    wp_redirect( admin_url( $location ) );
    exit;

}

/*
 * Notices
 */
add_action('admin_notices', 'custom_order_status_notices_printed');

function custom_order_status_notices_printed() {
 
    global $pagenow, $typenow;
  
    if( $typenow == 'shop_order' 
     && $pagenow == 'edit.php'
     && isset( $_REQUEST['marked_as_printed'] )
     && $_REQUEST['marked_as_printed'] == 1
     && isset( $_REQUEST['changed'] ) ) {
 
        $message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
        echo "<div class=\"updated\"><p>{$message}</p></div>";
    
    }
    
}

This is the error that I get:

PHP Fatal error: Uncaught TypeError: join(): Argument #2 ($array) must be of type ?array, string given in /nas/content/live/allstaruhlmann/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code:32\nStack trace:\n#0 /nas/content/live/allstaruhlmann/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code(32): join(Array, ',')\n#1 /nas/content/live/allstaruhlmann/wp-includes/class-wp-hook.php(307): bulk_process_status_printed('')\n#2 /nas/content/live/allstaruhlmann/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', Array)\n#3 /nas/content/live/allstaruhlmann/wp-includes/plugin.php(476): WP_Hook->do_action(Array)\n#4 /nas/content/live/allstaruhlmann/wp-admin/admin.php(419): do_action('admin_action_ma...')\n#5 /nas/content/live/allstaruhlmann/wp-admin/edit.php(10): require_once('/nas/content/li...')\n#6 {main}\n thrown in /nas/content/live/allstaruhlmann/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code on line 32, referer: https://allstaruhlmann.com/wp-admin/edit.php?post_type=shop_order
4
  • 1
    Have you checked your error logs - what's the full error message?
    – Sally CJ
    Jul 13, 2022 at 10:46
  • I've just managed to find the error - can you shed any light? I'm not a developer by trade so understand some aspects but not exactly where I need to go and do
    – J4G
    Jul 13, 2022 at 11:19
  • 1
    It's good to see you already have the answer, but I thought you should check debugging in WordPress and the common WordPress errors, and you should enable debugging when developing to ensure your code and website are free from not just fatal/critical errors, but PHP warnings and notices as well. 🙂
    – Sally CJ
    Jul 14, 2022 at 0:35
  • 1
    And FYI, for general PHP questions like this (which was about PHP 8 and the join() function in PHP), they are better suited at Stack Overflow. More details on that here
    – Sally CJ
    Jul 14, 2022 at 0:38

2 Answers 2

4

On this line in bulk_process_status_printed():

'ids' => join( $_REQUEST['post'], ',' ),

you've got the arguments backwards: it should be join(separator, array):

'ids' => join( ',', $_REQUEST['post'] ),

This is actually a PHP 8 change not WordPress 6

8.0.0 - Passing the separator after the array is no longer supported.

1
  • You're a LEGEND, thanks so much! I'll update the question to reflect PHP 8 not WP 6
    – J4G
    Jul 13, 2022 at 11:35
0

The critical error in WordPress is generally caused by a malfunctioning plugin, script, or code that prevents WordPress from functioning properly.

WordPress is unable to load the rest of the files it needs unless you resolve this issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.