Support » Developing with WordPress » Issue with customizing the WP Admin table of Custom Post Type

  • Hello everyone,

    I have a custom post type which -obviously- has its own admin table. This admin table is basically a table with events (=aanmeldingen in the code example). So every event has its own row. Furthermore, every event has its own folder in the file structure. E.g. when there is a ‘post’ with id 5106, there also is a folder with 5106 in the file structure.

    When someone registers for an event, a registration form in PDF is added to the specific folder.

    Next, I created a button in one of the table columns with a delete button. When a WP user (one of the employees) clicks on this button, all the files in the folder are deleted. This works perfectly EXCEPT for the first row of the admin table. Somehow, when clicking the delete button on the first row, nothing happens. In every other row it works without any issues. When I filter or sort the table, the issue only happens in the first row. Thus when event 5106 is on the first row, I cannot delete the files. When I sort the column and event 5106 appears on the fifth, thirteenth or 50th row, I can delete them without any problems.

    Also, there is a redirection on the same button which also doesn’t work, and I added a small inline css to the button. This inline css (margin-top:10px;) also works in every row except for the first row.

    The code I am using is as follows (inside of switch statement to define the columns):

    case 'delete_aanmeldingen' :
     ?>
    <form method="post">
     <input type="submit" name="delete-<?php echo $post_id; ?>" value="delete"/>
     </form>
     <?php
     $folder_path = / / path //  . $post_id .'/*'); 
     if(isset($_POST["delete-$post_id"])){
     // Deleting all the files in the list
     foreach($folder_path as $folder_file) {
     // Delete the given file
     unlink($folder_file); 
     }
     header("Location: // url // ");
     exit;
     }
     break;

    I really need to make this work, but I have no clues how to debug this problem. Does anyone have an idea what it could be? If my question is unclear, or if you need additional information, please let me know!

    Cheers,

    Hans

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot make a header() call after output has begun. If you want to do redirection after output has started, it needs to be done via JavaScript.

    $folder_path is not an array, so using foreach on it is illogical.

    Neither is probably related to the first row problem. Typical debugging steps: Be sure the HTML output is as expected. Check the form’s POSTed data through your browser’s developer tool network tab. Output each variable’s value to verify each is as expected.

    Thread Starter hansvmourik

    (@hansvmourik)

    Hello bcworkz,

    Thanks a lot for your extensive reply! I found the difference between the ‘delete’ button on the first row and the ‘delete’ buttons on the other rows. Now indeed the issue becomes more clear. It appears that the ‘delete’ button on the first row is not surrounded by the form elements. The delete button on all the other rows are surrounded by the form element. Thus the html output on row 1 is:

    <button type=”submit” name=”delete-1234″>Delete</button>

    whereas the html output on all the other rows is:

    <form method=”post” style=”margin-top:10px;”>
    <button type=”submit” name=”delete-1234″>Delete</button>
    </form>

    Do you (or someone else?) have an idea how that’s possible? Hope to hear from you soon!

    Cheers,

    Hans

    Moderator bcworkz

    (@bcworkz)

    Sorry for the slow reply, notifications on this topic seem to have gone wonky for me.

    That output of either type doesn’t match your OP code at all (button vs. input tags for one). It seems some other code is outputting the HTML. You need to find the actual code responsible before there’s any hope of resolving this.

    Often first row errors like this are due to faulty logic within the loop when various elements are output in various parts of the code. For example, the form tag is output from a conditional that fails to go true until one pass has been made, due to the wrong value being checked in the conditional. Other elements are output unconditionally. Without seeing the code responsible, this is just a wild guess.

    @bcworkz

    Not the most elegant solution, but we encountered a similar issue on a project.

    Could not find any help relating to the bug, but managed to find a “dirty” solution.

    Found that you can add a dummy html form tag, which will be deleted on the first post in your list, but your second form tag should remain 🙂

    For all the other posts, it will have both form tags, but it shouldn’t matter as the first one isn’t doing anything.

    (see below)

    case 'delete_aanmeldingen' :
            echo '
                <!-- Sacrificial form tag -->
                <form></form>
    
                <form method="post">
                <input type="submit" name="delete-'. $post_id .'" value="delete"/>
                </form>
            ';
    }

    Hope it helps if you didn’t solve this already!

    • This reply was modified 7 months, 3 weeks ago by taylorbennett. Reason: typos
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Issue with customizing the WP Admin table of Custom Post Type’ is closed to new replies.