WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Cant edit existing Custom Post Type

[Resolved] Cant edit existing Custom Post Type

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Rachel Carden

    @bamadesigner

    Somtimes javascript issues can cause these problems. I, myself, had difficulty with it the other day because my browser was acting up and messing with all my javascript.

    If the problem persists, you can remove the script with the following code:

    add_action( 'admin_head', 'my_website_admin_head' );
    function my_website_admin_head() {
       wp_dequeue_script( 'custom-post-type-onomies-admin-options-validate' );
    }

    I’ve created a small plugin that you are free to use that validates fields on a custom post type, replaces the standard admin notice with a custom admin notice, no javascript required

    add_action('save_post', 'album_save_post', 10, 2);
    
    function album_save_post( $album_id, $album ) {
    
    	if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $album->post_type != 'music_album') return;
    
    	// echo '<pre>';
    	// print_r($album);
    	// echo '</pre>';
    	// die();
    
    	$errors = array();
    
    	// Validation filters
    	$title = $album->post_title;
    	if ( ! $title ) {
    		$errors['title'] = "The title is required";
    	}
    
    	// if we have errors lets setup some messages
    	if (! empty($errors)) {
    
    		// we must remove this action or it will loop for ever
    		remove_action('save_post', 'album_save_post');
    
    		// save the errors as option
    		update_option('album_errors', $errors);
    
    		// Change post from published to draft
    		$album->post_status = 'draft';
    
    		// update the post
    		wp_update_post( $album );
    
    		// we must add back this action
    		add_action('save_post', 'album_save_post');
    
    		// admin_notice is create by a $_GET['message'] with a number that wordpress uses to
    		// display the admin message so we will add a filter for replacing default admin message with a redirect
    		add_filter( 'redirect_post_location', 'album_post_redirect_filter' );
    	}
    }
    
    function album_post_redirect_filter( $location ) {
    	// remove $_GET['message']
    	$location = remove_query_arg( 'message', $location );
    
    	// add our new query sting
    	$location = add_query_arg( 'album', 'error', $location );
    
    	// return the location query string
    	return $location;
    }
    
    // Add new admin message
    add_action( 'admin_notices', 'album_post_error_admin_message' );
    
    function album_post_error_admin_message() {
    	if ( isset( $_GET['album'] ) && $_GET['album'] == 'error' ) {
    		// lets get the errors from the option album_errors
    		$errors = get_option('album_errors');
    
    		// now delete the option album errors
    		delete_option('album_errors');
    
    		$display = '<div id="notice" class="error"><ul>';
    
    		// Because we are storing as an array we should loop through them
    		foreach ( $errors as $error ) {
    			$display .= '<li>' . $error . '</li>';
    		}
    
    		$display .= '</ul></div>';
    
    		// finally echo out our display
    		echo $display;
    
    		// add some jQuery
    		?>
    		<script>
    		jQuery(function($) {
    			$("#title").css({"border": "1px solid red"})
    		});
    		</script>
    		<?php
    	}
    }

    full tutorial here:
    http://kevinphillips.co.nz/news/custom-post-type-validation-without-javascript/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Cant edit existing Custom Post Type’ is closed to new replies.