2016-05-22 3 views
-1

Ich habe eine Metabox innerhalb einer Klasse erstellt. Ich brauchte es für einen benutzerdefinierten Post-Typ, den ich hatte. Die Metabox wird angezeigt, aber die Werte können nicht gespeichert werden. Ich habe den Code mehrmals geändert, aber es funktioniert immer noch nicht für mich. Weißt du, was ich falsch mache? Hier ist der vollständige Code:Metabox-Daten werden nicht gespeichert

<?php 
class portfolio_metabox { 

    public function __construct() { 
     if (is_admin()) { 
      add_action('load-post.php', array($this, 'init_metabox')); 
      add_action('load-post-new.php', array($this, 'init_metabox')); 
     } 
    } 

    public function init_metabox() { 
     add_action('add_meta_boxes', array($this, 'add_metabox')); 
     add_action('save_post', array($this, 'save_metabox'), 10, 2); 

    } 

    public function add_metabox() { 
     add_meta_box(
      'details', 
      __('Project Details', 'chic'), 
      array($this, 'render_project_metabox'), 
      'portfolio', 
      'advanced', 
      'default' 
     ); 
    } 

    public function render_project_metabox($post) { 
     // Retrieve an existing value from the database. 
     $portfolio_web_design = get_post_meta($post->ID, 'portfolio_web_design ', true); 
     $portfolio_web_dev = get_post_meta($post->ID, 'portfolio_web_dev ', true); 
     $portfolio_digital_art = get_post_meta($post->ID, 'portfolio_digital_art ', true); 
     $portfolio_graphic_design = get_post_meta($post->ID, 'portfolio_graphic_design ', true); 

     // Set default values. 

     // Form fields. 
     echo '<table class="form-table">'; 

     echo '<tr>'; 
     echo '<th><label for="portfolio_project" class="portfolio_project_label">' . __('Project Type', 'chic') . '</label></th>'; 
     echo '<td>'; 
     echo '<label><input type="checkbox" name="portfolio_web_design " class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked($portfolio_web_design , 'checked', false) . '> ' . __(' Web Design', 'chic') . '</label><br>'; 
     echo '<label><input type="checkbox" name="portfolio_web_dev " class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked($portfolio_web_dev , 'checked', false) . '> ' . __(' Web Development', 'chic') . '</label><br>'; 
     echo '<label><input type="checkbox" name="portfolio_digital_art " class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked($portfolio_digital_art , 'checked', false) . '> ' . __(' Digital Art', 'chic') . '</label><br>'; 
     echo '<label><input type="checkbox" name="portfolio_graphic_design " class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked($portfolio_graphic_design , 'checked', false) . '> ' . __(' Graphic Design', 'chic') . '</label><br>'; 
     echo '<p class="description">' . __('Project Type', 'chic') . '</p>'; 
     echo '</td>'; 
     echo '</tr>'; 
     echo '</table>'; 

    } 

    public function save_metabox($post_id, $post) { 

     // Sanitize user input. 
     $portfolio_new_web_design = isset($_POST[ 'portfolio_web_design' ]) ? 'checked' : ''; 
     $portfolio_new_web_dev = isset($_POST[ 'portfolio_web_dev' ]) ? 'checked' : ''; 
     $portfolio_new_digital_art = isset($_POST[ 'portfolio_digital_art' ]) ? 'checked' : ''; 
     $portfolio_new_digital_art = isset($_POST[ 'portfolio_graphic_design' ]) ? 'checked' : ''; 

     // Update the meta field in the database. 
     update_post_meta($post_id, 'portfolio_web_design ', $portfolio_new_web_design ); 
     update_post_meta($post_id, 'portfolio_web_dev ', $portfolio_new_web_dev ); 
     update_post_meta($post_id, 'portfolio_digital_art ', $portfolio_new_digital_art ); 
     update_post_meta($post_id, 'portfolio_graphic_design ', $portfolio_new_digital_art ); 

    } 

} 

new portfolio_metabox; 

?> 

UPDATE:

Der Code gegeben durch Mukesh Panchal hat super funktioniert. Es speichert, wie ich es wollte, aber ich kann immer noch nicht die Metadaten auf der Vorlage anzeigen. Irgendwelche Ideen, warum das so ist?

Antwort

0

Können Sie bitte unten aktualisierten Code versuchen:

<?php 
class portfolio_metabox { 

    public function __construct() { 
     if (is_admin()) { 
      add_action('load-post.php',  array($this, 'init_metabox')); 
      add_action('load-post-new.php', array($this, 'init_metabox')); 
     } 
    } 
    public function init_metabox() { 
     add_action('add_meta_boxes',  array($this, 'add_metabox')  ); 
     add_action('save_post',    array($this, 'save_metabox'), 10, 2); 
    } 

    public function add_metabox() { 
     add_meta_box(
      'details', 
      __('Project Details', 'chic'), 
      array($this, 'render_project_metabox'), 
      'portfolio', 
      'advanced', 
      'default' 
     ); 
    } 

    public function render_project_metabox($post) { 

     // Retrieve an existing value from the database. 
     $portfolio_web_design = get_post_meta($post->ID, 'portfolio_web_design ', true); 
     $portfolio_web_dev = get_post_meta($post->ID, 'portfolio_web_dev ', true); 
     $portfolio_digital_art = get_post_meta($post->ID, 'portfolio_digital_art ', true); 
     $portfolio_graphic_design = get_post_meta($post->ID, 'portfolio_graphic_design ', true); 

     // Set default values. 
     // Form fields. 
     echo '<table class="form-table">'; 
     echo ' <tr>'; 
     echo '  <th><label for="portfolio_project" class="portfolio_project_label">' . __('Project Type', 'chic') . '</label></th>'; 
     echo '  <td>'; 
     echo '   <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked($portfolio_web_design , 'checked', false) . '> ' . __(' Web Design', 'chic') . '</label><br>'; 
     echo '   <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked($portfolio_web_dev , 'checked', false) . '> ' . __(' Web Development', 'chic') . '</label><br>'; 
     echo '   <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked($portfolio_digital_art , 'checked', false) . '> ' . __(' Digital Art', 'chic') . '</label><br>'; 
     echo '   <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked($portfolio_graphic_design , 'checked', false) . '> ' . __(' Graphic Design', 'chic') . '</label><br>'; 
     echo '   <p class="description">' . __('Project Type', 'chic') . '</p>'; 
     echo '  </td>'; 
     echo ' </tr>'; 
     echo '</table>'; 

    } 

    public function save_metabox($post_id, $post) { 

     // Sanitize user input. 
     $portfolio_new_web_design = isset($_POST[ 'portfolio_web_design' ]) ? 'checked' : ''; 
     $portfolio_new_web_dev = isset($_POST[ 'portfolio_web_dev' ]) ? 'checked' : ''; 
     $portfolio_new_digital_art = isset($_POST[ 'portfolio_digital_art' ]) ? 'checked' : ''; 
     $portfolio_new_digital_art = isset($_POST[ 'portfolio_graphic_design' ]) ? 'checked' : ''; 

     // Update the meta field in the database. 
     update_post_meta($post_id, 'portfolio_web_design', $portfolio_new_web_design ); 
     update_post_meta($post_id, 'portfolio_web_dev', $portfolio_new_web_dev ); 
     update_post_meta($post_id, 'portfolio_digital_art', $portfolio_new_digital_art ); 
     update_post_meta($post_id, 'portfolio_graphic_design', $portfolio_new_digital_art ); 
    } 
} 
new portfolio_metabox; 
?> 
1

Es gibt einen zusätzlichen Platz in Ihrem Eingangs name = "" Attribute, wie hier:

name="portfolio_web_design " 

Dies sollte:

name="portfolio_web_design" 

Danach Ihre isset sollte einwandfrei funktionieren;)

0

Dies sollte es tun:

class portfolio_metabox { 

    public function __construct() { 

     if (is_admin()) { 
      add_action('load-post.php', array($this, 'init_metabox')); 
      add_action('load-post-new.php', array($this, 'init_metabox')); 
     } 

    } 

    public function init_metabox() { 

     add_action('add_meta_boxes', array($this, 'add_metabox')); 
     add_action('save_post', array($this, 'save_metabox'), 10, 2); 

    } 

    public function add_metabox() { 

     add_meta_box(
      'details', 
      __('Project Details', 'chic'), 
      array($this, 'render_project_metabox'), 
      'portfolio', 
      'advanced', 
      'default' 
     ); 

    } 

    public function render_project_metabox($post) { 
     // Retrieve an existing value from the database. 
     $post_meta = get_post_meta($post->ID); 

       $portfolio_web_design = (isset($post_meta['portfolio_web_design'][0]) && $post_meta['portfolio_web_design'][0] != '') ? $post_meta['portfolio_web_design'][0] : 0; 
       $portfolio_web_dev = (isset($post_meta['portfolio_web_dev'][0]) && $post_meta['portfolio_web_dev'][0]!= '') ? $post_meta['portfolio_web_dev'][0] : 0; 
       $portfolio_digital_art = (isset($post_meta['portfolio_digital_art'][0]) && $post_meta['portfolio_digital_art'][0]!= '') ? $post_meta['portfolio_digital_art'][0] : 0; 
       $portfolio_graphic_design = (isset($post_meta['portfolio_graphic_design'][0]) && $post_meta['portfolio_graphic_design'][0]!= '') ? $post_meta['portfolio_graphic_design'][0] : 0; 
     // Set default values. 

     // Form fields. 
     ?> 
     <table class="form-table"> 
      <tr> 
       <th><label for="portfolio_project" class="portfolio_project_label"><?php esc_html_e('Project Type', 'chic') ?></label;></th> 
       <td> 
        <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="1" <?php checked($portfolio_web_design , 1) ?>> <?php esc_html_e(' Web Design', 'chic'); ?></label><br> 
        <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="1" <?php checked($portfolio_web_dev , 1) ?>> <?php esc_html_e(' Web Development', 'chic'); ?></label><br> 
        <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="1" <?php checked($portfolio_digital_art , 1) ?>> <?php esc_html_e(' Digital Art', 'chic'); ?></label><br> 
        <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="1" <?php checked($portfolio_graphic_design , 1) ?>> <?php esc_html_e(' Graphic Design', 'chic'); ?></label><br> 
        <p class="description"><?php esc_html_e('Project Type', 'chic') ?></p;> 
       </td> 
      </tr> 
     </table> 
     <?php 
    } 

    public function save_metabox() { 
     global $post; 
     // Sanitize user input. 
     $portfolio_new_web_design = (isset($_POST['portfolio_web_design']) && $_POST['portfolio_web_design'] == 1) ? 1 : 0; 
     $portfolio_new_web_dev = (isset($_POST['portfolio_web_dev']) && $_POST['portfolio_web_dev'] == 1) ? 1 : 0; 
     $portfolio_new_digital_art = (isset($_POST['portfolio_digital_art']) && $_POST['portfolio_digital_art'] == 1) ? 1 : 0; 
     $portfolio_new_digital_art = (isset($_POST['portfolio_graphic_design']) && $_POST['portfolio_graphic_design'] == 1) ? 1 : 0; 

     // Update the meta field in the database. 
     update_post_meta($post->ID, 'portfolio_web_design', $portfolio_new_web_design); 
     update_post_meta($post->ID, 'portfolio_web_dev', $portfolio_new_web_dev); 
     update_post_meta($post->ID, 'portfolio_digital_art', $portfolio_new_digital_art); 
     update_post_meta($post->ID, 'portfolio_graphic_design', $portfolio_new_digital_art); 

    } 

} 

new portfolio_metabox; 

ich Ihren Code ein bisschen aufgeräumt. Versuchen Sie nicht, nach allem Platz zu machen, es ist nicht notwendig. Das Problem lag in der Art und Weise, wie Sie das Post-Meta herausziehen. Die Variablen wie $portfolio_web_design waren leer.

Ich habe das getestet und es hat funktioniert.

this helps :)

+0

"Notice: Undefined index: portfolio_web_design in /Applications/MAMP/htdocs/wordpress/wp-content/themes/chic/portfolio_meta.php on line 37" – Jubiki

+0

Er sagt, dass für alle von ihnen. – Jubiki

+0

Ich habe den Code ein wenig bearbeitet (hinzugefügt einen Haken), sollte jetzt funktionieren. –

Verwandte Themen