admin管理员组

文章数量:1122826

The best way to show you what I want is a picture:

I would like to add a button on the red area in order to create a special action concerning the Chambre1.

I was looking foor something similar to the "manage_posts_columns" filter that we use to create new columns in the Custom Post Types Admin Screen but I found nothing.

Anyone has a solution for this ?

Much thanks

The best way to show you what I want is a picture:

I would like to add a button on the red area in order to create a special action concerning the Chambre1.

I was looking foor something similar to the "manage_posts_columns" filter that we use to create new columns in the Custom Post Types Admin Screen but I found nothing.

Anyone has a solution for this ?

Much thanks

Share Improve this question asked Aug 30, 2017 at 17:27 juRioqs75juRioqs75 54 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

There are similar filters and actions for custom post type (replace $post_type with the name of your post type):

To add new columns filter is: manage_{$post_type}_posts_columns. This filter has one argument, list of all columns, and you need to add your column, and return results.

To display content of the column, action is manage_{$post_type}_posts_custom_column. This action passes the name of the column and post ID. You need to echo the data you want to display, but make sure you check if the column is indeed yours by checking the column name.

add_action('manage_{$post_type}_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column, $post_id) {
  if ($column == {$my_column_name}) {
    echo '<button>My Button</button>'
  }
}

Replace {$post_type} with the actual name of your post type, and replace {$my_column_name} with the name of the column you have added through the manage_{$post_type}_posts_columns filter.

This is simplified code, you need to create the button you need to use, and handle what the button does.

Use the manage_${post_type}_posts_columns filter to add a custom column header And also manage_${post_type}_posts_custom_column action to populate the custom column with content - Please change your post type slug - chambres and replace below function.

// 1. Add Custom Column Header
function custom_chambres_columns($columns) {
    $columns['custom_action'] = 'Action';
    return $columns;
}
add_filter('manage_chambres_posts_columns', 'custom_chambres_columns');

// 2. Populate Custom Column
function custom_chambres_column_data($column, $post_id) {
    if ($column == 'custom_action') {
        // Output button for custom action
        echo '<button class="custom-action-button" data-post-id="' . $post_id . '">Perform Action</button>';
    }
}
add_action('manage_chambres_posts_custom_column', 'custom_chambres_column_data', 10, 2);

// 3. Handle AJAX Call
add_action('wp_ajax_custom_action_handler', 'custom_action_handler');
function custom_action_handler() {
    $post_id = $_POST['post_id'];

    // Perform your custom action here

    // Return response (optional)
    wp_send_json_success('Action performed successfully');
}

// 4. Enqueue JavaScript
function enqueue_custom_admin_scripts() {
    global $pagenow, $post_type;

    if ($pagenow == 'edit.php' && $post_type == 'custom_post_type') {
        wp_enqueue_script('custom-admin-scripts', get_template_directory_uri() . '/js/admin-scripts.js', array('jquery'), '1.0', true);

        // Pass Ajax URL to JavaScript
        wp_localize_script('custom-admin-scripts', 'custom_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
}
add_action('admin_enqueue_scripts', 'enqueue_custom_admin_scripts');

本文标签: How to add a custom button to each field of a Custom Post Types Admin Screen