How to add a shortcode to an HTML image tag

I want to add a timestamp to use as unique id for conversion tracking.

I have a [[timestamp]] shortcode on my site. In text it works fine, but the shortcode does not work when placed inside the src attribute of an image tag, e.g.:

img src="http://example.com/transaction_id=[[timestamp]]"

Does anyone know a solution to this issue?

The shortcode I use:

// Do TimeStamper Shortcode ( timestamper() ) 
function timestamper( $atts ){

  if (!is_admin()) {
    add_filter('widget_text', 'do_shortcode', 11);
  }
  $time = current_time( 'timestamp' );
  return $time;
}

add_shortcode( 'timestamp', 'timestamper' );

I put the shortcode in the html code:

`img src="http://oa1.nl/m/4521/cf039315e6a96f616cc0fe87f861fc640b0f3327/?transactie_id=[timestamp]" style="width: 1px; height: 1px; border: 0px;”` 

As you can see here in the output the shortcode is not replaced by the time. In text (the numbers you see on the page) it works fine but below the text there is the image code (you can not see the image, only in code, because it is 1 x 1 pixel item). I attached a printscreen of the source code:

Topic shortcode Wordpress

Category Web


Use a separate function to enable shortcodes in the text widget because trying to do so within the shortcode function itself is too late.

// Enable the use of shortcodes within the text widget.
function wpse249090_widget_text_enable_shortcodes() {
    add_filter( 'widget_text', 'do_shortcode' );
}
add_action( 'init', 'wpse249090_widget_text_enable_shortcodes' );

// Do TimeStamper Shortcode ( timestamper() ) 
function timestamper( $atts ) {
  $time = current_time( 'timestamp' );
  return $time;
}
add_shortcode( 'timestamp', 'timestamper' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.