Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programmieren:wordpress:admin_pages_hinzufuegen [2020/02/29 08:07] jgehrkeprogrammieren:wordpress:admin_pages_hinzufuegen [2022/12/17 12:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
 Mit diesem Code Beispiel werden zwei Options / Settings Pages für Wordpress hinzugefügt. Dieser Code fügt nur die Seiten ins Menü ein. Es müssen noch Render Funktionen geschrieben werden, die eine Ausgabe erzeugen. Der Namespace ist optional, macht die Sache aber sauberer. Mit diesem Code Beispiel werden zwei Options / Settings Pages für Wordpress hinzugefügt. Dieser Code fügt nur die Seiten ins Menü ein. Es müssen noch Render Funktionen geschrieben werden, die eine Ausgabe erzeugen. Der Namespace ist optional, macht die Sache aber sauberer.
  
-===== Settings-Page Registrieren =====+===== 1. Settings-Page Registrieren (ohne Klasse) =====
  
 <code php> <code php>
Line 46: Line 46:
 Mehr dazu in der [[https://developer.wordpress.org/reference/functions/add_submenu_page/#used-by|Doku (siehe "used by"-Sektion)]]. Mehr dazu in der [[https://developer.wordpress.org/reference/functions/add_submenu_page/#used-by|Doku (siehe "used by"-Sektion)]].
  
-===== HTML Ausgabe und Speichern =====+ 
 +===== 2. Settings-Page als PHP-Klasse ===== 
 + 
 +Es kann nützlich sein Settings als PHP Class oder Objekt zu registrieren, damit alles in einem geschlossenem Objekt bleibt, z.b. für Plugins. Mit diesem Code wird eine Backendpage erzeugt.  
 + 
 +<code php> 
 +<?php 
 + 
 +namespace mein_namespace; 
 + 
 +/** 
 + * 
 + */ 
 +class Overview 
 +
 + 
 + private $plugin_url = ''; 
 + 
 + function __construct( $plugin_url ) 
 +
 + $this->plugin_url = $plugin_url; 
 + add_action( 'admin_menu', [ $this, 'add_page'] ); 
 + add_action( 'admin_enqueue_scripts', [ $this, 'register_scripts_styles' ] ); 
 +
 + 
 + public function add_page() 
 +
 + $page_title    = 'Tracking'; 
 + $menu_title    = 'Tracking'; 
 + $capability    = 'edit_posts'; 
 + $menu_slug     = 'plugin-hauptseite'; 
 + $page_function = [ $this, 'render_page']; 
 + $icon          = $this->plugin_url . 'assets/gfx/icon-line-chart-fill-white.png'; 
 + $position      = 50; 
 + add_menu_page( $page_title, $menu_title, $capability, $menu_slug,  $page_function, $icon, $position ); 
 +
 + 
 + public function register_scripts_styles(){ 
 + // Style Registrieren (noch nicht einbetten) 
 + /* 
 + $handle  = 'plugin-admin-style'; 
 + $src     = this->plugin_url . 'css/plugin_style.css'; 
 + $deps    = []; 
 + $version = '1.0'; 
 + wp_register_style( $handle, $src, $deps, $version ); 
 + */ 
 + 
 + // Script Registrieren (noch nicht einbetten) 
 + /* 
 + $handle    = 'alpine'; 
 + $src       = this->plugin_url  . 'js/alpine.min.js'; 
 + $deps      = []; 
 + $version   = '3.0'; 
 + $in_footer = false; 
 + wp_register_script( $handle, $src, $deps, $version,$in_footer ); 
 + */ 
 +
 + 
 + public function render_page() 
 +
 + //wp_enqueue_script( 'alpine' ); // Registriertes Script einbinden 
 + //wp_enqueue_style( 'moewe-arrowchat-admin-style' ); // Registrierten Style einbinden 
 +  
 + echo "<h1>Test</h1>"; 
 + include "html-datei.html"; 
 +
 +
 + 
 +</code> 
 + 
 +===== 3. HTML Ausgabe und Speichern (rudimentär) =====
  
 Page Renderer Function, so etwas wie: Page Renderer Function, so etwas wie:
 <code php> <code php>
-function renderer_function_unterseite(){ +function admin_setting_page_theme_links(){ 
- $menu_slug   = 'plugin-unterseite'';+ $menu_slug   = 'theme-links';
  $echo        = false;  $echo        = false;
  $form_action = menu_page_url( $menu_slug, $echo );  $form_action = menu_page_url( $menu_slug, $echo );
  
- require_once 'tpl/page.php'; // HTML-Template am besten in einer Sub-File anlegen+ $v = [ 
 + 'test_input' => save_and_load_option( 'test_input' ), 
 + ]; 
 + 
 + require_once 'tpl/admin-theme-links.php';
 } }
 +
 +/* Rudimentäre Saving & Lade Function */
 +function save_and_load_option( $key, $default = null ){
 + $value = get_option( $key, $default );
 +
 + if( isset( $_POST[$key] ) ){
 + $new_value = $_POST[$key];
 + $autoload  = false;
 + update_option( $key, $new_value, $autoload );
 + $value     = $new_value;
 + }
 +
 + return $new_value;
 +}
 +
 </code> </code>
  
Line 63: Line 152:
 <div class="wrap"> <div class="wrap">
 <h1>Your Plugin Page Title</h1> <h1>Your Plugin Page Title</h1>
-<form method="post" action="<?php echo $form_action; ?>?action=save">+<form method="post" action="<?php echo $form_action; ?>&action=save">
  
  <table class="form-table" role="presentation">  <table class="form-table" role="presentation">
Line 72: Line 161:
  </th>  </th>
  <td>  <td>
- <input name="" type="text" id="" value="" class="regular-text" />+ <input name="test_input" type="text" id="" value="<?php echo $v['test_input']; ?>" class="regular-text" />
  <p class="description" id="tagline-description">Beschreibung</p>  <p class="description" id="tagline-description">Beschreibung</p>
  </td>  </td>
  </tr>  </tr>
  
- <tr> 
- <th scope="row"> 
- <label for="">Label</label> 
- </th> 
- <td> 
- <input name="" type="text" id="" value="" class="regular-text" /> 
- <p class="description" id="tagline-description">Beschreibung</p> 
- </td> 
- </tr> 
  </tbody>  </tbody>
  </table>  </table>
Line 94: Line 174:
  
 </code> </code>
- 

Page Tools