WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Cant edit existing Custom Post Type (3 posts)

  1. jlapitan
    Member
    Posted 1 year ago #

    I was trying to edit or add functionality to existing Custom Post Type but it says "That post type name already exists. Please choose another name."

    I was adding an option to put a featured image for the post type

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    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' );
    }
  3. dollar_dad
    Member
    Posted 1 year ago #

    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/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.