Title: PHP Code to Replace Published Date with Last Modified Date
Last modified: December 26, 2022

---

# PHP Code to Replace Published Date with Last Modified Date

 *  [michaelnorth](https://wordpress.org/support/users/michaelnorth/)
 * (@michaelnorth)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/)
 * Hi. I’m trying to replace the displayed published date with the last modified
   date. I’m injecting this script with the WPCode plugin. However, it’s not working
   at all — no change to the publish date on either pages or posts. Any ideas?
 *     ```wp-block-code
       <?php
   
       // Hook into the 'the_modified_date' filter for posts
       add_filter( 'the_modified_date', 'wpse_modified_date', 10, 2 );
   
       // Hook into the 'the_time' filter for pages
       add_filter( 'the_time', 'wpse_modified_date', 10, 2 );
   
       /**
        * Replace the published date with the last modified date.
        *
        * @param string  $the_modified_date   The current date string.
        * @param string  $date_format The date format.
        * @return string The modified date string.
        */
       function wpse_modified_date( $the_modified_date, $date_format ) {
       	// Get the post object
       	$post = get_post();
   
       	// Get the last modified date
       	$modified_date = get_the_modified_date( $date_format, $post );
   
       	// Replace the published date with the modified date
       	return $modified_date;
       }
   
       ?>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Kaavya Iyer](https://wordpress.org/support/users/kaavyaiyer/)
 * (@kaavyaiyer)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/#post-16320177)
 * Hi [@michaelnorth](https://wordpress.org/support/users/michaelnorth/),
 * I added the piece of code that you provided above in my child theme’s functions.
   php file and it correctly replaced the published date with last modified date.
   You can do the same.
 * Please make sure you don’t edit the theme files directly as they will be overwritten
   when the theme is updated. To protect your changes from updates, create a [Child Theme](https://developer.wordpress.org/themes/advanced-topics/child-themes/)
   in which to keep all your changes.
 * As for the issue with the snippet not reflecting when added via the WPCode plugin,
   please reach out to the [WPCode plugin Support Forum](https://wordpress.org/support/plugin/insert-headers-and-footers/)
   as this is not a WordPress Core issue.
 *  Thread Starter [michaelnorth](https://wordpress.org/support/users/michaelnorth/)
 * (@michaelnorth)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/#post-16326269)
 * Does anyone have an idea why this code works in a child theme, but not when injected
   with WPcode or Code Snippet plugins?
 *  [miketopher](https://wordpress.org/support/users/miketopher/)
 * (@miketopher)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/#post-16329537)
 * Assuming you want to do this on the single posts, and not archives, etc…
 * Try this code in your code snippets plugin:
 *     ```wp-block-code
       function get_last_modified_date() {
         if ( is_single() ) { // only display the last modified date on single posts
           $post_id = get_the_ID(); // get the current post ID
           $last_modified_date = get_post_modified_time('F j, Y', false, $post_id, false); // get the last modified date in the desired format
           return $last_modified_date; // return the last modified date
         }
       }
       ```
   
 *  Thread Starter [michaelnorth](https://wordpress.org/support/users/michaelnorth/)
 * (@michaelnorth)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/#post-16333716)
 * [@miketopher](https://wordpress.org/support/users/miketopher/)
   Thanks, but in
   this case, I need it work across pages, posts, archives, etc. Still baffled why
   the code works in a child theme, but not in Code Snippets. Thought that was the
   whole idea of the plugin?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘PHP Code to Replace Published Date with Last Modified Date’ is closed
to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [michaelnorth](https://wordpress.org/support/users/michaelnorth/)
 * Last activity: [3 years, 5 months ago](https://wordpress.org/support/topic/php-code-to-replace-published-date-with-last-modified-date/#post-16333716)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
