0

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: the code on output

1
  • 1
    Shortcodes only need one opening and closing bracket, e.g. [timestamp]. Would you please explain what you mean by doesn't work, and please add the PHP used for generating the shorcode's output. – Dave Romsey Dec 13 '16 at 10:02
0

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' );
2
  • Hi Dave, thanks for your answer. I have updated my question with some code to refine my problem... – Wilfred van den Brand Dec 13 '16 at 11:05
  • Hello, Wilfred. Thank you for the update. I've updated my answer based on the new information. – Dave Romsey Dec 13 '16 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.