WordPress.org

Ready to get started?Download WordPress

Forums

Auto creation of posts using wp_insert_post function (5 posts)

  1. clockticks
    Member
    Posted 1 year ago #

    So I just found the function wp_insert_post. The thing is, I know little PHP and not sure how I can start to tie this into what I want to do.

    I have a python script that inserts a list of urls and titles each day into a file on my server. I want to be able to insert these urls and titles into the mysql db a couple times a day.

    It looks like I need to somehow tie this into using the wp_insert_post function in PHP somehow. What I think I need to do is have a PHP script or something with that function read from this file that has my data, I'm not totally sure.

    How should I go about attacking this, can the community please provide me with some guidance as to how they would go about doing this?

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    You'd want to set up a cron job that runs a function. Example below:

    add_action('my_action', 'Actual_Function_That_Runs_Insert');
    
    function my_activation() {
    	if ( !wp_next_scheduled( 'my_action' ) ) {
    		wp_schedule_event( time(), 'daily', 'my_action');
    	}
    }
    add_action('wp', 'my_activation');
    
    function Actual_Function_That_Runs_Insert()
    {
    // run insert
    }

    This will set up a cron job to runs daily in WordPress. You can't do them the "standard" way in cpanel (or actual linux if you're boss) because of the mod rewrite in the htaccess file, or at least I never have been able to get them to work. Cron View is a handy plugin to see if your job was scheduled successfully.

  3. clockticks
    Member
    Posted 1 year ago #

    I really can't just run it in cron? I'm not using a control panel at all other than logging into the wp admin page, it's a centos box I have root on.

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    If you can get it to work please post in this thread and let me know what you did. I've written a million cron jobs but have never gotten one to work on a WP site. Considering the amount of plugins out there for managing cron jobs in WordPress, I don't think we're the first people to run into this problem.

  5. fonglh
    Member
    Posted 1 year ago #

    I haven't tried it, but I don't see why you can't run any script you want that inserts entries into the wp_posts table with cron.

    Andrew's method would be the more WordPress way.

    Note that cron and wp-cron are not the same.

Topic Closed

This topic has been closed to new replies.

About this Topic