Ich habe eine neue Admin-Seite in WordPress registriert, auf der ich Ausgabe-HTML und -format anzeige und einige Eingaben validiere und in der DB speichere. Alles funktioniert super. Was nicht wirklich funktioniert, ist die Verwendung des admin_notices-Hooks, um Fehler-/Aktualisierungsmeldungen anzuzeigen. Ich vermute, ich kann diesen Hook nicht innerhalb der add_menu_page-Funktion aufrufen? Wenn ja, wie sollen Sie mit Fehlern auf benutzerdefinierten Seiten in OOP-Plugins umgehen?
Ich habe eine sehr abgespeckte Version des Codes unten eingefügt.
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;
}
}
}
}
Probieren Sie es aus, es könnte funktionieren! Speichern Sie alle Ihre Benachrichtigungen in einer Variablen
$error_notices
$error_notices = $this->admin_notices['updated'][] = __( 'item(s) successfully deleted.', 'fItems' );
function my_admin_notice() {
global $error_notices;
echo $error_notices;
}
add_action( 'admin_notices', $this->my_admin_notice );
– Rameez SOOMRO
28. Oktober 2013 um 18:38 Uhr