#### config.php

Note that the OAuth callback URL is based on http://twitter.dev site. This is the way I defined my virtual host in apache on my machine. You can define yourself with other name.

In this file we define our constants. The first four are parameters in the TwitterOAuth constructor (see above) and the last one replaces the callback URL we entered when defined the application. You get the first four constants from the Twitter application.

### The tweet function

The tweet function is hooked to WordPress save_post action and receives as a parameter the post id. With that post id it gets the post content as an array. We use this array to get the post title which then we use for tweeting based on the \$connection variable stored in the class.

The complete code of our worker class looks like following:

## Hook in to WordPress

Hooking in to WordPress means making WordPress to call your functions when a specific event occur. It can be done in two different modes: by using filters and by using actions. The function you use to hook in is either add_action or add_filter. Which one you should use is specified in the WordPress documentation. If the event you want to hook to is an action, then use add_action otherwise use add filter.

For our Twitter plugin we hook in to two events: the init action and the save_post action. The init action is called when the page is loaded and we will use this action to connect to Twitter. The save_post action is called whenever the user creates and updates a post so we will use it to Tweet. The code is listed below:

The code above has a small bug left intentionally inside to create an exercise for you. As I specified, the save_post action is called when the user creates or updates a post. Therefore it will be called twice when the user created a post and the post has no content and when the post is saved. This will lead to tweeting twice one with a post title given by WordPress by default and second with the title given by the user. You have to fix this bug using any method you find appropriate, maybe even post your solution in the comments.

## That's it!

Well, it wasn't so hard was it? Writing this plugin was quite easy and we also learned how to reuse other people's code. You can extend this example with other things that you want to do, for instance you can try to send a direct message to somebody.

Don't forget to check the WordPress and Twitter documentation: