Home / Admin / Create a Smart Tag for the Current Time
Duplicate Snippet

Embed Snippet on Your Site

Create a Smart Tag for the Current Time

This snippet will create a new Smart Tag for you that you can use anywhere in the form builder. Once you've added the snippet, just add the {current_time} to any Default Value to have the current time entered as a value in your forms.

This snippet is set up with date_default_timezone_set( 'US/Eastern' ); in the snippet. Please remember to change this timezone to match yours before saving the snippet.

70+
Code Preview
php
<?php
/**
 * Create a custom Smart Tag 
   
   Original doc link: https://wpforms.com/developers/how-to-create-a-smart-tag-for-the-current-time/
   For support, please visit: https://www.facebook.com/groups/wpformsvip
 */
 
function wpf_dev_register_smarttag( $tags ) {
  
    // Key is the tag, item is the tag name.
    $tags[ 'current_time' ] = 'Current Time';
     
    return $tags;
}
  
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
  
  
/**
 * Process the Smart Tag.
 *
 * @link https://wpforms.com/developers/how-to-create-a-smart-tag-for-the-current-time
 */
 
function wpf_dev_process_smarttag( $content, $tag ) {
  
    // Only run if it is our desired tag.
    if ( 'current_time' === $tag ) {
         
        date_default_timezone_set( 'US/Eastern' );
         
        $link = date( 'h:i:s A' );
         
        // Replace the tag with our link.
        $content = str_replace( '{current_time}', $link, $content );
    }
  
    return $content;
}
  
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

Comments

Add a Comment