Support » Developing with WordPress » Custom Field Text/Data not saved

  • worels

    (@worels)



    Hi
    I’m trying to build a custom page type for articles (using code snippet).

    I’m testing my code skills and I managed to get the post type, so I tried to ad a custom field to see if it works …
    I can define the field (textarea) but when I test it, the field doesn’t store data/text typed in the field. On every store/refresh- action the field is empty ..
    I can’t figure out, why this happens.
    (I did nearly the same with my cpt workshop – pages and there it works …)
    Maybe one of you could be so nice to look over my code and tell me, what I’m doin’ wrong …
    Tnx in advance …

    
    // BENUTZERDEFINIERTE  Fachartikel PSy    S E I T E N ------------------------------------------------------ //
    /* Dazugehörige single-fachartikel_psy.php (to do: archiv-fachartikel_psy.php) siehe register_post_type unten*/
    
    add_action( 'init', 'mypsyui_custom_content_type' );
    
    function mypsyui_custom_content_type() {
    
    	$labels = array(
    		'name'               => 'Fachartikel Psychotherapie - Psychologie',
    		'singular_name'      => 'Fachartikel Psychotherapie - Psychologie',
    		'menu_name'          => 'Fachartikel Psyche',
    	  
    	  
    		"all_items" => __( "Alle Psychologisch-Psychotherapeutischen Fachartikel", "" ),
    		"add_new" => __( "Erstellen", "" ),
    		"add_new_item" => __( "Neuen Fachartikel erstellen", "" ),
    		"edit_item" => __( "Fachartikel bearbeiten", "" ),
    		"new_item" => __( "Neuer Fachartikel", "" ),
    		"view_item" => __( "Fachartikel anzeigen", "" ),
    		"view_items" => __( "Fachartikel anzeigen", "" ),
    		"search_items" => __( "Fachartikel suchen", "" ),
    		"not_found" => __( "Keine Fachartikel gefunden", "" ),
    		"not_found_in_trash" => __( "Keine Fachartikel im Papierkorb gefunden", "" ),
    		"parent_item_colon" => __( "Übergeordnetes Fachthema", "" ),
    		"featured_image" => __( "Beitragsbild für diesen Fachartikel", "" ),
    		"set_featured_image" => __( "Lege das Beitragsbild für diesen Fachartikel fest", "" ),
    		"remove_featured_image" => __( "Entfernen des Beitragsbildes für diesen Fachartikel", "" ),
    		"use_featured_image" => __( "Verwende ein Beitragsbild für diesen Fachartikel", "" ),
    		"archives" => __( "Fachartikel-Archiv", "" ),
    		"insert_into_item" => __( "In Fachartikel einfügen", "" ),
    		"uploaded_to_this_item" => __( "Zu diesem Artikel hochladen", "" ),
    		"filter_items_list" => __( "Fachartikel Liste filtern", "" ),
    		"items_list_navigation" => __( "Fachartikel Listen - Navigation", "" ),
    		"items_list" => __( "Fachartikel-Liste", "" ),
    		"attributes" => __( "Fachartikel Attribute", "" ),
    		"parent_item_colon" => __( "Übergeordnetes Fachthema", "" ),
    	);
    
    	$args = array(
    		'label' => __( "Fachartikel Psychotherapie - Psychologie", "" ),
    		'labels' => $labels,
    		'description' => "Fachartikel im Bereich Psychotherape, Psychologie, sowie verwandte Bereiche wie z.b. Pädagogik",
    		'public' => true,
    	  	'exclude_from_search' => false,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    	  	'show_in_nav_menus'   => true,
    	 	'show_in_menu' => true,
    	  	'show_in_admin_bar'   => true,
    	 	'menu_position'       => 5,
    	  	'capability_type' => 'post',
    	   	'hierarchical'        => false,
    		'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'author', 'page-attributes', 'revisions' ),
    		'has_archive' => true,
    		'rewrite'  => array( 'slug' => 'fachartikel_psy','with_front' => true ),
    		'query_var'=> true,	
    		'map_meta_cap' => true,
    
    	);
    
    	register_post_type( "fachartikel_psy", $args );
    }
     // flush_rewrite_rules();
    
     add_action('admin_init','cpt_mypsyui_meta_boxen');
     add_action('save_post','cpt_mypsyui_daten_speichern');
    
     function cpt_mypsyui_meta_boxen(){
       
    add_meta_box("mytest-meta", "mytest-Info","cpt_mypsyui_feld_mytest", "fachartikel_psy","normal","default");
     }
    
     function cpt_mypsyui_feld_mytest(){
    	global $post;
    	$custom = get_post_custom($post->ID);
    	$mytest = $customm["mytest"][0];
    	echo '<textarea name="mytest" style="width:60em; height:20em;">' . $mytest .'</textarea>';
     }
    
     function cpt_mypsyui_daten_speichern (){
    	global $post;
    	
    	update_post_meta($post->ID, "mytest", $_POST["mytest"]);
     }
     
    
     // Anzeige der Felder im Backend
    
    add_filter("manage_edit-mypsyui_columns", 
       "cpt_mypsyui_spalten");
       add_action("manage_mypsyui_posts_custom_column",  
       "cpt_mypsyui_neue_spalte");
    
       function cpt_mypsyui_spalten($columns){
          $columns = array(
                     "cb" => "<input type=\"checkbox\" />",
                     "title" => "Artikeltitel",
    		        "datum" => "Datum",
                     "date" => "Hinzugefügt"
          );
    	
          return $columns;
       }
    
       function cpt_mypsyui_neue_spalte($column){
          global $post;
    		if ("datum" == $column) {
             $custom = get_post_custom();
             echo $custom["datum"][0];
          }
    
       }
    //----------Ende Felderanzeige im Backend-----------------------
    
    // ENDE --------------- BENUTZERDEFINIERTE Fachartikel Psy    S E I T E N ---------------------------------- //
    

    Brgds Worels

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hi @worels

    I’m sorry that you got into this problem.
    I can see that you have registered post type correctly. You can also find an example at the bottom of the page on the link below.
    https://codex.wordpress.org/Function_Reference/register_post_type.

    I think that you have registered the meta boxes with the wrong hook and the cpt_mypsyui_meta_boxen function declaration looks fine.
    add_action('admin_init','cpt_mypsyui_meta_boxen');.
    Try using it with
    add_action('add_meta_boxes','cpt_mypsyui_meta_boxen');

    And the save_post hook must have a parameter in a function like cpt_mypsyui_daten_speichern($post_id) without the declaration of global $post inside the function.

    Try using $custom = get_post_meta($post->ID, 'mytest', true); to get the post’s meta value.
    Find the complete example of registering metabox and saving it in the link here.
    All the functions declaration that you have used could be also right but I would suggest you use it with WordPress standards.

    Hope this works. I would be delighted to hear back from you.

    Thank you. Best Regards.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.