Search code examples
phpwordpresswp-api

How to add posts using own plugin in wordpress


Creating a wordpress plugin and need this plugin to automatically add posts to wordpress when it installs. What is the better solution to do that?


Solution

  • You should use the register_activation_hook to carry out any custom functions (like adding posts) after the plugin is initialised.

     function myplugin_activate() {
    
         // Add your posts here...
     }
     register_activation_hook( __FILE__, 'myplugin_activate' );
    

    That way, once the plugin is installed, and activated, myplugin_activate() will fire, and carry out any logic you put there.

    See: https://codex.wordpress.org/Function_Reference/register_activation_hook for more information.