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