twitter-timeline.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' );
  3. function twitter_timeline_shortcode( $atts ) {
  4. $default_atts = array(
  5. 'username' => '',
  6. 'id' => '',
  7. 'width' => '450',
  8. 'height' => '282',
  9. );
  10. $atts = shortcode_atts( $default_atts, $atts, 'twitter-timeline' );
  11. $atts['username'] = preg_replace( '/[^A-Za-z0-9_]+/', '', $atts['username'] );
  12. if ( empty( $atts['username'] ) && ! is_numeric( $atts['id'] ) ) {
  13. return '<!-- ' . __( 'Must specify Twitter Timeline id or username.', 'jetpack' ) . ' -->';
  14. }
  15. $output = '<a class="twitter-timeline"';
  16. /** This filter is documented in modules/shortcodes/tweet.php */
  17. $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
  18. if ( ! empty( $partner ) ) {
  19. $output .= ' data-partner="' . esc_attr( $partner ) . '"';
  20. }
  21. if ( is_numeric( $atts['width'] ) ) {
  22. $output .= ' data-width="' . esc_attr( $atts['width'] ) . '"';
  23. }
  24. if ( is_numeric( $atts['height'] ) ) {
  25. $output .= ' data-height="' . esc_attr( $atts['height'] ) . '"';
  26. }
  27. if ( is_numeric( $atts['id'] ) ) {
  28. $output .= ' data-widget-id="' . esc_attr( $atts['id'] ) . '"';
  29. }
  30. if ( ! empty( $atts['username'] ) ) {
  31. $output .= ' href="' . esc_url( 'https://twitter.com/' . $atts['username'] ) . '"';
  32. }
  33. $output .= '>';
  34. $output .= sprintf( __( 'Tweets by @%s', 'jetpack' ), $atts['username'] );
  35. $output .= '</a>';
  36. wp_enqueue_script( 'jetpack-twitter-timeline' );
  37. return $output;
  38. }
  39. function twitter_timeline_js() {
  40. if ( is_customize_preview() ) {
  41. wp_enqueue_script( 'jetpack-twitter-timeline' );
  42. }
  43. }
  44. add_action( 'wp_enqueue_scripts', 'twitter_timeline_js' );