Home Forums Plugin Support WP User Frontend Pro Delete post action for custom template

This topic is: resolved

This topic contains 9 replies, has 2 voices, and was last updated by  docandtee 5 years, 1 month ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #14188

    docandtee
    Participant
    Post count: 25

    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

    #14205

    Tareq Hasan
    Keymaster
    Post count: 2831

    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

    #14660

    docandtee
    Participant
    Post count: 25

    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

    #14704

    Tareq Hasan
    Keymaster
    Post count: 2831

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

    #14721

    docandtee
    Participant
    Post count: 25

    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

    #14867

    docandtee
    Participant
    Post count: 25

    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

    #14920

    Tareq Hasan
    Keymaster
    Post count: 2831

    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.

    #14955

    docandtee
    Participant
    Post count: 25

    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

    #14965

    Tareq Hasan
    Keymaster
    Post count: 2831

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

    #14968

    docandtee
    Participant
    Post count: 25

    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 10 posts - 1 through 10 (of 10 total)

The topic ‘Delete post action for custom template’ is closed to new replies.