繁体   English   中英

提交由WordPress插件中的函数呈现的自定义表单后,如何显示admin_notice?

[英]How do I show admin_notice after submitting a custom form which is rendered by function in WordPress plugin?

我正在开发一个简单的WordPress插件,管理员可以在其中使用管理表单创建用户角色。 如果角色已经存在,我想显示一个admin_notice

我遇到的问题甚至是该角色存在,消息也没有出现。 如果我理解正确,这是因为该表单实际上已重定向到admin-post.php 如果是这样,提交后如何显示admin_notice?

简而言之,我有这个:

  • 我的插件中的一个函数可以呈现表单。
  • 一种保存的功能。 保存之前,WP检查角色是否存在。 如果是,我想显示一个admin_notice并“返回”。 否则,角色将被保存。

检查并显示admin_notice的代码段:

<?php

function save_user_role()
{
    $role_name = $_POST['role_name'];
    $role_slug = sanitize_title_with_dashes($role_name);
    $role = get_role($role_slug);
    if (!empty($role))
    {
        $notice = 'Role exists';
        do_action('admin_notices', $this->display_notice($notice));

        // return; // If I use the return the correct message coming up in a blank page.

    }
    else
    {

        // Save the role

    }
}

以下是我编写的完整代码

function save_user_role() {
   // Role already exists?
   $role_name = $_POST[ 'coolmedia_role_name' ];
   $role_slug = sanitize_title_with_dashes( $role_name );
   $role = get_role( $role_slug );

   if( ! empty( $role ) ) {
      // Error. Role already exists
      $notice = 'Speficed role ' . $role_name . ' already exists';
      do_action( 'admin_notices', $this->display_notice( $notice ) );
   } else {
     // Safe to create the new role
     $role_caps = array(
       'read'  => true,
     );

     ...

     add_role( $role_slug, esc_html( $role_name ), $role_caps );

     // Redirect back to form page
     wp_redirect( admin_url( 'admin.php?page=user-roles' ) );
   }
}

显示通知功能:

function add_user_role_markup() { ?>
     <div class="wrap">
        <h1>Add Role</h1>
        <form method="post" action="<?php esc_html(admin_url('admin-post.php')); ?>">
        <table class="form-table">
           <tr class="first">
              <th>Role name</th>
              <td><input required type="text" id="role_name" name="role_name" /></td>
           </tr>
           <tr>
               <th>
                   <?php
('coolmedia-role-save', 'coolmedia-role-nonce'); ?>
                   <input type="hidden" name="action" value="new_user_role">
               </th>
               <td><?php
('Save Role'); ?></td>
           </tr>
        </table>
        </form>
</div>
<?php
}

这取决于何时save_user_role以及该函数之后save_user_role发生什么。 可能是admin_notices钩子正在触发,但是(如您所建议的那样)发生了重定向,这将导致它在重定向后触发,因为(正确地) save_user_role没有被再次调用,或者save_user_roleadmin_notices 之后被触发了钩子被称为(在这种情况下,您来不及了)。

一种解决方案可能是在通知时将选项临时存储在数据库中,然后检查该选项并根据需要显示该选项(如果存在)。 这样,WordPress可以进行重定向,然后检查并在返回时显示您的通知。

这样的事情。 (注意:我的add_action假设这是在一个类中发生的,我假设这是您根据对$this->display_notice()使用来构造它的方式)

function save_user_role(){
  ...
  if( ! empty( $role ) ) {
    // Error. Role already exists
    $notice = 'Specified role ' . $role_name . ' already exists';
    update_option( 'coolmedia_role_exists_message', $notice, 'no' );
  }
  ...
}

add_action( 'admin_notices', array( $this, 'maybe_display_notice' ) );
function maybe_display_notice(){
  $notice = get_option( 'coolmedia_role_exists_message', false );
  if( $notice ){
    delete_option( 'coolmedia_role_exists_message' );
    $this->display_notice( $notice );
  }
}

function display_notice( $notice ){
  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM