Nov 14 2012

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. You sir… are a genius!

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

    • Eric Silva on Sunday, February 24, 2013 at 11:34 pm
      Author

    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.

  3. 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];

    • Eric Silva on Monday, February 25, 2013 at 12:00 am
      Author

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

  4. 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.

    • Eric Silva on Monday, February 25, 2013 at 12:14 am
      Author

    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

  5. 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
    );

    • Eric Silva on Monday, February 25, 2013 at 12:44 am
      Author

    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);

  6. 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!

    • Eric Silva on Monday, February 25, 2013 at 6:58 am
      Author

    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.

  7. Thanks Eric,
    Greatly appreciated!
    Cheers
    Dan

    • Natascha on Friday, April 11, 2014 at 11:58 am

    This is so cool! Thank you!!

    • kie on Monday, September 15, 2014 at 4:00 pm

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

    • Eric Silva on Monday, September 15, 2014 at 8:31 pm
      Author

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

    • marcus on Friday, December 5, 2014 at 4:18 am

    Can anyone post a working php file?

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

    • Eric Silva on Friday, December 5, 2014 at 1:59 pm
      Author

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

    • marcus on Sunday, January 25, 2015 at 6:19 am

    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?

    • marcus on Monday, January 26, 2015 at 7:19 am

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

  8. 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.

    • john calabe on Tuesday, April 5, 2016 at 12:45 am

    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