WordPress.org

Ready to get started?Download WordPress

Forums

custom post type and custom meta box issues (1 post)

  1. smeyer012
    Member
    Posted 3 years ago #

    Hello,

    I am developing a site for a client that needs to go live next week and I've run into a serious problem that I don't know how to get around.

    I built the site on my local server. I have created a custom post type for news and a custom post type for testimonials. The testimonial post type utilizes custom meta boxes for the name, title, company, etc. Everything seemed to be working fine, but now that I have moved the site to our dev server for the client to review, the testimonial meta boxes don't seem to be saving (the fields are empty after I click publish). I see that I am creating posts in the database, but it isn't saving the meta box data. What's more concerning is that I am not seeing empty posts on the list page, even though after saving, it says "post published." I don't know why this would be; everything was working fine before. I am able to save news items, but the news post type doesn't use custom meta boxes.

    If that's not bad enough, I needed to work on a separate issue, so I moved a copy of the site back to my local server. Issue #1: I made the move, the site looked fine on the front end, but when I tried to log into the backend, the login screen was white. I tried renaming my plugin folder, but it didn't help. So I renamed my custom theme folder, and I was able to login. I changed the folder name back and reactivated the theme. Issue #2: I can't seem to save or delete posts now...if I go into a post/page/news/testimonial and click publish, I get a white screen. When you start to create the content within the editor on any one of these screens, where you would normally see "Last updated by..." or "Saving autodraft..." in the bottom right of the editor, a empty red box appears (as if it is trying to give an error message). I have tried disabling all plugins, which didn't help. It definitely seems to be a theme issue.

    Here is the code that I have for the post types in functions.php:

    //Create news custom post type
    add_action( 'init', 'create_my_post_types' );
    
    function create_my_post_types() {
    	register_post_type( 'News',
    		array(
    			'labels' => array(
    				'name' => __( 'News' ),
    				'singular_name' => __( 'News' ),
    				'add_new' => __( 'Add News Item' ),
    				'add_new_item' => __( 'Add News Item' ),
    				'edit_item' => __( 'Edit News Item' ),
                    'new_item' => __( 'Add News Item' ),
                    'view_item' => __( 'View News Item' ),
                    'search_items' => __( 'Search News' ),
                    'not_found' => __( 'No News Items found' ),
                    'not_found_in_trash' => __( 'No news items found in trash' )
    			),
    			'public' => true,
    			'rewrite' => array("slug" => "news"),
                'show_in_nav_menus' => false
    		)
    	);
    	register_post_type( 'Testimonials',
            array(
                'labels' => array(
                    'name' => __( 'Testimonials' ),
                    'singular_name' => __( 'Testimonial' ),
                    'add_new' => __( 'Add New Testimonial' ),
                    'add_new_item' => __( 'Add New Testimonial' ),
                    'edit_item' => __( 'Edit Testimonial' ),
                    'new_item' => __( 'Add New Testimonial' ),
                    'view_item' => __( 'View Testimonial' ),
                    'search_items' => __( 'Search Testimonial' ),
                    'not_found' => __( 'No Testimonials found' ),
                    'not_found_in_trash' => __( 'No testimonials found in trash' )
                ),
                'public' => true,
                'capability_type' => 'post',
                'supports' => array( 'thumbnail'),
                'rewrite' => array("slug" => "testimonials"), // Permalinks format
                'register_meta_box_cb' => 'add_testi_metaboxes',
                'show_in_nav_menus' => false,
                'exclude_from_search' => true
            )
        );
    }
    
    // Add the Testimonial Meta Boxes
    
    function add_testi_metaboxes() {
        add_meta_box('wpt_testi_info', 'Testimonial', 'wpt_testi_info', 'Testimonials', 'normal', 'high');
    }
    
    // The Testimonial Metabox
    
    function wpt_testi_info() {
        global $post;
    
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="testimeta_noncename" id="testimeta_noncename" value="' .
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    
        // Get the location data if its already been entered
        $testi_text = get_post_meta($post->ID, '_testi_text', true);
        $testi_company = get_post_meta($post->ID, '_testi_company', true);
        $testi_name = get_post_meta($post->ID, '_testi_name', true);
        $testi_title = get_post_meta($post->ID, '_testi_title', true);
    
        // Echo out the field
            echo '<p>Enter the testimonial: <strong>DO NOT USE QUOTATION MARKS</strong></p>';
        	echo '<textarea name="_testi_text" rows="8" class="widefat">' . $testi_text . '</textarea>';
            echo '<p>Enter the company that the person works for:</p>';
            echo '<input type="text" name="_testi_company" value="' . $testi_company  . '" class="widefat" />';
            echo '<p>Enter the name of the person:</p>';
            echo '<input type="text" name="_testi_name" value="' . $testi_name  . '" class="widefat" />';
            echo '<p>Enter the title of the person:</p>';
            echo '<input type="text" name="_testi_title" value="' . $testi_title  . '" class="widefat" />';
     		echo '<input type="hidden" name="post_title" value="' . $testi_name  . '" />';
    }
    
    // Save the Metabox Data
    
    function wpt_save_testi_meta($post_id, $post) {
    
        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if ( !wp_verify_nonce( $_POST['testimeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
        }
    
        // Is the user allowed to edit the post or page?
        if ( !current_user_can( 'edit_post', $post->ID ))
            return $post->ID;
    
        // OK, we're authenticated: we need to find and save the data
        // We'll put it into an array to make it easier to loop though.
    
        $testi_meta['_testi_text'] = $_POST['_testi_text'];
        $testi_meta['_testi_company'] = $_POST['_testi_company'];
        $testi_meta['_testi_name'] = $_POST['_testi_name'];
        $testi_meta['_testi_title'] = $_POST['_testi_title'];
    
        // Add values of $events_meta as custom fields
    
        foreach ($testi_meta as $key => $value) { // Cycle through the $testi_meta array!
            if( $post->post_type == 'revision' ) return; // Don't store custom data twice
            $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // If the custom field doesn't have a value
                add_post_meta($post->ID, $key, $value);
            }
            if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
        }
    
    }
    
    add_action('save_post', 'wpt_save_testi_meta', 1, 2); // save the custom fields

    The only thing that might be slightly different than what is expected is that I have a hidden field for the testimonial post that assigns the "name" to the post name...I did this because I am not using the usual title field, and it otherwise calls all of the posts "auto draft" in the testimonial post list page. Like I said before, all of this worked fine on the local server when I was building it initially.

    I am mostly concerned that the testimonials are not saving, but I also need a working test environment since I have more work to do on the site. I am also extremely nervous about moving the site to the client's server. I would be absolutely thrilled to send anyone any requested files if they can please please help.

Topic Closed

This topic has been closed to new replies.

About this Topic