Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Timestamp Latest Update to Link?

Featured Replies

Hi, New here. I was searching for help and found a related topic on this forum.  The topic I wanted to post on is closed to comments.  I was wondering if it possible to use html or javascript to add a "Last Updated: Date and Time" to an audio file link. I'm using Wordpress with Divi builder so I can use the Codes module. My audio files are often overwritten using an overwrite plugin. I need the last updated to update when the file is overwritten with a file of the same name. 

<a href="http://url.com">News.mp3</a>

File Link: News.mp3
Last Updated:Fri Jun 12, 2020 03:20:13 pm

 

Thank you
Kimberly

I don't use Wordpress but it may store a date stamp with the file record in the database, check there first - if a timestamp is recorded the you could use that value and use a CMS template variable to output in the date format you wanted.

Other than that you could use the entry date or last edit date if that's updated at the same time as the file. Failing that you could create a custom field to update when the file updates.

The JavaScript date method wouldn't work in this case because it would always display the current date and not the actual date the file was uploaded. WordPress stores the metadata in the database and you can access it using the following function https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata. Honestly, I have no idea how you would do this with a page builder like Divi, you would probably have to create a custom shortcode that you can pass an attachment ID into.

This is quiet interesting, can you not make it in such a way that the filename already reflect the date and time before you even upload it?

  • Author
6 minutes ago, JaredM said:

This is quiet interesting, can you not make it in such a way that the filename already reflect the date and time before you even upload it?

I have 12 news files, mp3 that are updated daily using an overwrite plugin in wordpress. The client doesn't want to change the last updated daily. They want it to automatically happen. It works in the old 2015 websites which has not been updated since it was build. My job was to update the site to php 7 but the theme will not work when updating so I'm rebuilding it. I'm desperate trying to fix this one last problem. I can't figure out how they did it in the old website.  I'm looking for alternatives way to add a timestamp or at least a tutorial of some type. 

I want to understand how are you approaching it. Are you using a plugin or just using the native wordpress features?

  • Author
23 minutes ago, JaredM said:

I want to understand how are you approaching it. Are you using a plugin or just using the native wordpress features?

I've tried to figure out how to use a timestamp in PHP but I've not been able to figure that out. I've also tried to use javascript with a audio link but that was a no go too as I learned more. I've looked for plugins and I can't find any that will do that. I've tried css and shortcodes but neither can be done. I'm being told it must be done in php but I'm not sure how. That is where I'm at. 

Let's see if we can solve this.

How about just creating a copy of the news.mp3 file naming the copy to news+TS.mp3 (TS-time stamp). Maybe you can write a code to do this and just issue a CRON job to execute it every first minute of the day. You might also consider creating an archive folder to where these duplicate files will be stored.

 

There is a way to do this but you need to edit the theme templates.

Divi is the big problem. It’s so bad many developers won’t go anywhere near it.

The uploaded file is given an attachment/post ID. You can see the ID in the URL of the media editor.

Once you have the attachment ID you can use this function:

https://developer.wordpress.org/reference/functions/the_modified_date/

 

Edited by Jo 90
Removed direct link

Yeah, Fisicx is right. This should be trivial in terms of getting the modified date, but because you're using the Divi theme, we need to explain to potentially a non-programmer how you would do this and it does involve using PHP.

Your options would be something like:

  • Build a custom shortcode that you can use in Divi. This would take the file path or ID and output everything with the modified date, but this would need to be written custom - https://pagely.com/blog/creating-custom-shortcodes/. Try getting the first example here to work and if it goes ok I can put some code together you can use.
     
  • Use JavaScript but the date would just be the current date so it's likely not what you want but it might work if the content is brand new every day.
     
  • Perhaps explain to the client is unusual for people to download an identical file that has been modified daily. I can't think of a use where that makes sense over uploading a new file every time it has changed. If it's news, would you not potentially want to go back to a previous day's news?
  • Author
7 hours ago, Jack said:

Yeah, Fisicx is right. This should be trivial in terms of getting the modified date, but because you're using the Divi theme, we need to explain to potentially a non-programmer how you would do this and it does involve using PHP.

Your options would be something like:

  • Build a custom shortcode that you can use in Divi. This would take the file path or ID and output everything with the modified date, but this would need to be written custom - https://pagely.com/blog/creating-custom-shortcodes/. Try getting the first example here to work and if it goes ok I can put some code together you can use.
     
  • Use JavaScript but the date would just be the current date so it's likely not what you want but it might work if the content is brand new every day.
     
  • Perhaps explain to the client is unusual for people to download an identical file that has been modified daily. I can't think of a use where that makes sense over uploading a new file every time it has changed. If it's news, would you not potentially want to go back to a previous day's news?

I can build it outside divi.  I start working on websites in 1995, it's just been forever since I did any php work. Thanks for the shortcodes link, I will check it out. 

This is a farm stocks and news website and radio stations download these mp3 files often, some several times a day. Which makes the time important.  The original website was design and last updated in 2015 but the theme will not work with php7 which I why the new theme. I can't find how the did the timestamp in the old theme.  

Unless I find a answer by last afternoon, I think I'm going to post the job on a freelancer website, see if someone can do this for me.  I will look at the shortcodes links

  • Author
10 hours ago, fisicx said:

There is a way to do this but you need to edit the theme templates.

Divi is the big problem. It’s so bad many developers won’t go anywhere near it.

The uploaded file is given an attachment/post ID. You can see the ID in the URL of the media editor.

Once you have the attachment ID you can use this function:

https://developer.wordpress.org/reference/functions/the_modified_date/

 

Most my website clients have small business websites so Divi is a quick easy answer. It's been so many years since I've had a client that wants actually work on a website done but I couldn't find any other themes that had features I needed. 

1 hour ago, Kimber said:

Most my website clients have small business websites so Divi is a quick easy answer. It's been so many years since I've had a client that wants actually work on a website done but I couldn't find any other themes that had features I needed. 

Fair enough.

This is still what you need: https://developer.wordpress.org/reference/functions/the_modified_date/

It's fairly simple to add this to your theme temlates. If you are using Divi, set up a child theme.

An alternative is to build a quick plugin. The plugin creates a shortcode. The shortcode calls a function that gets the attachment ID and from that returns the modified date.

2 hours ago, Kimber said:

I can build it outside divi.  I start working on websites in 1995, it's just been forever since I did any php work. Thanks for the shortcodes link, I will check it out. 

This is a farm stocks and news website and radio stations download these mp3 files often, some several times a day. Which makes the time important.  The original website was design and last updated in 2015 but the theme will not work with php7 which I why the new theme. I can't find how the did the timestamp in the old theme.  

Unless I find a answer by last afternoon, I think I'm going to post the job on a freelancer website, see if someone can do this for me.  I will look at the shortcodes links

How do you add the files currently? Are they in a text editor and you replace the file using something like the Media Replace plugin?

  • Author
7 minutes ago, Jack said:

How do you add the files currently? Are they in a text editor and you replace the file using something like the Media Replace plugin?

They use https://wordpress.org/plugins/overwrite-uploads/  which overwrites the old file when the new file of the same name is dragged into the media file.  They have 12 files they replace daily if not more. 

  • Author
18 minutes ago, fisicx said:

Fair enough.

This is still what you need: https://developer.wordpress.org/reference/functions/the_modified_date/

It's fairly simple to add this to your theme temlates. If you are using Divi, set up a child theme.

An alternative is to build a quick plugin. The plugin creates a shortcode. The shortcode calls a function that gets the attachment ID and from that returns the modified date.

I've been trying to leave how and where to add that code to the theme or child theme but I'm clueless. I usually only do this type of stuff when I have a step by step tutorial to follow. I live in a small town and usually run small business websites that mostly products added to the online store and events listed. This is a bigger client which I really need. I've been looking at the cost of hiring a freelancer to write the codes for me and tell me where to put them. I thank everyone on this forum for their help. They have five more websites after this one so I need to get it done and move on. sorry, now I'm just venting.  :) 

It's an hour's work to build this.

You can add the action to the functions.php ina child theme.

If this is the sort of thing they want I suspect there will be a lot more php needed.

  • Author
16 minutes ago, fisicx said:

It's an hour's work to build this.

You can add the action to the functions.php ina child theme.

If this is the sort of thing they want I suspect there will be a lot more php needed.

I found a media manager plugin that does what I need but when you overwrite a file that a problem. I hope to have this worked out soon using this plugin   thanks

This will do what you want, add it to your functions.php file inside your theme.

function render_timestamp_shortcode($atts) {
  $fileID = !empty($atts) ? $atts['id'] : null;

  if( !$fileID ) return null; // return early if no ID exists

  $fileName = get_the_title($fileID);
  $filePath = wp_get_attachment_url($fileID);
  $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);
	
  $template = "
    <p class='timestamp-block'>
      File Link: <a href='${filePath}' download>${fileName}</a>
      <br /> Last Updated: ${fileDate}
    </p>
  ";
  return $template;
};
add_shortcode('timestamp-block', 'render_timestamp_shortcode');

To use it, add the following in a text editor and include the ID of the upload. You can find the ID in the URL when you click on a media item.

[timestamp-block id="12"]

You will need to remove the current media replace plugin, or deactivate it and install "Easy Media Replace" instead. The current one replaces the ID which won't work in this case.

  • Author
1 hour ago, Jack said:

This will do what you want, add it to your functions.php file inside your theme.


function render_timestamp_shortcode($atts) {
  $fileID = !empty($atts) ? $atts['id'] : null;

  if( !$fileID ) return null; // return early if no ID exists

  $fileName = get_the_title($fileID);
  $filePath = wp_get_attachment_url($fileID);
  $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);
	
  $template = "
    <p class='timestamp-block'>
      File Link: <a href='${filePath}' download>${fileName}</a>
      <br /> Last Updated: ${fileDate}
    </p>
  ";
  return $template;
};
add_shortcode('timestamp-block', 'render_timestamp_shortcode');

To use it, add the following in a text editor and include the ID of the upload. You can find the ID in the URL when you click on a media item.


[timestamp-block id="12"]

You will need to remove the current media replace plugin, or deactivate it and install "Easy Media Replace" instead. The current one replaces the ID which won't work in this case.

THANK YOU SO VERY MUCH!!  I wish I could hug you.

  • Author
On 6/18/2020 at 11:20 PM, Kimber said:

THANK YOU SO VERY MUCH!!  I wish I could hug you.

I would LOVE to use Easy Media Replace but the client wants this drag and drop replace file overwrite plugin. They use FTP and I wish they would use that on updating files. I've been looking at your codes and trying to see if I can get folder ID to work. Honestly I'm going to to try and talk them into use ftp file replacement.

UPDATE:  I think I found my overwrite issue answer. https://wordpress.org/plugins/wp-file-manager/  

 

How many audio clips do they have to replace every day? It's possible to work out something but it could be slow to process. Without trying, I'd imagine it would involve querying the database for each clip because the plugin they want to use deletes the old file and creates a new one with a brand new ID. You would have to do a database query to return the ID of the file based on the name, but it you couldn't have duplicate filename anywhere.

There's a more straight-forward way of doing this but it relies on passing in the full URL of the media file instead of the ID. I don't know if that plugin changes the file path depending on when it's uploaded or whether it always places the file in the same folder when it's replaced. If it does change the folder based on the upload date, to something like "/uploads/2020/06/" then this method will eventually stop working.

 

1 hour ago, Kimber said:

IUPDATE:  I think I found my overwrite issue answer. https://wordpress.org/plugins/wp-file-manager/ 

It's not going to work. You have to update the attachment using the media manager if you want to change the timestamp. Using FTP or the plugin will just update the file not the database.

The timestamp is recorded in the database. Unless the post object is updated the timestamp will be wrong.

  • Author
8 minutes ago, fisicx said:

It's not going to work. You have to update the attachment using the media manager if you want to change the timestamp. Using FTP or the plugin will just update the file not the database.

The timestamp is recorded in the database. Unless the post object is updated the timestamp will be wrong.

I know and the don't want to use file manager to upload media which would have worked. I'm looking for alternative file managers plugins that allow people to upload files.  They have one part owner in the company who doesn't want to change how they do things.  At some point he is going to have to give in some. 

No matter which plugin you use, the timestamp won't change. The only way to change the timestamp is to update the post object.

  • Author
33 minutes ago, fisicx said:

It's not going to work. You have to update the attachment using the media manager if you want to change the timestamp. Using FTP or the plugin will just update the file not the database.

The timestamp is recorded in the database. Unless the post object is updated the timestamp will be wrong.

I got it to work with a  file manager plugin because the media ID said the same because it was being uploaded directly in the cpanel upload folder in wordpress but they didn't like that either. 

Is it not possible to just create a folder and every time they upload a new version of the file, it triggers the creation of a new file (duplicate of the just uploaded file) with the date and time stamp which is to be stored in the folder created.

For example:

original filename: news.mp3

Once they upload a new version on 20 June 2020 @ 4:28am a copy of the new file is created with the following filename: news20June20200428.mp3

And for proper housekeeping maybe a folder named "archives" will hold all the files having time stamp with them.

say: ./archives/news20June20200428.mp3

Just my thought

The timestamp isn't the issue, If the poster wants to try then I can re-write what I have to work with attachment URLs instead. The problem is, if the client replaces a file in a months time, will WordPress put it in a different directory or will it use the existing one? Since replacing isn't built into WP, there's no docs on this, so I'm not 100% sure. WP organises media files into year and month folders, if you're passing a full URL into a shortcode and the folder changes, the script stops working.

I know there is an option under settings labelled "Organize my uploads into month- and year-based folders", but I don't know what happens to existing files if this option is unticked. It might be that this option needs to be unticked and the files need to be re-uploaded for this to work consistently going forward.

  • Author
6 hours ago, Jack said:

The timestamp isn't the issue, If the poster wants to try then I can re-write what I have to work with attachment URLs instead. The problem is, if the client replaces a file in a months time, will WordPress put it in a different directory or will it use the existing one? Since replacing isn't built into WP, there's no docs on this, so I'm not 100% sure. WP organises media files into year and month folders, if you're passing a full URL into a shortcode and the folder changes, the script stops working.

I know there is an option under settings labelled "Organize my uploads into month- and year-based folders", but I don't know what happens to existing files if this option is unticked. It might be that this option needs to be unticked and the files need to be re-uploaded for this to work consistently going forward.

I've rebuild the site from scratch on an dummy url I use for redesigning websites. I uncheck that option for the beginning and made sure all the files are in one directory. That is the only way the overwrite plugin they want to use works. It uploads the new file of the same name and deletes the old file.  But new file has a different ID number which messes up the timestamp shortcodes. I found a file manager plugin that allowed them to upload directly to the upload folder which fixed all the issues but which my contact loves the ideas. He got vetoed, they want it to work like it has for years which I have no clue how the guy back in 2015 set this up. I'm going back through all the php codes looking for something that says all link have timestamps or all media files have timestamps.  Its just a mess!!!!!

  • Author
4 hours ago, fisicx said:

If you untick it only works on new files. Everything else stays where it is.

 

I set that up ahead of time when rebuilding the website. 

Amending the previous function to this will allow it to accept a URL instead of an ID, which will work with the "Overwrite Uploads" plugin.

function render_timestamp_shortcode($atts) {
  $getFileUrl = !empty($atts['url']) ? $atts['url'] : null;
  $getFileID = attachment_url_to_postid($getFileUrl);

  if(!$getFileID) return null;

  $fileID = $getFileID ? $getFileID : null;

  if(!$fileID) return null;

  $fileName = get_the_title($fileID);
  $filePath = wp_get_attachment_url($fileID);
  $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);

  $template = "
    <p class='timestamp-block'>
      File Link: <a href='${filePath}' download>${fileName}</a>
      <br /> Last Updated: ${fileDate}
    </p>
  ";
  return $template;
};

add_shortcode('timestamp-block', 'render_timestamp_shortcode');

You will need to change your shortcodes to pass in a URL, which you can get by clicking on an item in Media and copying the File URL. Just make sure it's not an actual HTML link in the editor because WP might try to convert it.

[timestamp-block url="https://dev.test/wp-content/uploads/file.mp3"]

Lastly, deactivate any replacement plugins and reactivate "Overwrite Uploads".

Edited by Jack

  • Author
On 6/21/2020 at 6:28 AM, Jack said:

Amending the previous function to this will allow it to accept a URL instead of an ID, which will work with the "Overwrite Uploads" plugin.


function render_timestamp_shortcode($atts) {
  $getFileUrl = !empty($atts['url']) ? $atts['url'] : null;
  $getFileID = attachment_url_to_postid($getFileUrl);

  if(!$getFileID) return null;

  $fileID = $getFileID ? $getFileID : null;

  if(!$fileID) return null;

  $fileName = get_the_title($fileID);
  $filePath = wp_get_attachment_url($fileID);
  $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);

  $template = "
    <p class='timestamp-block'>
      File Link: <a href='${filePath}' download>${fileName}</a>
      <br /> Last Updated: ${fileDate}
    </p>
  ";
  return $template;
};

add_shortcode('timestamp-block', 'render_timestamp_shortcode');

You will need to change your shortcodes to pass in a URL, which you can get by clicking on an item in Media and copying the File URL. Just make sure it's not an actual HTML link in the editor because WP might try to convert it.


[timestamp-block url="https://dev.test/wp-content/uploads/file.mp3"]

Lastly, deactivate any replacement plugins and reactivate "Overwrite Uploads".

THANK YOU SO MUCH.   There are no words to show how much I appreciate this.

  • Author
On 6/21/2020 at 6:28 AM, Jack said:

Amending the previous function to this will allow it to accept a URL instead of an ID, which will work with the "Overwrite Uploads" plugin.


function render_timestamp_shortcode($atts) {
  $getFileUrl = !empty($atts['url']) ? $atts['url'] : null;
  $getFileID = attachment_url_to_postid($getFileUrl);

  if(!$getFileID) return null;

  $fileID = $getFileID ? $getFileID : null;

  if(!$fileID) return null;

  $fileName = get_the_title($fileID);
  $filePath = wp_get_attachment_url($fileID);
  $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);

  $template = "
    <p class='timestamp-block'>
      File Link: <a href='${filePath}' download>${fileName}</a>
      <br /> Last Updated: ${fileDate}
    </p>
  ";
  return $template;
};

add_shortcode('timestamp-block', 'render_timestamp_shortcode');

You will need to change your shortcodes to pass in a URL, which you can get by clicking on an item in Media and copying the File URL. Just make sure it's not an actual HTML link in the editor because WP might try to convert it.


[timestamp-block url="https://dev.test/wp-content/uploads/file.mp3"]

Lastly, deactivate any replacement plugins and reactivate "Overwrite Uploads".

Hi, I've been trying to work this out myself with no luck. On this codes:

 $fileDate = get_the_modified_date('d/m/Y \a\t g:ia', $fileID);

It says on the website

File Link: hat006
Last Updated: 06/13/2020 at 5:56pm

and it should say  6/25/2020 at 2:34

Any ideas why?  

Thanks

I'm not sure it will work the way you want.

get_the_modified_date accesses the database object for the attachment. You will need to update the object meta data when the new attachment is uploaded.

https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata/

I have checked it and the modified date does update properly, it sounds more like a timezone issue. If you go to Settings -> General in the WP admin you can amend your timezone. The next time the files are replaced the date should reflect the change.

I'm not 100% sure why the date is so out, it's possibly that the server your code is running on has an incorrect date set. Start with the timezone amend first and see if it gets you any closer.

Edited by Jack

  • Author
19 hours ago, Jack said:

I have checked it and the modified date does update properly, it sounds more like a timezone issue. If you go to Settings -> General in the WP admin you can amend your timezone. The next time the files are replaced the date should reflect the change.

I'm not 100% sure why the date is so out, it's possibly that the server your code is running on has an incorrect date set. Start with the timezone amend first and see if it gets you any closer.

I've been paying with this time setting in wordpress and it's not helping. I going to go look at the cpanel and server settings.  Thank you for the input

12 minutes ago, Kimber said:

I've been paying with this time setting in wordpress and it's not helping. I going to go look at the cpanel and server settings.  Thank you for the input

It works fine for me whenever I change timezone. You'll need to replace the media files to see any changes too.

Edited by Jack

  • Author
1 hour ago, Jack said:

It works fine for me whenever I change timezone. You'll need to replace the media files to see any changes too.

I put it on another hosting company and it worked fine. It has something to do with the hosting they use. 

  • Jo 90 locked this topic
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.