How to create a post in WordPress using PHP

You may, on occasion, find that creating a post manually using the WordPress admin dashboard may not be sufficient; particularly when you have a lot of posts to create, or you are developing a plugin that needs to create some posts for whatever reason. Well, in that case you need to script your post with PHP. Here is a simple example of how that can be accomplished. The following snippet might be placed in the “activate()” function of a plugin for example, where the post gets created upon plugin activation.


Note: the snippet is included as a link to text because the WP text editor wants to either lose the formatting or execute the code if copied as is…