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..
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..
Share Improve this question asked May 31, 2020 at 5:20 JoBeJoBe 1712 silver badges11 bronze badges1 Answer
Reset to default 0I 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.
- Go to Dashboard > Plugin > Add New
- Upload the downloaded zip.
- Activate the plugin.
- Done.
If you wanna write it the please follow the steps:
- Create a Directory
frontend-del
in wp-content/plugins folder - Create
frontend-del.php
inside that folder. - Paste this code below.
/*
Plugin Name: frontend-del
Plugin URI: https://www.wordpress
Description: Enable frontend delete.
Version: 1.0
Author: Behemoth
Author URI: https://wordpress.stackexchange/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" );
- Do not skip the above comment line. and place the whole thing inside
<?php ... ?>
. - 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.