This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programmieren:wordpress:klassische_metabox_fuer_posts_erstellen [2023/06/14 19:26] – jgehrke | programmieren:wordpress:klassische_metabox_fuer_posts_erstellen [2024/08/10 10:38] (current) – jgehrke | ||
---|---|---|---|
Line 4: | Line 4: | ||
<code php> | <code php> | ||
- | /* | + | <?php |
- | METABOX EINKLINGEN | + | /* |
- | */ | + | |
new MetaboxPost(); | new MetaboxPost(); | ||
/** | /** | ||
- | | + | |
*/ | */ | ||
class MetaboxPost | class MetaboxPost | ||
Line 23: | Line 22: | ||
public function add_metabox() | public function add_metabox() | ||
{ | { | ||
- | $mb_id | + | $mb_id |
- | $mb_title | + | $mb_title |
$mb_callback | $mb_callback | ||
$mb_screens | $mb_screens | ||
Line 51: | Line 50: | ||
// Update von einem Metafeld | // Update von einem Metafeld | ||
// | // | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Alternative als statische Klasse ===== | ||
+ | |||
+ | Das hier ist eine alternative Version mit einer Klasse aus Static-Methods. Der Vorteil ist, dass diese nicht initialisiert werden muss, und wirklich nur aufgerufen wird, wenn Metaboxes von Wordpress gerendert werden. | ||
+ | |||
+ | <code php> | ||
+ | <? | ||
+ | |||
+ | namespace wp_admin\wp_metabox; | ||
+ | |||
+ | add_action( ' | ||
+ | add_action( ' | ||
+ | |||
+ | final class MyMetaBox | ||
+ | { | ||
+ | private static $ID = ' | ||
+ | private static $title | ||
+ | private static $render_callback = [' | ||
+ | private static $show_on_screens = [ ' | ||
+ | private static $position | ||
+ | private static $priority | ||
+ | |||
+ | public static function add_metabox() | ||
+ | { | ||
+ | add_meta_box( self::$ID, self:: | ||
+ | } | ||
+ | |||
+ | public static function render_metabox() | ||
+ | { | ||
+ | echo "< | ||
+ | } | ||
+ | |||
+ | public static function save() | ||
+ | { | ||
+ | // Nichts machen bei Auto-Save | ||
+ | if ( defined( ' | ||
+ | return $post_id; | ||
+ | } | ||
+ | |||
+ | // Sanitizen von Eingaben | ||
+ | //$mydata = sanitize_text_field( $_POST[' | ||
+ | |||
+ | // Update von einem Metafeld | ||
+ | // | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Erweitertes Beispiel mit statischer Klasse ===== | ||
+ | |||
+ | In diesem Beispiel ist alles etwas konkreter. Es zeigt wie Daten aus einer Textarea gespeichert und geladen werden. | ||
+ | |||
+ | Mit dieser Variante, kann man auf die gespeicherten Daten der Metabox auch direkt über die statische Methode zurück greifen, via: | ||
+ | '' | ||
+ | |||
+ | <code php> | ||
+ | <? | ||
+ | |||
+ | | ||
+ | |||
+ | add_action( ' | ||
+ | add_action( ' | ||
+ | |||
+ | final class AdditionalPostInfos | ||
+ | { | ||
+ | private static $ID = ' | ||
+ | private static $title | ||
+ | private static $render_callback = [' | ||
+ | private static $show_on_screens = [ ' | ||
+ | private static $position | ||
+ | private static $priority | ||
+ | |||
+ | private static $entry_meta_key | ||
+ | |||
+ | public static function add_metabox() | ||
+ | { | ||
+ | add_meta_box( self::$ID, self:: | ||
+ | } | ||
+ | |||
+ | public static function render_metabox() | ||
+ | { | ||
+ | $value_description = self:: | ||
+ | |||
+ | echo <<< | ||
+ | <div class=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | .mb_additional_post_infos label{ | ||
+ | width : 100%; | ||
+ | } | ||
+ | .mb_additional_post_infos input{ | ||
+ | width : 100%; | ||
+ | } | ||
+ | .mb_additional_post_infos textarea{ | ||
+ | margin | ||
+ | width | ||
+ | border | ||
+ | outline | ||
+ | border-radius | ||
+ | border-left | ||
+ | background-color : rgb(245, | ||
+ | color | ||
+ | resize | ||
+ | transition | ||
+ | box-shadow | ||
+ | } | ||
+ | .mb_additional_post_infos textarea: | ||
+ | border-left | ||
+ | background-color : rgb(250, | ||
+ | } | ||
+ | .mb_additional_post_infos textarea: | ||
+ | border | ||
+ | outline | ||
+ | border-left | ||
+ | color | ||
+ | background-color : rgb(255, | ||
+ | box-shadow | ||
+ | } | ||
+ | |||
+ | .mb_additional_post_infos p em { | ||
+ | margin | ||
+ | font-size | ||
+ | line-height : 12px; | ||
+ | } | ||
+ | </ | ||
+ | HTML; | ||
+ | |||
+ | } | ||
+ | |||
+ | public static function save( $post_id ) | ||
+ | { | ||
+ | // Nichts machen bei Auto-Save | ||
+ | if( defined( ' | ||
+ | |||
+ | // Nichts tun, wenn gar kein Meta mit geschickt wurde | ||
+ | if( !isset( $_POST[' | ||
+ | |||
+ | update_post_meta( $post_id, ' | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | public static function get_description(): | ||
+ | { | ||
+ | global $post; | ||
+ | $post_meta_description = get_post_meta( $post-> | ||
+ | |||
+ | return $post_meta_description; | ||
} | } | ||
} | } | ||
</ | </ | ||