我假设您正在使用WordPress。
您可以选择执行以下操作:
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.
通过使用get_the_ID
您可以显示帖子id。可以创建哈希wp_generate_password
功能
您还必须使用add_post_meta
保存哈希
在本例中,删除表单始终可见。您可以使用其他js和css隐藏该表单,并在单击时可见。
解决方案
//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" );
这些是使用插件实现的步骤,或者你可以下载我写的这个插件。下载
Frontend Delete如果你下载了插件。
转到仪表板;插件(>);添加新内容上载下载的zip激活插件完成如果您想写,请按照以下步骤操作:
创建目录frontend-del
在wp content/plugins文件夹中创建frontend-del.php
在该文件夹中将此代码粘贴到下面插件名称:前端del插件URI:https://www.wordpress.org
说明:启用前端删除
版本:1.0作者:Behemoth作者URI:https://wordpress.stackexchange.com/users/188582/behemoth
许可证:GPLv2或更高版本*/
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" );
不要跳过上面的注释行。把整个东西放进去
<?php ... ?>
.保存并转到WordPress仪表板;插入并激活
frontend del
插件结论这是一个基本的解决方案,您可以根据需要定制它。例如,您可能需要ajax提交或将其重定向到所需页面
您还可以使用面向对象的概念进一步实现这一点。