The mbct_{$this->model->name}_column_output filter

Support MB Custom Table The mbct_{$this->model->name}_column_output filterResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #41078
    James CooperJames Cooper
    Participant

    I'm using the mbct_{$this->model->name}_column_output filter to modify the output of a column for a custom model, however, the modified output is not displaying in the admin list table. Do you know why that might be?

    Here is my code:

    
    /**
     * Modify the output of the 'title' column for the 'import' custom model to include the 'edit' link.
     *
     * @param mixed  $output      Column output.
     * @param string $column_name Column name.
     * @param array  $item        Item data.
     * @param object $model       Model instance.
     *
     * @return mixed Modified column output.
     */
    add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 10, 4);
    function bcc_modify_title_column_output($output, $column_name, $item, $model) {
            
        if ($column_name === 'title') {
            $base_url = admin_url("admin.php?page=model-import");
            
            $edit_url = add_query_arg([
                'model-action' => 'edit',
                'model-id' => $item['ID'],
            ], $base_url);
    
            // Make the title a link to the 'edit' page.
            $output = sprintf('<a href="%s">%s</a>', esc_url($edit_url), esc_html($output));
        }
        
        return $output;
    }
    
    #41091
    PeterPeter
    Moderator

    Hello,

    I've tried to run the code on my local site and see there are two issues:

    - You should run the callback function with a higher priority like 99

    add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 99, 4);
    

    - You should return the HTML code instead of printing it out, because in the WordPress list table function, it is outputted by using echo state. Please check the file wp-admin/includes/class-wp-list-table.php line 1532. It should be:

    // Make the title a link to the 'edit' page.
    $output = '<a href="' . esc_url($edit_url) . '">' . $output . '</a>';

    Let me know how it goes.

    #41135
    James CooperJames Cooper
    Participant

    Perfect! Works great now!

    Here is the updated code:

    
    /**
     * Modify the output of the 'title' column for the 'import' custom model to include the 'edit' link.
     *
     * @param mixed  $output      Column output.
     * @param string $column_name Column name.
     * @param array  $item        Item data.
     * @param object $model       Model instance.
     *
     * @return mixed Modified column output.
     */
    add_filter('mbct_import_column_output', 'bcc_modify_title_column_output', 99, 4);
    function bcc_modify_title_column_output($output, $column_name, $item, $model)
    {
        if ($column_name === 'title') {
            $base_url = admin_url('admin.php?page=model-import');
    
            $edit_url = add_query_arg(
                [
                    'model-action' => 'edit',
                    'model-id' => $item['ID'],
                ],
                $base_url
            );
    
            // Make the title a link to the 'edit' page.
            $output = '<a href="' . esc_url($edit_url) . '">' . $output . '</a>';
        }
    
        return $output;
    }
    

    Thank you Peter!

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