假设您正在谈论一个自定义用户role 名为“摄影师”,我相信这样的东西应该会增加delete_posts
该角色的能力。
function add_delete_cap_to_photog_role() {
$role = get_role( \'photographer\' );
$role->add_cap( \'delete_posts\' );
}
add_action( \'admin_init\', \'add_delete_cap_to_photog_role\');
为角色添加上限后,要解决其余问题,您可以
保持blockusers_init
和沟渠get_delete_post_link
对于ajax删除挖沟blockusers_init
函数并执行条件重定向我会对每一个都给出一些想法。我喜欢开沟get_delete_post_link
在这种情况下。
Several steps below, so be aware the code is provided as a guide only. Rewrite and rename things as needed.
沟渠get_delete_post_link
AJAX删除替换get_delete_post_link
用这样的方式排队:
<?php if( current_user_can( \'delete_post\' ) ) : ?>
<a href="#" data-id="<?php the_ID() ?>" data-nonce="<?php echo wp_create_nonce(\'ajax_delete_post_nonce\') ?>" class="delete-post">delete</a>
<?php endif ?>
Enqueue some JS 在文件中:函数。php
function delete_post_ajax() {
wp_enqueue_script( \'delete_ajax\', get_template_directory_uri() . \'/js/my_script.js\', array( \'jquery\' ), \'1.0.0\', true );
wp_localize_script( \'delete_ajax\', \'TheAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
}
add_action( \'wp_enqueue_scripts\', \'delete_post_ajax\' );
onClick to pass data to delete method
在文件:/js/my\\u脚本中。js公司
jQuery( document ).ready( function($) {
$(document).on( \'click\', \'.delete-post\', function() {
var id = $(this).data(\'id\');
var nonce = $(this).data(\'nonce\');
var post = $(this).parents(\'.post:first\');
$.ajax({
type: \'post\',
url: TheAjax.ajaxurl,
data: {
action: \'wpse_ajax_delete_post\',
nonce: nonce,
id: id
},
success: function( result ) {
if( result == \'success\' ) {
post.fadeOut( function(){
post.remove();
});
}
}
})
return false;
})
})
the delete method
文件中:函数。php(我们需要的钩子只是“
wp_ajax
“在
action
在js文件中)
add_action( \'wp_ajax_wpse_ajax_delete_post\', \'wpse_ajax_delete_post_func\' );
function wpse_ajax_delete_post_func(){
$permission = check_ajax_referer( \'ajax_delete_post_nonce\', \'nonce\', false );
if( $permission == false ) {
echo \'error\';
}
else {
wp_delete_post( $_REQUEST[\'id\'] );
echo \'success\';
}
die();
}
要通过传递uri参数执行上述操作:
更改我们调出的位置get_delete_posts_link
为此:
<?php if( current_user_can( \'delete_post\' ) ) : ?>
<?php $nonce = wp_create_nonce(\'ajax_delete_post_nonce\') ?>
<a href="<?php echo admin_url( \'admin-ajax.php?action=wpse_ajax_delete_post&id=\' . get_the_ID() . \'&nonce=\' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<?php endif ?>
查看此
a more involved walkthrough explaining each step 沟渠blockusers_init
(又名条件重定向)如果用户没有manage_options
cap,这是一个管理员角色。但首先,它会使用一些css伪隐藏内容并向用户添加消息:
The CSS bit
在文件中:
函数。phpadd_action(\'admin_head\', \'hide_admin_via_css\');
function hide_admin_via_css() {
if (!current_user_can( \'manage_options\' )) {
echo \'<style>body * {visibility:hidden !important;} body:before {content:"Give it a second...";}
\';
}
}
Enqueueing the JS file
在文件中:
函数。phpfunction my_enqueue( $hook ) {
if (!current_user_can( \'manage_options\' )) {
wp_enqueue_script( \'my_custom_script\', get_template_directory_uri() \'/js/block-admin.js\' );
}
}
add_action(\'admin_enqueue_scripts\', \'my_enqueue\');
Set the JS to just immediately redirect
文件:
theme\\u root/js/block admin。js公司window.location.href = "/";
OR a timed redirect
setTimeout(function () {
window.location.href = "/";
}, 2000);
这种方法
came from clicknathan, and he offers more details here.