$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>actions - Create post revision on slug change|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

actions - Create post revision on slug change

matteradmin10PV0评论

I'd like to be able to track changes in the slug / post_name field of the post.

I've tried using wp_save_post_revision(...); if I detect a slug change, but according to this page wordpress only tracks specific fields, post_name not being one of them.

Am I SOL or is there a way to accomplish this?

I'd like to be able to track changes in the slug / post_name field of the post.

I've tried using wp_save_post_revision(...); if I detect a slug change, but according to this page wordpress only tracks specific fields, post_name not being one of them.

Am I SOL or is there a way to accomplish this?

Share Improve this question asked Feb 4, 2019 at 19:24 Tyler SebastianTyler Sebastian 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

If you're comfortable with PHP, it would be possible to keep track of when the slug is updated. The only way I can think of is a bit complicated:

  1. Make sure revisions are enabled, both for your install and for the post type you're targeting. You'll have to determine how many revisions you want to save, which will be a balance between how many times you think someone might change the post (whether it's a slug change, or content, or anything about the post) and how bloated you allow the database to become with all that extra data.

  2. Add version control for a postmeta field you will use to keep track of the slug with every revision. First install the plugin "WP-Post-Meta-Revisions." Then, in a custom plugin, or a child theme's functions.php file:

add_filter('wp_post_revision_meta_keys', 'wpse_track_slug_postmeta');

function wpse_track_slug_postmeta() {
    // Access the array of postmeta keys that is already versioned
    global $versionedKeys;

    // Add your custom postmeta key to the array
    $versionedKeys[] = 'versionedSlug';

    // Pass the array back to WordPress
    return $versionedKeys;
}

Once this is done, every time you save a revision, the postmeta "versionedSlug" for that specific revision will be saved.

  1. Every time a revision is saved, you need to tell WP to save the slug as that "versionedSlug" postmeta. You can use the wp_insert_post hook:
// Every time WP inserts a post, run this function
add_action('wp_insert_post', 'wpse_save_slug_postmeta');

function wpse_save_slug_postmeta($post_id, $post, $update) {
    // Make sure this is a revision
    if(wp_is_post_revision($post_id)) {
        // Save the slug as postmeta
        // First parameter is the post ID of the revision
        // Second parameter is what you want to call this postmeta key
        // Third parameter is what you're saving - the slug itself
        update_post_meta($post->ID, 'versionedSlug', $post->slug);
    }
}

You will then have to decide how you want to access this data. If you just want to browse a post's revisions, the revision comparison view will show you when the slug changed because it's now explicitly saved as postmeta. You could also create various reports or widgets, depending on your needs.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far