WordPress admin_notices通知显示在错误的位置

axr492tv  于 2023-03-29  发布在  WordPress
关注(0)|答案(1)|浏览(162)

我正在尝试向我的WordPress插件添加通知和错误消息。使用admin_notices钩子,我的代码看起来像这样:

<?php
//Define notifications
function customer_admin_notice($message_type, $customer_nr = "") {
    if ($message_type === 'customer_update_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer updated successfully.'; ?></p>
        </div>
        <?php
    } else if ($message_type === 'customer_new_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer added successfully.'; ?></p>
        </div>
        <?php
    }  else if ($message_type === 'customer_delete_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer with number ' . $customer_nr . ' has been deleted.'; ?></p>
        </div>
        <?php
    } else if ($message_type === 'customer_delete_error') {
        ?>
        <div class="notice notice-error is-dismissible">
            <p><?php echo 'No customer with number ' . $customer_nr . ' was found.'; ?></p>
        </div>
        <?php
    }
}
add_action('admin_notices', 'customer_admin_notice');
?>

通知会打印出来,但看起来像这样:Notification shows full width and behind side panel
我还注意到,通知get直接附加在body标签下,不像其他附加到wpbody-content的通知:DOM
有没有人知道为什么通知没有正确显示?

xxe27gdn

xxe27gdn1#

似乎admin_notices钩子在呈现wpbody-content容器之前被触发。下面是更新后的代码,应该可以正常工作:

<?php
//Define notifications
function customer_admin_notice($message_type, $customer_nr = "") {
    if ($message_type === 'customer_update_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer updated successfully.'; ?></p>
        </div>
        <?php
    } else if ($message_type === 'customer_new_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer added successfully.'; ?></p>
        </div>
        <?php
    }  else if ($message_type === 'customer_delete_success') {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo 'Customer with number ' . $customer_nr . ' has been deleted.'; ?></p>
        </div>
        <?php
    } else if ($message_type === 'customer_delete_error') {
        ?>
        <div class="notice notice-error is-dismissible">
            <p><?php echo 'No customer with number ' . $customer_nr . ' was found.'; ?></p>
        </div>
        <?php
    }
}

function customer_admin_notice_wrapper() {
    echo '<div class="wrap">';
    do_action( 'customer_admin_notices' );
    echo '</div>';
}

add_action('admin_notices', 'customer_admin_notice');
add_action('customer_admin_notices', 'customer_admin_notice_wrapper');
?>

它可能会帮助你解决这个问题!

相关问题