Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在OOP插件中使用WordPress admin_通知_Php_Oop_Wordpress - Fatal编程技术网

Php 在OOP插件中使用WordPress admin_通知

Php 在OOP插件中使用WordPress admin_通知,php,oop,wordpress,Php,Oop,Wordpress,我在WordPress中注册了一个新的管理页面,在这里我显示输出HTML和格式,验证一些输入并保存到数据库。一切都很好。真正不起作用的是使用admin_notices钩子显示任何错误/更新消息。我猜我不能从add\u menu\u page函数中调用这个钩子?如果是这样的话,您打算如何处理OOP插件中自定义页面上的错误 我在下面包含了一个非常精简的代码版本 class Fitems_Admin { public $admin_notices = array(); public

我在WordPress中注册了一个新的管理页面,在这里我显示输出HTML和格式,验证一些输入并保存到数据库。一切都很好。真正不起作用的是使用admin_notices钩子显示任何错误/更新消息。我猜我不能从add\u menu\u page函数中调用这个钩子?如果是这样的话,您打算如何处理OOP插件中自定义页面上的错误

我在下面包含了一个非常精简的代码版本

class Fitems_Admin {

    public $admin_notices = array();

    public function __construct(){
        // Register Menus
        add_action( 'admin_menu', array( $this, 'register_menus' ) );
    }

    public function register_menus(){
        add_menu_page( 'fItems', 'fItems', 'administrator', 'fItems', array( $this, 'items' ) );
    }

    public function items(){
        // do stuff, validate input & register any errors, example below
        $this->admin_notices['updated'][] = __( 'item(s) successfully deleted.', 'fItems' );
        // Register errors
        add_action( 'admin_notices', array( $this , 'display_admin_notices' ) );
        // display output;
        echo $output;
    }

    // Just formats and echos any errors in the $this->admin_notices array();
    public function display_admin_notices( $return = FALSE ){
        if( ! empty( $this->admin_notices ) ){
            // Remove an empty and then sort
            array_filter( $this->admin_notices );
            ksort( $this->admin_notices );
            $output = '';
            foreach( $this->admin_notices as $key => $value ){
                // Probably an array but best to check
                if( is_array( $value ) ){
                   foreach( $value as $v ){
                       $output .= '<div class="' . esc_attr( $key ) . '"><p>' . esc_html( $v ) . '</p></div>';
                   }
                } else {
                    $output .= '<div class="' . esc_attr( $key ) . '"><p>' . esc_html( $value ) . '</p></div>';
                }
            }
            if( $return ){
                return $output;
            } else {
                echo $output;
            }
        }
    }
}
class Fitems\u Admin{
public$admin_notices=array();
公共函数构造(){
//注册菜单
添加操作('admin_menu',数组($this,'register_menu'));
}
公共功能寄存器\菜单(){
添加菜单页面('fItems','fItems','administrator','fItems',数组('this','items');
}
公共职能项目(){
//做一些事情,验证输入并注册任何错误,下面的例子
$this->admin_notices['updated'][]=“已成功删除项目”,“fItems”);
//寄存器错误
添加动作('admin_notices',数组($this,'display_admin_notices'));
//显示输出;
echo$输出;
}
//只需格式化并回显$this->admin_notices数组()中的任何错误即可;
公共功能显示\管理\通知($return=FALSE){
如果(!empty($this->admin_notices)){
//删除一个空文件,然后进行排序
数组过滤器($this->admin\u notices);
ksort($this->admin_通知);
$output='';
foreach($this->admin\u通知为$key=>$value){
//可能是数组,但最好检查
if(是_数组($value)){
foreach(价值为$v){
$output.=''.esc_html($v)。'

'; } }否则{ $output.=''.esc_html($value)。'

'; } } 如果($返回){ 返回$output; }否则{ echo$输出; } } } }
管理员通知不用于此操作,并且钩子在您放置钩子的位置不起作用。为了有选择地触发它,我们使用
get\u选项
get\u user\u meta

对于此类通知,我们使用admin类:

if( $success )
    echo '<div class="updated"><p>class .updated with paragraph</p></div>';

if( $error )
    echo '<div class="error"><p>class .error with paragraph</p></div>';
if($success)
回音“类。更新为段落“”;
如果($error)
回显“类。段落出现错误”

下面是一个帮助器插件,用于设计我们的管理屏幕:


或者,使用整体,它具有显示通知的功能,不管名称如何,它都用于显示
更新
错误
消息。

试试它可能会起作用!将所有通知存储在一个变量中
$error\u notices
$error\u notices=$this->admin\u notices['updated'][]=\uuu('item(s)successfully deleted','fItems')
函数my_admin_notice(){
全局$error_notices;
回显$error_notices;
添加操作('admin_notices',$this->my_admin_notices')哼,好吧,所以我第一次做的是正确的。我想如果你想使用尽可能多的WordPress内置函数,你可能会有点集成狂!!谢谢,很有趣。它实际上不是一个设置页面(或者使用任何设置API),而是一个自定义表单。问题是add_settings_error()应该只与settings API一起使用,还是应该用作一般的错误/通知函数。我在想前者?我们只有在使用API时才能使用该函数。我们可以在任何自定义插件或主题页面中自由使用API。不编译一些错误配置,但这就是想法。在重定向后,如何确定它是成功还是错误?