I think this is going to be difficult to explain, but I hope someone will understand. 😉
I’m building a plugin that queries data from a non-Wordpress database (a forum) and inserts it into WordPress as custom posts. Thanks to the help of bcworkz in this topic (Thx!) I managed to create a function that does this and scheduled an event that uses this function once every hour to import any new posts created on the forum or update any edited posts. It’s alsp assigning different categories to posts from different forum sections – so, I can say I’m quite happy with my first WordPress plugin, but obviously I want it to do more. 😉
What I want to do is to use WordPress options to change what the plugin does. I want it to do three things and I need to control some parts of them. Here’s how it should work:
1. A scheduled event will import/update ALL posts from SELECTED forum sections once a day.
2. A second scheduled event will import/update a SELECTED NUMBER of latest posts from SELECTED forum sections once every hour.
3. A form will let me execute the import function to update ONE SELECTED post (let’s say in case it’s older than the posts which are updated every hour).
Scheduling events isn’t a problem for me, neither is setting options – I already created options panel and I’m able to save options into WordPress database. What I don’t know is how to pass these options into my function depending on which event is fired. I still consider myself a newbie in PHP, so maybe the solution is simple and I just can’t see it. What I think I have to do is to use variables in the WHERE clause of SQL SELECT statement of my import function. Right now it looks like this:
WHERE forums.id IN (61, 62, 63) ORDER BY topic.id DESC LIMIT 5
I think in case of my daily event it should look like this:
WHERE forums.id IN ($forum_ids) ORDER BY topic.id DESC
This will let me change or add forum sections without editing the code if my forum would have to be reorganized somehow in the future.
And in my hourly event it should look like this:
WHERE forums.id IN ($forum_ids) ORDER BY topic.id DESC $limit
So I could change the number of updated posts.
The third case is different and it’s not the WHERE clause that is the biggest problem – it will be one topic.id. The problem is it appears I can’t use the Setting API to execute a function. I would have to connect a form in my admin panel to my import function so that when I type in the ID and hit the submit button, the function is executed – but again, I don’t know how to do it.
So in short, I need to know how to pass different number of options (one in case of the daily event, two in case of the hourly event and again one in the third case) and how to do it. Unless the whole idea is wrong, and there is a better way. I also need to know how to create the form that executes my plugins function.
I’ll be grateful for any advice.
- The topic ‘Using one function for two scheduled events and a form’ is closed to new replies.