/modules/shortcodes/twitter-timeline.php

  1. <?php 
  2. add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' ); 
  3.  
  4. function twitter_timeline_shortcode( $attr ) { 
  5.  
  6. $default_atts = array( 
  7. 'username' => '',  
  8. 'id' => '',  
  9. 'height' => 282,  
  10. 'width' => 450,  
  11.  
  12. ); 
  13.  
  14. $attr = shortcode_atts( $default_atts, $attr, 'twitter-timeline' ); 
  15.  
  16. if ( $attr['username'] != preg_replace( '/[^A-Za-z0-9_]+/', '', $attr['username'] ) ) 
  17. return '<!--' . __( 'Invalid username', 'jetpack' ) . '-->'; 
  18.  
  19. if ( ! is_numeric( $attr['id'] ) ) 
  20. return '<!--' . __( 'Invalid id', 'jetpack' ) . '-->'; 
  21.  
  22. $tweets_by = sprintf( __( 'Tweets by @%s', 'jetpack' ), $attr['username'] ); 
  23. $output = '<a class="twitter-timeline" width="' . (int)$attr['width'] . '" height="' . (int)$attr['height'] . '" href="' . esc_url( 'https://twitter.com/'. $attr['username'] ) . '" data-widget-id="' . esc_attr( $attr['id'] ) . '">' . esc_html( $tweets_by ) . '</a>'; 
  24. add_action( 'wp_footer', 'twitter_timeline_js' ); 
  25.  
  26. return $output; 
  27.  
  28. function twitter_timeline_js() { 
  29. echo '<script>!function(d, s, id) {var js, fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)) {js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js, fjs);}}(document, "script", "twitter-wjs");</script>'; 
.