Delete post action for custom template

This topic contains 9 reply and 2 voices, and was last updated by docandtee 10 years, 2 months ago
Viewing 9 Posts - 1 through 9 (of 9 total)
Author Posts
January 6, 2014 at 11:30 pm 14188
docandtee Hello I'm trying to create my own dashboard templates to display different post type - each different post type needs a different layout for the dashboard. I've got it all working pretty nicely apart from one thing - I can't get the delete post link to work. I've got this in my page template, which seems to generate the correct url including wp_nonce - except nothing actually happens when you click on it:
<?php if ( wpuf_get_option( 'enable_post_del', 'wpuf_dashboard', 'yes' ) == 'yes' ) {
                                    $del_url = add_query_arg( array('action' => 'del', 'pid' => $post->ID) );
                                    ?>
                                    <a href="<?php echo wp_nonce_url( $del_url, 'wpuf_del' ) ?>" onclick="return confirm('Are you sure to delete?');"><?php _e( 'Delete', 'wpuf' ); ?></a>
                                <?php } ?>
Am I missing something? Any help would be much appreciated. D
January 7, 2014 at 3:55 pm 14205
Tareq Hasan Tareq Hasan

The delete function only works in WPUF’s dashboard page, it was loaded that way. So even if you ‘ve created the same URL, it won’t work. You need to make sure you are also replicating the delete function in /class/frontend-dashboard.php

January 21, 2014 at 6:07 pm 14660
docandtee docandtee

Hi Tareq
Thanks for getting back to me.
I’m still struggling with this.
I’ve added this to my functions file but still not able to delete posts from the front end.

    function delete_post() {
        global $userdata;

        $nonce = $_REQUEST['_wpnonce'];
        if ( !wp_verify_nonce( $nonce, 'wpuf_del' ) ) {
            die( "Security check" );
        }

        //check, if the requested user is the post author
        $maybe_delete = get_post( $_REQUEST['pid'] );

        if ( ($maybe_delete->post_author == $userdata->ID) || current_user_can( 'delete_others_pages' ) ) {
            wp_delete_post( $_REQUEST['pid'] );

            //redirect
            $redirect = add_query_arg( array('msg' => 'deleted'), get_permalink() );
            wp_redirect( $redirect );
        } else {
            echo '<div class="error">' . __( 'You are not the post author. Cheeting huh!', 'wpuf' ) . '</div>';
        }
    }

}

Am I missing anything else?

Many thanks

D

January 22, 2014 at 11:46 pm 14704
Tareq Hasan Tareq Hasan

Can you paste your whole code to pastebin.com and share the link here?

January 23, 2014 at 6:19 pm 14721
docandtee docandtee

Hi Tareq
Here’s the template code:
http://pastebin.com/34jAnKbm

It’s a bit over complicated I know – but it’s an overly complicated project!

Let me know what you think I should add to my functions file.

Many thanks

D

January 29, 2014 at 4:41 pm 14867
docandtee docandtee

Hi Tareq
Did you have a chance to look at my code?
Still unsure as to what I need to add to my functions file to get this delete function to work.
Would be really useful.

Many thanks

D

January 30, 2014 at 11:55 pm 14920
Tareq Hasan Tareq Hasan

Here’s the delete_post function which belongs to WPUF_Frontend_Dashboard class and it runs only with wpuf_dashboard shortcode.

[php]
function delete_post() {
global $userdata;

$nonce = $_REQUEST[‘_wpnonce’];
if ( !wp_verify_nonce( $nonce, ‘wpuf_del’ ) ) {
die( "Security check" );
}

//check, if the requested user is the post author
$maybe_delete = get_post( $_REQUEST[‘pid’] );

if ( ($maybe_delete->post_author == $userdata->ID) || current_user_can( ‘delete_others_pages’ ) ) {
wp_delete_post( $_REQUEST[‘pid’] );

//redirect
$redirect = add_query_arg( array(‘msg’ => ‘deleted’), get_permalink() );
wp_redirect( $redirect );
} else {
echo ‘<div class="error">’ . __( ‘You are not the post author. Cheeting huh!’, ‘wpuf’ ) . ‘</div>’;
}
}
[/php]

it’s used as:
[php]
if ( isset( $_REQUEST[‘action’] ) && $_REQUEST[‘action’] == "del" ) {
$this->delete_post();
}
[/php]

So try this on your template page. Remember to rename the function and call it accordingly.

January 31, 2014 at 5:58 pm 14955
docandtee docandtee

Hey Tareq
Thanks for your help on this. Still not getting this to work.
Here’s what I’ve got in my functions:

function delete_gallery_image() {
global $userdata;

$nonce = $_REQUEST['_wpnonce'];
if ( !wp_verify_nonce( $nonce, 'wpuf_del' ) ) {
die( "Security check" );
}

//check, if the requested user is the post author
$maybe_delete = get_post( $_REQUEST['pid'] );

if ( ($maybe_delete->post_author == $userdata->ID) ||
current_user_can( 'delete_others_pages' ) ) {
wp_delete_post( $_REQUEST['pid'] );

//redirect
$redirect = add_query_arg( array('msg' => 'deleted'),
get_permalink() );
wp_redirect( $redirect );
} else {
echo '<div class="error">' . __( 'You are not the
post author. Cheeting huh!', 'wpuf' ) . '</div>';
}
}

And here’s my posts query in my template file:

<?php global $current_user; get_currentuserinfo(); $user_id = get_current_user_id();
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] ==
"del" ) {
$this->delete_gallery_image();
}	
$homequery = new WP_Query( array( 'author' => $user_id, 'post_type' => 'artistimage', 'showposts' => '50')  ); ?>
<?php while ( $homequery->have_posts() ) : $homequery->the_post(); ?>
<img src="<?php $image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'blog-feature-sml', true);
echo $image_url[0];  ?>" alt="<?php the_title(); ?>" />
<?php $del_url = add_query_arg( array('action' => 'del', 'pid' => $post->ID) );?>
<a href="<?php echo wp_nonce_url( $del_url, 'wpuf_del' ) ?>" onclick="return confirm('Are you sure to delete?');"><?php _e( 'Delete', 'wpuf' ); ?></a>
<?php endwhile; wp_reset_postdata(); ?>	

Can you see where I’m going wrong with this? It’s driving me slightly nuts!

Many thanks

D

January 31, 2014 at 8:57 pm 14965
Tareq Hasan Tareq Hasan

$this->delete_gallery_image();, your function is not a member of a PHP Class. So just call it like delete_gallery_image();

January 31, 2014 at 9:09 pm 14968
docandtee docandtee

Aha! Of course!
Thank you so much for your help Tareq. Amazing support!
Work perfectly now – and am now able to create my own templates!
Hopefully this will be of use to others out there.

Cheers and many thanks.

D

Viewing 9 Posts - 1 through 9 (of 9 total)