0

I have a code that fetch data from an API and add the data as posts in Wordpress. It's suppose to run hourly.

I don't understand where i place this script in wordpress?

This is the script, it's a php class with a hourly trigger.

class MND_News_Importer
{
    private function _schedule_import()
        {
            // If the schedule isn't set
            if ( !wp_next_scheduled( self::CRON_NAME ) )
            {
                // Use the built in function wp_schedule event to run the function every hour
                wp_schedule_event( time(), 'hourly', self::CRON_NAME );
            }
        }
}

Full code can be found here: http://webbgaraget.se/2014/05/28/importera-nyheter-fran-mynewsdesk-till-wordpress-del-1/

Where am i suppose to place this code?

3
  • You can make it a template & assign that template to any page of wordpress & put that page URL in your cron job command. Commented Jun 17, 2015 at 10:46
  • create a new file inside a plugin folder mynewsdesk-importer.php and paste the full code. It will create a plugin for wordpress. Now go to admin and activate the plugin and you are done. Commented Jun 17, 2015 at 10:49
  • So if i run this page once with the plugin activated, will the wp_schedule_event( time(), 'hourly', self::CRON_NAME ); keep running every hour forward? Commented Jun 17, 2015 at 10:59

1 Answer 1

1

You can add this class into function.php file in WordPress.

function.php location wp-content/themes/(activeTheme)/function.php

Sign up to request clarification or add additional context in comments.

1 Comment

Can i just add the whole class? or do i need to init it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.