这就是我正在做的。。。在BuddyPress中,在删除用户帐户之前,如果用户之前上载了附件,他或她将被重定向到自己的附件列表:
function custom_get_count_before_delete() {
global $bp, $wpdb;
$user_id = $bp->loggedin_user->id;
return $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(DISTINCT p.ID)
FROM $wpdb->posts p
WHERE p.post_author = %d
AND p.post_status = \'inherit\'
AND p.post_type = \'attachment\'", $user_id
) );
}
function custom_check_delete_account () {
global $bp;
if ( $bp->current_component == \'settings\' && $bp->current_action == \'delete-account\' ) {
if ( $count = custom_get_count_before_delete() ) {
wp_redirect(\'http://www.mysite.lh/wp-admin/upload.php\', 301 ); exit;
}
}
}
add_action(\'init\', \'custom_check_delete_account\', 11);
我的问题是:如何在重定向之前设置或注入消息或警告消息,并在重定向之后在“/wp admin/upload.php”附件列表中打印?
提前谢谢。
最合适的回答,由SO网友:José Pablo Orozco Marín 整理而成
我明白了:
function custom_get_count_before_delete() {
global $bp, $wpdb;
$user_id = $bp->loggedin_user->id;
return $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(DISTINCT p.ID)
FROM $wpdb->posts p
WHERE p.post_author = %d
AND p.post_status = \'inherit\'
AND p.post_type = \'attachment\'", $user_id
) );
}
function custom_check_delete_account() {
global $bp;
if ( $bp->current_component == \'settings\' && $bp->current_action == \'delete-account\' ) {
if ( $count = custom_get_count_before_delete() ) {
$message = \'please_delete_your_media_before_delete_your_account\';
wp_redirect(\'http://www.mysite.lh/wp-admin/upload.php?message=\' . $message, 301 );
exit;
}
}
}
add_action(\'init\', \'custom_check_delete_account\', 11);
function custom_upload_notice() {
echo \'<div id="message" class="updated below-h2">
<h2>Primer paso:</h2><p>Para borrar su cuenta, por favor borre todos sus Chaines primero.</p>
</div>\';
remove_action(\'admin_notices\', \'custom_upload_notice\');
}
function custom_check_delete_account_upload() {
global $pagenow;
if ( !isset($_REQUEST[\'deleted\']) ) {
if ( \'please_delete_your_media_before_delete_your_account\' == $_REQUEST[\'message\'] && \'upload.php\' == $pagenow ) {
add_action(\'admin_notices\', \'custom_upload_notice\');
}
}
}
add_action(\'admin_init\', \'custom_check_delete_account_upload\', 11);
请让我知道你的想法,或者是否有更好的方法。