我只剩下一个需要解的函数(如果它是可解的)。
我有一个针对未注册用户的前端post系统。但是如果用户以后想删除帖子,他们没有办法。
所以我在想,如果我在我的表单(他们创建帖子的地方)中要求他们在自定义字段中写入密码,那么是否可以使用帖子id nr和他们提供的密码来删除帖子?
。。还是这完全不可能?
我之所以想这样做,是因为目前没有任何前端定制海报插件具有任何预览功能,而且可能是用户做错了什么,他们想再发一篇帖子。。
我只剩下一个需要解的函数(如果它是可解的)。
我有一个针对未注册用户的前端post系统。但是如果用户以后想删除帖子,他们没有办法。
所以我在想,如果我在我的表单(他们创建帖子的地方)中要求他们在自定义字段中写入密码,那么是否可以使用帖子id nr和他们提供的密码来删除帖子?
。。还是这完全不可能?
我之所以想这样做,是因为目前没有任何前端定制海报插件具有任何预览功能,而且可能是用户做错了什么,他们想再发一篇帖子。。
我假设您正在使用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
在该文件夹中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 ... ?>
.frontend del 插件结论这是一个基本的解决方案,您可以根据需要定制它。例如,您可能需要ajax提交或将其重定向到所需页面我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。