• Resolved meadows19

    (@meadows19)


    Hi. Thanks for this superb plugin!

    I want to add a ‘delete’ link after each post title, so that users can delete their own posts from the front end.

    I’m trying to do that using the the output filter and this code.:

    <?php
    $url = get_bloginfo('url');
      if (current_user_can('edit_post', $post->ID)){
        echo '<a class="delete-post" href="';
        echo wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID);
        echo '">Delete post</a>';
      }
    ?>

    I’m trying to modify the example here:

    https://displayposts.com/2019/01/04/display-time-after-post-date/#customize-output

    where something extra is added through the output filter, but I just can’t figure it out. Can someone give me any advice?

    Thank you kindly.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter meadows19

    (@meadows19)

    Hi

    I’ve been trying to do this, and have got this far:

    <?php
    /*
    Plugin Name: AAA test plugin
    Plugin URI: 
    Description: 
    Version: 
    Author: 
    Author URI: 
    License: 
    License URI: 
    */
    
    /**
     * Add Delete Link to Display Posts Shortcode plugin
     * @author 
     *
     * @param $output string, the original markup for an individual post
     * @param $atts array, all the attributes passed to the shortcode
     * @param $image string, the image part of the output
     * @param $title string, the title part of the output
     * @param $date string, the date part of the output
     * @param $excerpt string, the excerpt part of the output
     * @return $output string, the modified markup for an individual post
     */
     
    add_filter( 'display_posts_shortcode_output', 'be_display_posts_delete', 10, 6 );
    function be_display_posts_delete( $output, $atts, $image, $title, $date, $excerpt ) {
    	
    	// Here's the delete code we'll be adding to the excerpt
    	
    $delete = '';
    
    $url = get_bloginfo('url');
    if (current_user_can('edit_post', $post->ID)){
    
    $delete = '<a class="delete-post" href="' . wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID) . '">Delete post</a>';
    
    }
    
    	
    	// First check if an excerpt is included by looking at the shortcode $atts
    	if ( $atts['include_delete'] )
    		// Now let's rebuild the excerpt with the facebook code at the end
    		$excerpt = ' - <span class="delete">' . $delete . '</span>';
    	else $excerpt = '';
    	
    	// Now let's rebuild the output. Only the excerpt changed so we're using the original $image, $title, and $date
    	$output = '<li>' . $image . $title . $date . $excerpt . '</li>';
    	
    	// Finally we'll return the modified output
    	return $output;

    It now shows a delete text next to the post title. It’s messy and spaghetti code, because it is mixed it with the excerpt code. The problem is that the delete link doesn’t actually delete.

    WP gives an white error box when the link is clicked, saying the link has expired.

    Any clues?

    Thread Starter meadows19

    (@meadows19)

    So I managed to get it working like this:

    <?php
    /*
    Plugin Name: FM Delete Link
    Plugin URI: 
    Description: Creates a link that allows users to delete posts from the front end
    Version: 1.0
    Author: Felix Meadow
    Author URI:
    License: 
    License URI: 
    */
    
    /**
     * Add Delete Link to Display Posts Shortcode plugin
     *
     * @param $output string, the original markup for an individual post
     * @param $atts array, all the attributes passed to the shortcode
     * @param $image string, the image part of the output
     * @param $title string, the title part of the output
     * @param $date string, the date part of the output
     * @param $excerpt string, the excerpt part of the output
     * @return $output string, the modified markup for an individual post
     */
     
    add_filter( 'display_posts_shortcode_output', 'fm_display_posts_delete', 10, 6 );
    function fm_display_posts_delete( $output, $atts, $image, $title, $date, $excerpt ) {
    	
    // Here's the delete code we'll be adding to the excerpt
    $delete_button = '';	
    if (current_user_can('edit_post', $post->ID)){
    $delete_button = ' <a class="delete-post" href="';
    $delete_button .= get_delete_post_link( $post->ID );
    $delete_button .= '">Delete</a>';
    }
    
    	// First check if include_delete is included by looking at the shortcode $atts
    	if ( $atts['include_delete'] )
    		// Now let's rebuild the output with the delete link at the end
    		$delete_link = '<span class="delete">' . $delete_button . '</span>';
    	else $delete_link = '';
    	
    	// Now let's rebuild the output. Only the delete link has been added, so we're using the original $image, $title, and $date
    	$output = '<li>' . $image . $title . $date . $excerpt . $delete_link .'</li>';
    	
    	// Finally we'll return the modified output
    	return $output;
    }

    So now if I use the Display Posts shortcode with the option: include_delete=”true”
    it shows a link saying ‘Delete post’ if the one viewing has the permissions to do so.
    So it seems to work.

    Any ideas to tidy the code and remove anything unnecessary from it?

    Thread Starter meadows19

    (@meadows19)

    There must be an error somewhere, because my

    ul.display-posts-listing and li.listing-item CSS styling no longer works if the plugin is active, whether I include the include_delete option in the shortcode or not.

    Plugin Author Bill Erickson

    (@billerickson)

    Here you go: https://displayposts.com/2019/01/19/add-delete-post-link-if-user-has-permission-to-edit-content/

    You were having issues because your output didn’t include the listing item CSS classes and a few other elements in the listing.

    I updated the code in two ways:
    1. Return the original $output if the shortcode doesn’t contain include_delete=”true” or if the user cannot edit this post. This limits the scope of your markup changes.
    2. I updated the final output to have all the elements used by Display Posts.

    I hope this helps!

    Thread Starter meadows19

    (@meadows19)

    Hi Bill,

    Thank you so much for helping.

    I really appreciate that you took the time to write the code to help me, and also explained what you did.

    And of course, thank you also for writing and sharing the Display Posts plugin itself.

    Thank you!

    • This reply was modified 6 years, 2 months ago by meadows19.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add code using output filter’ is closed to new replies.