4

I am trying to enable revisions for an existing custom post type. As the post type was already created about 2 years ago so where the post type was register I added revisions to the supports array.

Earlier my code was like:

   $labels = array(
    'name' => 'Products',
    'singular_name' => 'Product',
    'all_items' => 'All Products',      
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Product',
    'edit_item' => 'Edit Products',
    'new_item' => 'New Product',
    'view_item' => 'View Product',
    'search_items' => 'Search Products',
    'not_found' =>  'No Prducsts Found',
    'not_found_in_trash' => 'No Products in Trash', 
    'parent_item_colon' => ''
  );
  $supports = array(
    'title',
    'custom-fields',
    'editor',
    'thumbnail'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'menu_position' => 5,
    'rewrite' => array( 'slug' => 'products' ),
    'has_archive' => true,
    'supports' => $supports 
  ); 
  register_post_type('products', $args);

And now it looks like:

   $labels = array(
    'name' => 'Products',
    'singular_name' => 'Product',
    'all_items' => 'All Products',      
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Product',
    'edit_item' => 'Edit Products',
    'new_item' => 'New Product',
    'view_item' => 'View Product',
    'search_items' => 'Search Products',
    'not_found' =>  'No Prducsts Found',
    'not_found_in_trash' => 'No Products in Trash', 
    'parent_item_colon' => ''
  );
  $supports = array(
    'title',
    'custom-fields',
    'editor',
    'thumbnail',
    'revisions'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'menu_position' => 5,
    'rewrite' => array( 'slug' => 'products' ),
    'has_archive' => true,
    'supports' => $supports 
  ); 
  register_post_type('products', $args);

This code didn't enable revisions in the custom post type.

Then I found another code which I added to my theme's function.php:

function add_revisions_custom_post()
{
    add_post_type_support( 'products', 'revisions' );
}
add_action('init','add_revisions_custom_post');

This one is also not working.

Can anyone suggest me how I can enable revisions?

Thanks in advance.

EDIT: Here is my wp-config.php file.

<?php
/**
 * The base configurations of the WordPress.
 *
 * This file has the following configurations: MySQL settings, Table Prefix,
 * Secret Keys, WordPress Language, and ABSPATH. You can find more information
 * by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
 * wp-config.php} Codex page. You can get the MySQL settings from your web host.
 *
 * This file is used by the wp-config.php creation script during the
 * installation. You don't have to use the web site, you can just copy this file
 * to "wp-config.php" and fill in the values.
 *
 * @package WordPress
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('WP_MEMORY_LIMIT', '64MB');

define('DB_NAME', 'MYDBNAME');

/** MySQL database username */
define('DB_USER', 'MYDBUSER');

/** MySQL database password */
define('DB_PASSWORD', 'MYDBPASSWORD');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

define('WP_MEMORY_LIMIT', '64MB');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'c7ypbekexsfbt4wiucppwewene93cbtcfhf6xgpggepycabhildsoyqpre5iv3wi');
define('SECURE_AUTH_KEY',  'ujjxufvylpndsj0qeuwa90gxawj3cgaqnyusrdbmujmb08r37pnyreorpcyxqouu');
define('LOGGED_IN_KEY',    'vwlhlmenthgrq9g5jcocihz4ndldhrpegmcp6qyb3rfmjvxjejbacv1zharaexlp');
define('NONCE_KEY',        'qhnyqgmqckh3ylasveugagqlvifiuqajl6s9e7ulfrxepdxh2mewr8qhdinua8o2');
define('AUTH_SALT',        'rlb723gcatjvkfrd3jscmvdjio3kx9apm5yie9e4ibxktnnlukvgfpgbdsohrns9');
define('SECURE_AUTH_SALT', 'pllenyye8zbtml91hekptc2clqr7bhvhlriecz5qozexfhiqptmcvxrlehj44c16');
define('LOGGED_IN_SALT',   '9lyqc6qod0zdgyh6esp7bsxmpmuyp3h64m62pcnwxyefejh6tjykm7tpxhecg3xy');
define('NONCE_SALT',       'ob0bq3fye46rubbgu5flycjuai4ygxqgxho8bb1k8t81mwhghcbkysrgxrjzx0fu');

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * WordPress Localized Language, defaults to English.
 *
 * Change this to localize WordPress.  A corresponding MO file for the chosen
 * language must be installed to wp-content/languages. For example, install
 * de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
 * language support.
 */
define ('WPLANG', '');

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 */
define('WP_DEBUG', false);

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
2
  • 1
    Don't worry, you are not alone with this. The revision on your site will work for NEW posts on that post type. It wont work for EXISTING posts in that post type, no matter how many edits you make to it. Why this is the case, I am still trying to figure out myself and will let you know once I do.
    – user75665
    Commented Jun 17, 2021 at 6:43
  • 1
    OK - plain simple. Please try to save the SAME post at least TWICE and each time make sure to actually EDIT something in the post. That will then trigger the revision. It is expected, because if you enable revision on a CPT that has no revisions yet, editing the post the FIRST time after enabling revision has NO revision to fallback to, thus, nothing can be shown. Only the SECOND time you edit it, it will have a revision to fallback to, and thus, will display it. I am almost 100% sure this is the issue you faced here.
    – user75665
    Commented Jun 17, 2021 at 6:51

2 Answers 2

0
$cap_type = 'post';
$plural = 'Products';
$single = 'Product';
$cpt_name = 'products';

$opts['supports'] = array( 'products', 'title', 'editor', 'thumbnail', 'author', 'revisions' );
$opts['can_export'] = TRUE;
$opts['capability_type'] = $cap_type;
$opts['description'] = '';
$opts['exclude_from_search'] = FALSE;
$opts['has_archive'] = TRUE;
$opts['hierarchical'] = TRUE;
$opts['map_meta_cap'] = TRUE;
$opts['menu_icon'] = 'dashicons-products';
$opts['menu_position'] = 2;
$opts['public'] = TRUE;
$opts['publicly_queryable'] = TRUE;
$opts['query_var'] = TRUE;
$opts['register_meta_box_cb'] = '';
$opts['rewrite'] = array('slug'=>'products', 'with_front'=>false);
$opts['show_in_admin_bar'] = TRUE;
$opts['show_in_menu'] = TRUE;
$opts['show_in_nav_menu'] = TRUE;

$opts['labels']['add_new'] = esc_html__( "Add New", 'wisdom' );
$opts['labels']['add_new_item'] = esc_html__( "Add New {$single}", 'wisdom' );
$opts['labels']['all_items'] = esc_html__( "All {$plural}", 'wisdom' );
$opts['labels']['edit_item'] = esc_html__( "Edit {$single}" , 'wisdom' );
$opts['labels']['menu_name'] = esc_html__( $plural, 'wisdom' );
$opts['labels']['name'] = esc_html__( $plural, 'wisdom' );
$opts['labels']['name_admin_bar'] = esc_html__( $single, 'wisdom' );
$opts['labels']['new_item'] = esc_html__( "New {$single}", 'wisdom' );
$opts['labels']['not_found'] = esc_html__( "No {$single} found", 'wisdom');
$opts['labels']['not_found_in_trash'] = esc_html__( "Not found {$plural} in Trash", 'wisdom' );
$opts['labels']['parent_item_colon'] = esc_html__( "Parent {$plural} :", 'wisdom' );
$opts['labels']['search_items'] = esc_html__( "Search {$plural}", 'wisdom' );
$opts['labels']['singular_name'] = esc_html__( $single, 'wisdom' );
$opts['labels']['view_item'] = esc_html__( "View {$single}", 'wisdom' );
register_post_type( strtolower( $cpt_name ), $opts );

try this

12
  • 1
    :( It's not working..
    – Thomas
    Commented Sep 29, 2018 at 7:48
  • are you sure you don't have revisions turned off? be sure this does not exist in wp-config.php: define('WP_POST_REVISIONS', false); Commented Sep 29, 2018 at 8:01
  • 1
    Have you check on a page/post edit panel? click on "Screen Options" in the top right and look for a "Revisions" checkbox and make sure it is active Commented Sep 29, 2018 at 8:07
  • 1
    Yes, I checked that too. In screen options, there is no option to show revisions. I just want to confirm one thing that if I added the revisions to the supports array of custom post type then will it affect the data which is already added to the posts?
    – Thomas
    Commented Sep 29, 2018 at 8:13
  • 1
    I am not using define('WP_POST_REVISIONS', false); in my config file but still i added define('WP_POST_REVISIONS', true); to my file. Still not working :(
    – Thomas
    Commented Sep 29, 2018 at 8:47
0

First, make sure the basics are all in place:

  • register_post_type includes 'revisions' in the supports args array
  • WP_POST_REVISIONS is either not defined or set to true in wp-config.php
  • open Screen Options and make sure "Revisions" is checked. (Though I think this only controls the big meta box, and the small "revisions" link should appear below "visibility" no matter how this is set)

Next, make sure you actually have two versions of the post you're trying to test.

  • Create a new test post of your CPT with any content
  • Make a change/edit to the content and Update (don't just press Update - make sure to actually change something)

You should now see revisions available and be able to revert to old versions.

1
  • h/t @BedaSchmid and OP for working most of this out in the comments already. Just wanted to put a clear summary here in the form of an answer. Commented Nov 24, 2021 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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