 Welcome back to the channel. In today's video I'm going to show you how to serve data to a foreign application or an external application outside of your WordPress. And the way we shall be doing that is by using what we commonly call APIs and webhooks. So for APIs we know that an API is just an application programming interface and this one allows us to share data. For example we've been using APIs with things like Google Maps and so on and the way we do that is we can request particular information like a location and we pull the data from each one of those places and we can put it in our own applications. For example in our mobile apps. Now with webhooks that's a little bit different and that is because a webhook is basically just a post request that is sent to a specific URL. Sometimes people call the webhooks reverse APIs and that is because they sort of do something that's a little bit different from what APIs do. How do we use the webhooks? We use them when we register our particular URL where we want data to be posted in the service. Now when an event happens in our server that will push the data to the URL that has been added to the service request. So for example if let's say an event happened for example we serve a post in WordPress that can be sent as a message to an external application for it to update its data. Now sometimes we can ask our service provider or a webhook provider to use particular events for data to be sent to our URL. For example github allows you to trigger particular information for example on a pull request, on a push, on a merge. Those are events that are in github that can be used on the webhooks that they do provide. Now the biggest difference that we do have with APIs and webhooks is that APIs require us to send a request to the server like you see in the diagram that we do have here and then data will be sent back into our system or application. While the webhook is different for it, it will automatically send data if there is any data available at that particular point. So APIs are two way they send a request and then they receive back the data while a webhook is just going to always trigger data to the URL that has been submitted as long as there is data that is available. Now that can depend on particular events depending on the user of the webhook, if they choose to or if the service provider is giving those options or it will always just send data irregardless. What common examples do we have? We can use emailing apps like for example when we use the gmail app data is already sent every other time whenever new email comes up we have a notification about the new email and we can be able to see it so you have the email server communicating with the client app and yet we also have things like payment systems for example payment processors like Stripe in their dashboards allow you to tap into the webhooks that they have available so you can be able to confirm that a particular payment has been made in your app whatever Stripe receives the payment from any of your customers. Now with this little information let us make some APIs or webhooks in one press and if you want to see more about APIs I have some in the descriptions right below and then if you want to make a webhook we are going to jump in the video now remember that you can subscribe to the channel if you find useful content from it you can like this video give it a thumbs up if you really like it and don't forget to leave a question for me in the comment section or just let me know whether this has been helpful otherwise let's jump into coding this now the first thing that I will ask you to do is actually open up an account with Intergrammat. Intergrammat is a nice tool that I've used on a couple of projects and it allows you to get different pieces of information and push them in any direction that you want to do it's a good automation product just like the way you would see things like Zappia and so on now they are not in any way sponsoring this video but it's a tool that I've enjoyed using and I think you would benefit from if you use the link on your screen or in the description I will be able to get a couple of cents off your sign up but let's sign in right now and after signing in you're going to get a dashboard like looks like this so I'll create a scenario this is going to be our external application that will receive our webhook I'm going to choose the webhook service that's part of Intergrammat I'll click continue and this will allow us to click on webhook and we're going to choose to have a custom webhook now once we do this we'll click add so that we can have a webhook of our own so I'll call this a wp plugin webhook I'll not restrict it to any ip address I'll click save and it's going to automatically give us a link that we can use here so I'll copy this to clipboard and I'm going to come to my editor of course I'll click okay here so I'll go inside my root folder of my website I'll go inside wp content I'll go inside plugins and I'm going to create a new folder and I'm going to call this plugins activation webhook and I'll drag and drop this inside my editor add it to my workspace and then the first thing I'll do is create a plugins activation webhook.php file and that's what our WordPress will use to trigger our plugins so I'll open this up with PHP scripts here and then I'll use my small snippet who will start plugin to allow us to add in a default plugin now I'm going to change the details that we have here so that we can begin working on our plugin and our plugin name will be plugins activation webhook as we have it here I'll change that to the my github URL where we're going to have this plugin seated and then I'll add in a description that this is a webhook from WordPress to send plugins activation and deactivation to external applications I'll add this version add a license I'll add a text domain so that we can do good translation of our plugin and then I'll add the basic security where I'm trying to make sure that this plugin is not triggered directly from any browser only by WordPress so I look for this abs path if it's defined if it's not then we execute die so the next thing that I'm going to do I'll go back to our webhook and I'm going to get this URL I'll copy that address from there and then I'm going to paste it here and the first thing I'll do is wrap this and save it as a variable and I'll call each URL just for now and of course add a semicolon and what we're going to do is we're going to push content to this webhook okay now the first thing that I will do is I'm going to use an add action which is default WordPress and it requires four different arguments it requires a tag and the tag that we shall use is save post and after save post we're going to use the function which I'll call take a press save post webhook and I'll add a priority here of 10 and they can pass in accepted arguments which are three now these three we can get from the documentation on WordPress and basically these are the post id the post object and updated which is a boolean telling us whether the post has just been updated or it has not so it's a true or false kind of situation but I don't want to use the updated for now I'll just use only the two arguments and those arguments will be our post object and of course the post id so I'll function add this name right here add our brackets and I'm going to pass in our post id and the post object so post id and then post object so these are the two that we need now the first thing that I will do is I'm going to use the wp remote post function of WordPress it requires a URL and it requires some arguments for each work now I'll push this URL that we had saved earlier so that we use it with our remote post and the only thing that will be left is for us to work out our arguments so in our arguments it's going to be an array because an array is expected of things and inside this array the only thing that I'm going to do is passing the body and I will say this body is going to be another array because I want to pass in particular things I want to say we want to pass a message and we're going to chain on this message to a message variable so I'll wrap this in quotes and then the only thing I need to work on is the message right here so our message is going to equal to two post I'll put a pound sign post and then I'm going to attach the post id so I'll say the post id the post with the number id has been created so I'll add another chain here it has been created with title and then I'll chain on one more thing I'll say the post and then get the post post title and that's all we need to do right now so we have our message we have a URL we have our arguments for the body we have a wp remote post now the only thing that's left is for us to test this so what I'm going to do is come back to our little server I'll open up the admin site by the way I'm using local by flywheel which is a good local server for you you download it install it and straight forward you begin using it so I'll go to the posts here and I'm going to add a new post and what I'm going to do is give this a title and say great article and of course I'll give it some gibberish if I want to and then I'll break that apart now before I hit publish or self draft what I'm going to do is come back to intergram mat because this webhook is not running directly all I'm going to do is say run once here then I'm going to come here and click publish and click publish here I'll go back to my intergram mat and you'll see that it has automatically received a one when I click on this to expand you're going to see that we have a bundle message it has the post ID of 153 has been created with the title great article now the reason I told this to run once is because this wasn't running all the while now if you have a paid service of this you can allow this to run and keep on tapping the different things that are coming over and over and over so this was waiting for information to come in and the moment that was ready right here the moment we saved our article that information was ready and it pushed it to the webhook that was received by the URL that we have in our code as this so that's a very simple webhook for you now of course you can make this as intricate as you want it to be maybe you want to monitor when plugins have been installed or deactivated but this is a very simple hook that we're using to self post you can make it more elaborate so I'm using a very simple hook in one press that taps a particular event and then it drives off information and send it sends it to a particular URL now of course you can build a good UI to be able to save the different URLs package this and make it a standard plugin you can do so much more than what I have showed you here now if you enjoyed this video don't forget to give it a thumbs up leave me a question in the comments below and just share it with your friend otherwise enjoy your day