0

I'm a little new to WordPress development and am SO frustrated-- I created some custom post types, and am trying to add custom fields to them. For some reason, the fields will just not show up when I try to add a new post!!! Here is my code--this is in the custom post type file.

<?php
/**
 * Plugin Name: Art Beat
 * Plugin URI: #
 * Version: 1.0
 * Author: bjorkland
 * Author URI: http://localhost:8888/wp-admin/plugins.php?plugin_status=all&paged=1&s
 * Description: A custom post type
 * License: GPL2
 */

class ArtBeat {

function __construct() {
  add_action( 'init', array( $this, 'register_custom_post_type' ) );
}
function register_custom_post_type() {
register_post_type( 'artbeat', array(
    'labels' => array(
        'name'               => _x( 'Art Beats', 'post type general name', 'artbeat' ),
        'singular_name'      => _x( 'Art Beat', 'post type singular name', 'artbeat' ),
        'menu_name'          => _x( 'Art Beat', 'admin menu', 'artbeat' ),
        'name_admin_bar'     => _x( 'Art Beat', 'add new on admin bar', 'artbeat' ),
        'add_new'            => _x( 'Add New Art Beat', 'art', 'artbeat' ),
        'add_new_item'       => __( 'Add New Art Beat', 'artbeat' ),
        'new_item'           => __( 'New Art Beat', 'artbeat' ),
        'edit_item'          => __( 'Edit Art Beat', 'artbeat' ),
        'view_item'          => __( 'View Art Beat', 'artbeat' ),
        'all_items'          => __( 'All Art Beats', 'artbeat' ),
        'search_items'       => __( 'Search Art Beats', 'artbeat' ),
        'parent_item_colon'  => __( 'Parent Art Beat:', 'artbeat' ),
        'not_found'          => __( 'No Art Beat found.', 'artbeat' ),
        'not_found_in_trash' => __( 'No Art Beat found in Trash.', 'artbeat' ),
    ),

    // Frontend
    'has_archive'        => false,
    'public'             => false,
    'publicly_queryable' => false,

    // Admin
    'capability_type' => 'post',
    'menu_icon'     => 'dashicons-businessman',
    'menu_position' => 10,
    'query_var'     => true,
    'show_in_menu'  => true,
    'show_ui'       => true,
    'supports'      => array(
        'title',
        'author',
        'comments',
    ),
) );
}
}

$ArtBeat = new ArtBeat;


function add_artbeat_meta_boxes() {
 add_meta_box("artbeat_contact_meta", "Contact Details", "add_contact_details_artbeat_meta_box", "artbeats", "normal", "low");
}
   function add_contact_details_artbeat_meta_box()
  {
global $post;
$custom = get_post_custom( $post->ID );

?>
 <style>.width99 {width:99%;}</style>
 <p>
<label>Date:</label><br />
  <textarea rows="5" name="date" class="width99"><?= @$custom["date"][0] ?></textarea>
   </p>
   <p>
     <label>Museum:</label><br />
     <input type="text" name="museum" value="<?= @$custom["museum"][0] ?>"    class="width99" />
  </p>
  <?php
}

function save_artbeat_custom_fields(){
 global $post;

if ( $post )
{
  update_post_meta($post->ID, "date", @$_POST["date"]);
  update_post_meta($post->ID, "museum", @$_POST["museum"]);
}
}
add_action( 'admin_init', 'add_artbeat_meta_boxes' );
add_action( 'save_post', 'save_artbeat_custom_fields' );
  • You should not use @ error suppression. Instead check if the data is available with if ( isset() ) or if ( empty() ) and display a default (or nothing) if it is not present. Also make sure that you use the proper esc_*() functions to escape possible unsafe data like for e.g. $_POST data before your save it to the database. Else you might find yourself getting hacked pretty quick. – kaiser Jul 31 '16 at 19:44
1

If you have created an Class in your plugin then you should use the action hook for admin_init in class only just like you have used "init"

function __construct() {
  add_action( 'init', array( $this, 'register_custom_post_type' ) );
  add_action( 'admin_init', array( $this, 'add_artbeat_meta_boxes' ) );
  add_action( 'save_post', array( $this, 'save_artbeat_custom_fields' ) );
}

Hope this will help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.