Add hashtags support to WordTwitKể từ khi tôi bắt đầu blog này tôi đã được sử dụng WordTwit bởi BraveNewCode (họ chạy ra khỏi không gian dường). Đó là một đơn giản để sử dụng các plugin cho Wordpress rằng sẽ cập nhật của bạn Twitter tình trạng bất cứ khi nào bạn xuất bản một bài blog mới.

Bạn có thể tùy chỉnh và sử dụng thông điệp của bạn [Tiêu đề] để thay thế nó với tiêu đề thực tế của bài viết và [liên kết] với một (rút ngắn) URL để bài viết. Vì vậy, bạn có thể làm điều gì đó như “New blog entry ‘[Tiêu đề]‘ đăng tại [liên kết]“ – Tốt, có lẽ bạn muốn giữ nó càng ngắn càng tốt mặc dù, nhưng nó để cung cấp cho bạn một ý tưởng.

Vì vậy, cho rằng bạn có thể tùy chỉnh thông điệp của bạn, bạn cũng có thể thêm một hoặc hai hashtags. Nhưng đây là những tất cả làm bằng tay, Ví dụ: “Blog [Tiêu đề] – [liên kết] # Blog” là những gì tôi đã được sử dụng bản thân mình.

Nhưng # Blog hashtag thực sự không phải là một cái gì đó rất nhiều người dân sẽ tìm. Vậy tại sao không tái sử dụng các thẻ mà tôi đã có thêm vào bài viết của tôi với Wordpress? Giống như bài viết này ví dụ:, tôi không thể sử dụng của nó tuỳ biến thẻ như là # tuỳ biến hashtag trong Twitter cập nhật của tôi? Tốt, with some modifications to WordTwit, nó có thể. Đây là cách.

The modification involves editing the file wordtwit.php, which can be found in your /wp-content/plugin/wordtwit/ thư mục. The first part is very minor and simply involves moving a line of code a little further up. You need to look for $post_tags = get_the_tags();” near line 484 (in version 2.2.4 of WordTwit):

...(snip)...
        $can_tweet = true;

        // check tags
        if ( count( $wt_tags ) ) {

            // we have a tag or a category
            $new_taxonomy = array();

            $post_tags = get_the_tags();
            if ( $post_tags ) {
                foreach ( $post_tags as $some_tag ) {
                    $new_taxonomy[] = strtolower( $some_tag->name );
                }
            }
...(snip)...

Move the  $post_tags = get_the_tags();” line up and place it under the $can_tweet = true;” dòng. This is done to avoid calling the same function twice. Admittedly, it’s a minor speed hurdle, but if you get into this habit of doing this throughout code, it’ll eventually speed things up. So now the above code will look like this:

...(snip)...
        $can_tweet = true;
        $post_tags = get_the_tags(); // <-- The line was moved here

        // check tags
        if ( count( $wt_tags ) ) {

            // we have a tag or a category
            $new_taxonomy = array();               

            if ( $post_tags ) {
                foreach ( $post_tags as $some_tag ) {
                    $new_taxonomy[] = strtolower( $some_tag->name );
                }
            }
...(snip)...

Up for the next bit.  At around line 515 of the same file (wordtwit.php) you need to inject the code that will handle the WordPress tags to Twitter hashtag conversion. The actual line number will depend on the version of WordTwit you’re using (Trong ví dụ này, Phiên bản 2.2.4) so I have included a snippet of the existing code as a reference where to add it. The actual code will be commented accordingly:

...(snip)...
            $message = $settings['message'];
            $message = str_replace( '[title]', $post->post_title, $message );

            global $post;
            $message = str_replace( '[link]', wordtwit_make_tinyurl( get_permalink(), true, $post->ID ), $message );

// !!! Wordpress tag to Twitter hashtag conversion STARTS HERE >>>

            if ( $post_tags && strpos($message, '[tags]') !== false ) {
                $tags_to_add = '';

                // What's the message length without this tag?
                $message_len = strlen($message) - 6;

                foreach ( $post_tags as $some_tag ) {
                    $tag_len = strlen($some_tag->name) + 2;

                    // Can we add this tag within the 140 char constraint? (accounts for hashtag and spacer)
                    if ( $tag_len + $message_len <= 140 ) {
                        $tags_to_add .= '#'.$some_tag->name.' ';
                        $message_len += $tag_len;
                    } else {
                        // Nope, we're done
                        break;
                    }
                }

                $tags_to_add = rtrim($tags_to_add);
                $message = str_replace('[tags]', $tags_to_add, $message);
            }

// <<< Wordpress tag to Twitter hashtag conversion ENDS HERE !!!   

            $twit_username = $settings['username'];
            $twit_password = $settings['password'];

            twit_update_status( $twit_username, $twit_password, $message );
...(snip)...

Save the file and you’re done with the modification!

Now you can customize your WordTwit message with three replaceable  tags: [Tiêu đề], [liên kết] and the new one [Tags]. When you specify [Tags] in your custom message, WordTwit will use the tags you’re using from WordPress and convert them to a Twitter hashtag. It will try to fit in as many as possible, up to a limit of 140 characters. If you have 10 WordPress tags but there’s only room for 3, then only the first 3 hashtags will appear in your Twitter status update. And obviously all 10 will appear if there’s room for them.

Bài viết liên quan:

  1. Nhanh hơn WP Super Cache với Nginx
  2. WP Viet Hộp Autohide tính năng
  3. WP Flickr nền phiên bản 1.0.2 (Beta) có sẵn
  4. Lifestream cho 15 Tháng Mười Hai
  5. Fancy WordPress từ