0

I've got only one function left that I need to solve (if it's solvable).

I've got a frontend post system for unregistered users. But there are no way for the users to delete a post if they later want.

So I was thinking, if I in my form (where they create their post), ask them to write a password in a custom field, is there then anyway to delete the post by using the post id nr and the password they provided?

.. Or is this completely impossible?

The reason I want this is because currently no front-end custom poster plugins have any preview function, and maybe the user did something wrong and they want to do another post..

0

I assume that you are using WordPress.

You can do the following things alternatively :
While the user is creating a post, allot them a [post_id] and [password]/[hash/key] whatever.
When they will comeback and try to delete ask them for the id and password/hash to delete.

By using get_the_ID you can display the post id.
And hash can be created wp_generate_password function.
Also you have to use add_post_meta to save the hash.
In this example the delete form is always visible you can use additional js and css to hid this and visible on click.

SOLUTION

//FUNCTION TO INSERT DELETE FORM AFTER EVERY POST.

function auto_insert_after_post($content){
    if (is_single()) {
        $content .= '
            <form action="'. esc_url( admin_url("admin-post.php") ) .'" method="post">
                <input type="text" name="aid" required>
                <input type="text" name="hash" required>
                <input type="hidden" name="action" value="hash_auth">
                <input type="submit" name="submit" value="Delete">
            </form>
        ';
        $content .= "value:".get_post_meta( get_the_ID(), "delete_auth_key", true );
    }
    return $content;
}

//FUNCTION TO GENERATE A POST HASH/PASSWORD DURING POST CREATION.

function generate_post_hash($id, $post){
    $hash_key = "delete_auth_key";
    $hash = wp_generate_password(10);
    add_post_meta($id, $hash_key, $hash);
}

//AUTHENTICATE AND DELETE POST

function delete_hash_auth(){
    //check if form fields are filled
    if(!empty($_POST['aid']) && !empty($_POST['hash'])){
        $aid = $_POST['aid'];
        $hash = $_POST['hash'];
        $auth_hash = get_post_meta($aid, "delete_auth_key", true);
        //check if the hash and the post id matches
        if(get_post($aid) && $auth_hash == $hash){
            //check if auth_hash matches hash
            wp_delete_post($aid);
        }
        else{
            echo "Please enter correct post id and hash!";
        }
    }
    else{
        echo "Empty fields not allowed!";
    }
    
}
add_action( 'admin_post_nopriv_hash_auth', 'delete_hash_auth', 10);
add_action( 'admin_post_hash_auth', 'delete_hash_auth', 10 );
add_action( "publish_post", "generate_post_hash", 20, 2 );
add_filter( "the_content", "auto_insert_after_post" );

IMPLEMENT USING PLUGIN

These are the steps to implement using a plugin or you can simply download this plugin I have written. Download Frontend Delete

If you downloaded the plugin.

  1. Go to Dashboard > Plugin > Add New
  2. Upload the downloaded zip.
  3. Activate the plugin.
  4. Done.

If you wanna write it the please follow the steps:

  1. Create a Directory frontend-del in wp-content/plugins folder
  2. Create frontend-del.php inside that folder.
  3. Paste this code below.

/*
Plugin Name: frontend-del
Plugin URI: https://www.wordpress.org
Description: Enable frontend delete.
Version: 1.0
Author: Behemoth
Author URI: https://wordpress.stackexchange.com/users/188582/behemoth
License: GPLv2 or later
*/

function delete_hash_auth(){
    
//check if form fields are filled
if(!empty($_POST['aid']) && !empty($_POST['hash'])){
    $aid = $_POST['aid'];
    $hash = $_POST['hash'];
    $auth_hash = get_post_meta($aid, "delete_auth_key", true);
    //check if the hash and the post id matches
    if(get_post($aid) && $auth_hash == $hash){
        //check if auth_hash matches hash
        wp_delete_post($aid);
    }
    else{
        echo "Please enter correct post id and hash!";
    }
}
else{
    echo "Empty fields not allowed!";
}

  }

add_action( 'admin_post_nopriv_hash_auth', 'delete_hash_auth', 10);
add_action( 'admin_post_hash_auth', 'delete_hash_auth', 10 ); 
function generate_post_hash($id, $post){
    $hash_key = "delete_auth_key";
    $hash = wp_generate_password(10);
    add_post_meta($id, $hash_key, $hash);
}
add_action( "publish_post", "generate_post_hash", 20, 2 );
function auto_insert_after_post($content){
    if (is_single()) {
        $content .= '
            <form action="'. esc_url( admin_url("admin-post.php") ) .'" method="post">
                <input type="text" name="aid" required>
                <input type="text" name="hash" required>
                <input type="hidden" name="action" value="hash_auth">
                <input type="submit" name="submit" value="Delete">
            </form>
        ';
        $content .= "value:".get_post_meta( get_the_ID(), "delete_auth_key", true );
    }
    return $content;
}
add_filter( "the_content", "auto_insert_after_post" );
  1. Do not skip the above comment line. and place the whole thing inside <?php ... ?>.
  2. Save it and go to your WordPress Dashboard > plugin and activate the frontend del plugin

CONCLUSION

This is a basic solution you can customise it to fit your needs. For example you may need a ajax submission or redirect it to your desired page.
Also you can further implement this using object oriented concepts.

| improve this answer | |
  • Hi @Behemoth thx for the information, sorry for the delay on my answer. I've been looking at it a couple of times, I'm still trying to workout where to put what.. or is all this code going into functions.php? – JoBe Jun 14 at 12:48
  • Yes, you can implement it in your functions.php file. – Behemoth Jun 15 at 7:36
  • thx a lot for the help, but I'm afraid I'm too stupid to implement those, well, add them to functions.php isn't an issue, but there it stops for me, I can't Ajax or java (basics, I can change and modify things, but not create something from scratch..) – JoBe Jun 15 at 18:35
  • Ok, then let me implement this in plugin so that you can implement it easily. – Behemoth Jun 18 at 6:56
  • I'm.. having problem with finding the right words, thank you.. – JoBe Jun 18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.