Support

Account

Home Forums Front-end Issues Using ACF to Delete Post From Frontend Reply To: Using ACF to Delete Post From Frontend

  • ETA: Just tested the below solution, no delete button :/

    I’m confused, but maybe I’m partially correct here? Using the inline method of including js per the link you provided, I’ve got this in my functions.php, which is basically all 3 code pieces provided in the tutorial I originally linked:

    add_filter('acf/save_post', 'delete_project');
    function delete_project($post_id) {
    $post_type = get_post_type($post_id);
    // change to post type you want them to be able to delete
    if ($post_type != 'post') {
    return;
    }
    if (get_field('delete_post', $post_id)) {
    $force_delete = false; // move to trash
    // change above to true to permanently delete
    wp_delete_post($post_id, $force_delete);
    header("Refresh:0");
    exit;
    }
    }
    
    function my_acf_input_admin_footer() { 
    ?>
    <script type="text/javascript">
    
    let editForms = $('.edit-proj-form form');
    const deleteBtn = 'Delete this post';
    editForms.each(function() {
    $(this).append(deleteBtn);
    });
    
    $(document).on('click', '.js-delete-proj', function() {
    const confirmDelete = window.confirm('This will permanently delete the project. Are you certain you wish to proceed?');
    const deleteCheck = $(this).siblings('.acf-form-fields').find('input[name="acf[delete_book]"]');
    const submitDiv = $(this).siblings('.acf-form-submit')[0];
    const submitBtn = $(submitDiv).find('.acf-button');
    if (confirmDelete) {
    deleteCheck.each(function() {
    $(this).val(1);
    });
    submitBtn.click();
    }
    });
    
    </script>
    <?php      
    }
    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    

    But am I understanding I now need to wrap the existing ACF edit post form I already have on my single post template in this div like so and then the delete post button will also appear?

    <?php
    if ( is_user_logged_in() && get_the_author_meta( 'ID' ) === get_current_user_id() )  { ?>
    <details style="float:right;">
      <summary>Edit Details</summary>
    <div class="edit-proj-form">
    	
    	
    <?php acf_form(array(
            'field_groups' => array(182, 1031, 156, 710), // Used ID of the field groups here.
             'post_title' => true, // This will show the title filed
             'post_content' => false, // This will show the content field
            'submit_value'  => __('Update')
    )); ?>	
    </div>
    </details> 
    <?php }	?>