• Resolved driftmagazine

    (@driftmagazine)


    I do not want to the_modified_time changed when admin change something.

    If I as an admin wants to make small changes in post I do not want to the_modified_time be changed, is it possible?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi driftmagazine,

    It is certainly possible to prevent the modified date from changing, however I would discourage you from doing so. If you’re absolutely certain this is what you want to do, then you will want to write a plugin that can utilize the wp_insert_post_data hook. You would want to perform a check for the administrator role using the current_user_can() function, to ensure you’re only targeting the correct thing.

    Here’s a sample plugin that could do this: https://gist.github.com/JPry/2e4b074a81d7c8234eb4.

    With this sample plugin, note that there’s no distinguishing at all between posts of various types. You may need to expand the logic to cover additional functionality.

    Thread Starter driftmagazine

    (@driftmagazine)

    Thanks!!
    Why would not you recommend it? Is it something I did not think of?

    If I want it not to be changed only in the custom post type “bil”?
    https://driftmagazine.se/blog/bil/

    Why would not you recommend it? Is it something I did not think of?

    When you modify the post, but don’t allow the post to know the timestamp of that modification, you’re essentially lying about the post history.

    If I want it not to be changed only in the custom post type “bil”?

    Add a conditional for post type, which is contained in the $data array:

    if ( 'bil' === $data['post_type'] && current_user_can( 'administrator' ) ) {
    // ...
    }
    Thread Starter driftmagazine

    (@driftmagazine)

    Thank you so much, unfortunately I do not get the last one to work.
    So here I put the last code, maybe completely wrong?

    <?php
    /**
     * Plugin Name: wp_insert_post_data example
     * Plugin URI: https://gist.github.com/JPry/2e4b074a81d7c8234eb4
     * Description: Example of prevnting the modified date from changing.
     * Version: 1.0
     * Author: Jeremy Pry
     * Author URI: https://jeremypry.com/
     * License: GPL2
     */
    
    // Prevent direct access to this file
    if ( ! defined( 'ABSPATH' ) ) {
    	die( "You can't do anything by accessing this file directly." );
    }
    
    add_filter( 'wp_insert_post_data', 'jpry_filter_insert_post_data', 10, 2 );
    function jpry_filter_insert_post_data( $data, $postarr ) {
    	// Only strip the modification time for Administrator role.
    	if ( current_user_can( 'administrator' ) ) {
    		unset( $data['post_modified'] );
    	}
    
    	if ( 'bil' === $data['post_type'] && current_user_can( 'administrator' ) ) {
    // ...
    }
    
    	return $data;
    }

    You would want the second if statement to entirely replace the first if statement. Take a look at this updated version: https://gist.github.com/JPry/2e4b074a81d7c8234eb4.

    Thread Starter driftmagazine

    (@driftmagazine)

    Thank you so much!! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘I do not want to the_modified_time changed when admin change’ is closed to new replies.