«

»

Nov 14 2012

Print this Post

Instagrate to WordPress – Hashtags and Post Tags

Like millions of others, I use Instagram. When I started using Instagram, I wanted the photos I shared to automatically show up on my WordPress site. I use the wonderful Instagrate to WordPress plugin to make this possible.  Besides the free version of the plugin, the developer also offers a Pro version that has many more features than the free version (obviously). One of these features is something I desperately wanted to see in the free version; the ability to automatically strip hashtags from the Instagram caption and convert them to post tags in the WordPress post created by the plugin. Instead of paying for the Pro version just to get this one feature, I decided to modify the plugin and add the functionality myself. To accomplish this modification, follow these steps.

  1. Edit the instagrate-to-wordpress.php file from the wp-content/plugins/instagrate-to-wordpress folder in your WordPress installation.
  2. Locate the following code around line 834:

//Custom Post Title
if ($customtitle != '' ){
  1. Add the following code right before the code above:
// Strip all hashtags from the title and create post tags from them.
preg_match_all('/#(\\w+)/', $post_title, $hashtags);

// Only need the string values without the hash '#'
$posttags = implode(',', $hashtags[1]);

// Remove the hashtags from the original post title to clean it up.
foreach ($hashtags[0] as $tag) {
    $post_title = str_replace($tag, '', $post_title);
}

$post_title = trim($post_title);
  1. The last thing you have to do is locate the following code about 40 lines down from where you just modified:
// Create post object
$my_post = array(
    'post_title' => $post_title,
    'post_content' => '',
    'post_author' => $postauthor,
    'post_category' => array($postcats),
    'post_status' => 'draft', //$poststatus,
    'post_type' => $posttype,
    'post_date' => $post_date, //The time post was made.
    'post_date_gmt' =>  $post_date_gmt //[ Y-m-d H:i:s ] //The time post was made, in GMT.
 );
  1. Add the following line after the ‘post_category’ entry in the array:
'tags_input' => $posttags,

Your code should now look like this:

// Create post object
$my_post = array(
    'post_title' => $post_title,
    'post_content' => '',
    'post_author' => $postauthor,
    'post_category' => array($postcats),
    'tags_input' => $posttags,
    'post_status' => 'draft', //$poststatus,
    'post_type' => $posttype,
    'post_date' => $post_date, //The time post was made.
    'post_date_gmt' =>  $post_date_gmt //[ Y-m-d H:i:s ] //The time post was made, in GMT.
);
  1. Save the file, and you are good to go. The next time you post an image using Instagram and use one or more hashtags (e.g., #picsofmycats), the plugin will convert the hashtags to a post tag.

Update (Dec. 5, 2014): I posted a working version of the instagrate-to-wordpress.php file.

Permanent link to this article: http://ericsilva.org/2012/11/14/instagrate-to-wordpress-hashtags-and-post-tags/

21 comments

Skip to comment form

  1. Dan Solo (@dansolocreative)

    You sir… are a genius!

  2. Dan Solo (@dansolocreative)

    Any idea how to also add the new stripped title to the media upload?

  3. Eric Silva

    The Instagrate plugin doesn’t create an object in the Media Library. It creates a link to your photo (hosted on Amazon AWS) in the post. You could modify the plugin to insert the Instagram photo into your media library and then use the stripped title as the title of the Media object.

  4. Dan Solo (@dansolocreative)

    There is an option to create a media object however it does not add the title unfortunately… Here is the relevant code (I think)…
    //image settings
    if ($imagesave == ‘link’) {
    //link to instagram image
    $image = ”;

    } else {

    //put image from instagram into wordpress media library and link to it.
    $post_image = self::strip_querysting($post_image);
    //load into media library
    $attach = self::attach_image($post_image,$new_post);

    $debug .= $attach[1];

  5. Eric Silva

    Do you want to set the title as the caption of the media object?

  6. Dan Solo (@dansolocreative)

    At the moment Title in the media object is blank as is caption. I’d like to set the title of the media object to the de-hashed title of the instagram title so that the media object would have the same title as the post that is also created. I am happy to leave the caption blank.

  7. Eric Silva

    I think you need to modify the attach_image() function to accept the $title as a parameter, and then modify the following code:
    $file_array = array(
    ‘name’ => basename( $url ),
    ‘tmp_name’ => $tmp
    );

    To include the additional line:
    ‘file’ => $title

  8. Dan Solo (@dansolocreative)

    Hmm it seems to have stopped instagram posts updating to the blog… Here is the code (is this what you meant?)

    /* Attach an image to the media library */
    function attach_image($url, $postid) {

    require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);

    $attach = array();

    $debug = “—————-Attaching Image: — “. Date( DATE_RFC822 ) . “\n”;

    try {

    $debug .= ‘——————URL: ‘.$url.’– ‘. Date( DATE_RFC822 ) . “\n”;

    $tmp = download_url( $url );
    $file_array = array(
    ‘name’ => basename( $url ),
    ‘tmp_name’ => $tmp,
    ‘file’ => $title
    );

  9. Eric Silva

    You need to update the function declaration to:

    function attach_image($url, $postid, $title = ”) {

    Then around Line 898, you need to change the following line:

    $attach = self::attach_image($post_image,$new_post);

    to:

    $attach = self::attach_image($post_image,$new_post, $post_title);

  10. Dan Solo (@dansolocreative)

    It’s still not updating now at all. Hmm…
    I changed your line here function attach_image($url, $postid, $title = ”) {
    to function attach_image($url, $postid, $title = “”) {
    was that what you intended?

    I now have

    /* Attach an image to the media library */
    function attach_image($url, $postid, $title=””) {

    require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);

    $attach = array();

    $debug = “—————-Attaching Image: — “. Date( DATE_RFC822 ) . “\n”;

    try {

    $debug .= ‘——————URL: ‘.$url.’– ‘. Date( DATE_RFC822 ) . “\n”;

    $tmp = download_url( $url );
    $file_array = array(
    ‘name’ => basename( $url ),
    ‘tmp_name’ => $tmp,
    ‘file’ => $title
    );

    and then at line 898…

    //load into media library
    $attach = self::attach_image($post_image,$new_post, $post_title);
    $debug .= $attach[1];

    Any ideas as to what I have done wrong? Very KonFooSing!

  11. Eric Silva

    Dan, if you send me your PHP file, I will take a look and see if I can see what’s causing the issue. You can send it to eric [at] ericsilva.org.

  12. Dan Solo (@dansolocreative)

    Thanks Eric,
    Greatly appreciated!
    Cheers
    Dan

  13. Natascha

    This is so cool! Thank you!!

  14. kie

    any way to convert post hashtags into tags in the very content?

  15. Eric Silva

    @kie, I am not sure I completely understand your question. You want to convert the Instragram hashtags into what exactly?

  16. marcus

    Can anyone post a working php file?

    And the the idea to convert hashtags to wordpress tags is smashing. Go for it guys!

  17. Eric Silva

    @marcus you can find my working file here: https://gist.github.com/ericjsilva/13ad79686601c3352ed6

  18. marcus

    Still getting some traces of hashtags in the title. All hashtags shows as tags in blogpost.

    Instragram title:
    WEBKILLEN TESTAR KOPPLINGEN TILL HEMSIDAN
    hashtags:
    #test #test2 #test3
    #test4 #testar #testar2 #testar4 #jag testar en lång tag #test åäö

    title Result:
    WEBKILLEN TESTAR KOPPLINGEN TILL HEMSIDAN. 2 34 AR AR2 AR4 TESTAR EN LÅNG TAG ÅÄÖ

    example:
    http://bygganaglar.se/webkillen-testar-kopplingen-till-hemsidan-2-34-ar-ar2-ar4-testar-en-lang-tag-aao/

    What can be the problem?

  19. marcus

    i was thinking maybe the swedish letters “ÅÄÖ” could be the problem. But numbers seems also become a problem converting hashtags to wordpresstags. please help

  20. skateboards

    thanks man, this worked perfectly first try! One modification I made tho, was to truncate the title starting at the index of ” #” (space, hashtag) so that it wouldn’t load up the title with 50 hashtags. that way, if you DO want a hashtag in the title, you simply prefix it with anything except a space, and otherwise, the first space hashtag stops the title nice and short. that was the final piece for me and now it’s auto-posting beautifully.

  21. john calabe

    I have used the updated php file in the plugin, but there is no luck. I see the same # tags in all my blogs page.

Leave a Reply