Skip to content

Auto post to Pinterest from WordPress

A few months ago I needed a way to cross post from a WordPress blog to a Pinterest board. Yes, there are plugins that claim to do this. But the few free ones that I tried never actually posted, and I didn’t want to spend money on a paid plugin that might not work. So I came up with my own solution.

One note though. This isn’t a WordPress plugin, although I may write one eventually. This is a script that runs from a cron job on your server. You can setup cron jobs through control panels like cPanel or directly from the command line. Either way, most people will find it a bit more complicated than just activating a plugin.

I ended up spinning this into a normal WordPress plugin that will automatically create pins when a post is published. If you’re interested you can find the Pinterest Auto Post plugin at Reliti.com.

What we’re building

The idea is that I didn’t really care that new posts were immediately posted to Pinterest as long as they got there relatively quickly and I didn’t have to do it manually.

It’s also important that posts only go to Pinterest once, so there needs to be a way to keep track of what’s already been posted.

What I ended up with is a script that runs every 30 minutes through a cron job, although you could change the timing to whatever you want. It pulls the most recent published post from WordPress and posts the link to a Pinterest board.

One caveat. Since Pinterest is visual and requires images, this script skips any posts that don’t have a featured image.

With that, let’s get it actually working.

Getting a Pinterest access token

First thing you’ll need to do is get an access token from Pinterest. This allows your script to connect to their API as you without you having to be there.

Head to the Pinterest Token Generator.

Since this is probably your first time there you may have to agree to their developer terms. You also probably don’t already have an app defined, so we’ll do that first. Click on either the App menu item on the top, or it’s likely that Pinterest will automatically pop up the following screen.

Pinterest app creation form
Pinterest new app form

 

You can name your app anything you want. Same thing with the description. You’re probably going to be the only one that sees it.

Once that’s done you’ll need to create an app token. You should see a screen like this.

Pinterest Token Generator Form
Token Generator form

 

Click on the blue Generate Token button. A form will come up asking you to verify that you want to give access to your Pinterest account. You’ll need to agree to go any farther.

After you do that the token will be in the text box where it says “Your Access Token” in the screen shot above. It’ll be a seemingly random string of characters. Copy that and paste it somewhere safe. Once you leave this page you won’t be able to get the token again, although you can request a new one.

Pinterest Library

There’s a library on GitHub that’ll handle the Pinterest API calls for us. Two choices. You can either download it from GitHub of you can load it through Composer.

I used Composer and added the library as a requirement to the composer.json fie. The package is named dirkgroenen/Pinterest-API-PHP. If you’re not using Composer you’ll need to handle including the Pinterest API library.

The Code

Here’s the script I came up with. We’ll discuss down the page a bit.

First two things we need to set are the key and what board you’re posting to. Look for the lines that have PINTEREST_KEY and PINTEREST_BOARD.

Paste the key you created earlier between the quotes after PINTEREST_KEY.

The board is made up of your account and the board slug. It’s pretty much everything after pinterest.com in the address bar. Let’s look at one of Disney’s Pinterest boards.

Disney Pixar Pinterest Board

Look in the address bar. The value we’re looking for is Disney/disneypixar. Of course you’ll need to change it to match your board.

Multisite?

Are you running WordPress multisite? You’ll need to adjust and uncomment a couple of lines.

You want to set the BLOG_ID value to match the numeric ID of the blog you’re posting from and uncomment that line.

And you’ll also need to uncomment the line that starts with switch_to_blog. That, along with the BLOG_ID constant, tells WordPress which blog to use.

If you’re not using multisite, just leave those two lines commented out.

Blog Settings and Includes

Next up we need to set the $_SERVER variable to match your site. Replace www.example.com with your domain name for both HTTP_HOST and SERVER_NAME. WordPress will need this later.

After that’s done we’re going to include a couple of files.

The first is the autoload.php from Composer. If you’ve downloaded the Pinterest API library and aren’t using Composer, this is where you would include all of the needed files from the library.

Next is the include to wp-load.php. This is the bootstrap file that loads all of WordPress for your script and gives it access to the functions that this script will need. You’re going to need to fill in the full server path for that file.

What’s Happening

Now that you’ve got the settings working, let’s look at what the script does with all of that.

First, it creates a new Pinterest object to handle all of the API calls and sets the token so that it’ll work.

Next it looks for the most recent post that hasn’t been cross posted to Pinterest. It does this by looking for posts that don’t have a meta key named pinterest_date set.  If nothing is found, the script exits.

Assuming it finds a post, it looks for the url of the featured image for that post. If it doesn’t find one it tags that post as complete so that it doesn’t get selected next time and then exits.

Now that we have a thumbnail it’s time to post. Using the Pinterest API library it submits the link along with the thumbnail to your board. Assuming that works it saves the date of the cross post in the same pinterest_date meta field. If it fails for some reason it prints out the exception so that you can figure out what’s going on. It also adds the pinterest_date field, even on errors, so that your server doesn’t keep trying to post the same errant post.

Cron Job

Last step for me was to automate this, and you’ll want to do that through cron.

This is the line in my crontab for this script.

What this tells your server is to run /usr/bin/php /path/to/post-to-pinterest.php every 30 minutes, the */30, every day of every week of every month of every year, the four * symbols.

You’ll need to change the path to where you actually put the script file though.

Published inCodingWordPress

4 Comments

  1. Jonathan Jonathan

    Thank you so much. I had been trying to get this to work for hours following other instructions. The code and steps you gave were perfect

  2. you know what friend, this article comes like a god, I was looking for the custom code and got this. Loved the code, the content and now you buddy. Thanks, A GREAT THANKS.

  3. Chapter 2 Online Arts Crafts and Bookstore in Pakistan. We serve to be online buy books, stationery, j. perfumes, acrylic paints or things for the party decor.

Leave a Reply

Your email address will not be published. Required fields are marked *