Support » Plugin: Jetpack - WP Security, Backup, Speed, & Growth » Add hashtags to twitter

Viewing 4 replies - 1 through 4 (of 4 total)
  • What I’ve done is adding the below function to my theme functions.php file:

    function append_jetpack_hashtags($ignorePostStatus = false) {
        $mess_max_length = 118;
        
        $strip_spaces = true;
       
        $meta = '_wpas_mess';
        
        $p = get_post();
        if (empty($p)) return;
        if (!$ignorePostStatus && $p->post_status === 'publish') return;
        $mess = get_post_meta($p->ID, $meta, true);
    
        if (empty($mess)) $mess = $p->post_title;
        else {
            if ($mess[0] == "#") {
                $a = explode(" ", $mess);
                $onlyHashtags = true;
                foreach ($a as $w) {
                    if ($w[0] != "#") {
                        $onlyHashtags = false;
                        break;
                    }
                }
                if ($onlyHashtags) $mess = $p->post_title ." ". $mess;
            }
        }
    
        if (!empty($mess) && !empty($mess_max_length) && $mess_max_length <= (strlen($mess))) return;
    
        // Change to the list of hashtags you want to be displayed.
        $ta = array( 'forex', 'trading' );
    
        if (empty($ta)) return;
    
        if (empty($mess)) $mess = '';
    
        foreach ($ta as $t) {
            // Create the hashtag, stripping spaces if needed.
            $ht = '#' . (($strip_spaces) ? str_replace(' ', '', $t) : $t);
            // only process newly-added hashtags, skipping duplicate ones
            if (stripos($mess,$ht) === false) {	
                if (!empty($mess_max_length) && $mess_max_length <= (strlen($mess) + strlen($ht))) break;
                $mess .= ' '.$ht;
            }
        }
    
        update_post_meta($p->ID, $meta, $mess);
    }
    add_action('save_post', 'append_jetpack_hashtags', 99);

    Change the below array to the hashtags you want to be displayed.
    $ta = array( 'forex', 'trading' );
    Or you can use this to automatically use the post tags as hashtags
    $ta = wp_get_post_tags();

    This does however, add the hashtags to every social media. But hey, nearly all social networks support hashtags, don’t they?

    Although I changed it, credits to the original author of the code (I cannot find the original location).

    Plugin Contributor Ryan C.

    (@ryancowles)

    @stevemordue: Great question! Are you trying to automatically add hashtags to every post, or are you trying to add them manually for each post? If the latter, you can customize the message you send to Twitter for each post, and simply include the hashtags in that message.

    Thread Starter stevemordue

    (@stevemordue)

    I will try the function above and report back. Looks like it might do the job, thanks

    Plugin Contributor Ryan C.

    (@ryancowles)

    That sounds like a plan! Please let us know if you have any other questions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add hashtags to twitter’ is closed to new replies.