I have a plugin that imports csv data into wordpress tables. It runs through the Wordpress dashboard where you input some details and click a button to execute it.
I've altered the plugin so that the input data is static(from the same csv every time) and is now all located in one php file. I want to schedule a Cron job to execute this script every hour or so.
I tried to set it up using cPanel and directly accessing the php file but it does not work(nothing is displayed). I believe this is because the plugin uses wordpress functions such as wp_insert_post.
How can i run this script, as if it were run through wordpress dashboard, as a scheduled event?
Note: the file also contains some javascript.
You’ve got to include 2 files to get access to admin side functions: First, wp-load.php. wp-load.php gets everything set up, and fires up wordpress. However, you are calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) – so when wp-load is called, you are not going to be in the admin section, and not going to get access to those functions. On the bright side, you also don’t have to deal with WordPress forcing you to login. Since you still need those admin functions, include wp-admin/admin-functions.php. This loads up the admin side and gives you access to the admin functions – and you are set to go
You should use wp_schedule_event. See in WordPress codex here : http://codex.wordpress.org/Function_Reference/wp_schedule_event. Use something like this:
register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');
function my_activation() {
wp_schedule_event( time(), 'hourly', 'my_hourly_event');
}
function do_this_hourly() {
// do something every hour
}
You can perfectly call a PHP file inside your do_this_hourly() function.
Accepted answer worked well for me. I modded this, and here are my findings. It is the case that you can use this solution outside of a plugin scenario. Basically, you can have your server run using cron jobs using native wordpress functionality as followed. You can create a file at the top level of your wordpress app and then include /wp-load.php; therein.
This loads in native wordpress and allows you to call a class that you can define as part of your must use plugins. Then your good to go regarding basic wordpress functionality, things like $wpdb and get_usermeta()
Now you can instantiate your class with $class_variable = new YourClass; and from there you can call class functions. The great part about this is that you can schedule cron jobs as you would normally on your server, and when you do you have your wordpress function running as it would inside wordpress. This means you don't need to maintain your cron job stuff as part of a plugin, which may or may not be useful depending on what your up to.
Related
I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.
I have created a snippet that takes specified code and minifies it, but I'm looking to turn this into a plugin to streamline the process. I'm looking to have the code minify when I save a file, and create a new file in that directory with a new name. I have this working as a snippet, but I don't want to be writing files every time the page is loaded.
Is there a way to check when a file (in the Files Tab/Assets) is saved in ModX, and then execute a plugin? I looked through the existing System Events, and didn't find anything.
Looks like you have 3 system events that may work for you: OnFileManagerUpload, OnFileEditFormPrerender & OnFileCreateFormPrerender. My guess would be OnFileManagerUpload.
Docs here:
http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onfilemanagerupload
Though it's important to note [the last time I checked] that even if a system event is listed in the manager and/or documented, they are not all implemented [i.e. won't work] - best to check on this in the modx forums or dig through the code to see if it actually exists.
Now, you are trying to do this from the manager or some front end form?
In the manager attaching a plugin to the OnFileManagerUpload hopefully works. In the front end you may have to attach your plugin to one of the page rendering events either read the file's time/date or append/prepend a timestamp within the file itself.
[or if you are using formit, you can probably use a custom hook to get the job done as well]
I want to use a PHP file to create posts for my Wordpress site by calling this kind of function:
wp_insert_post( $post, $wp_error );
This PHP file reads some info from a database (not the Wordpress database), perform some data handling, and then call wp_xxxxxx() functions to write data into the Wordpress database.
I was originally thinking to put this PHP in a subdomain, but then I think it should be in somewhere related to the Wordpress because Wordpress functions/database are used.
Where is the best place I should put this file? Do I need to include anything in this PHP file? and then I can execute this PHP file via a web browser, like http://xxx.xxx.xxx/yyy1/yyy2/zzz.php?
And this PHP file can be called like a cron job every 15 mins?
I found the solution that best fits my conditions:
Posting in wordpress database as a new post
Just like the first answer, I add the PHP in http://MyWordPressdomain.com/scripts and in the PHP header I add
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
I can now execute the PHP file from a web browser & from my site's cron job setting directly.
I am currently trying to write a couple of pages into my website that are not part of the wordpress site but I would like to be able to use the wordpress users. I have this working using the following code
require_once("../../../wp-load.php");
$current_user = wp_get_current_user();
Now I am able to use the $current_user variable for everything I need. However because I am includeing the wp-load.php file there is a lot of overhead that I really don't need.
My question is how can I get the current wordpress user without including wp-load?
I don't mind having to include a few extra files myself but I really don't need or want the entire wp enviroment to be set-up each time this page is called just so I can get the user.
What are you doing in those pages? I'm asking because, if you want to send some POST via ajax, you should check this.
If you don't use ajax, but you just need some pages where you can acces Wordpress functions, well somehow you must include wp-load because this file is loading Wordpress.
I suggest creating a file called page-custom-name.php in your theme folder, and publish an empty page with the exact title "Custom-name". Now when you'll be visiting www.yoursite/custom-name you will see that page, and you can get the current user info, or access other Wordpress functions.
Later edit:
This idea a partial solution: How about using hooks?
In your functions.php put these lines:
add_action('wp_login', 'aboutuser');
function aboutuser($username, $user)
{
$userObject = $user;
//find a solution to send this data to your url of your application
}
Basically, when users are logging in Wordpress, the aboutuser() function is executed, and it's get 2 params(in Wordpress 3.3; in earlier versions it gets only the username).
Now, when users are logging in we have acces to the wp user object. Maybe there is a way pass this data(a POST request) to your application and store it in a session.
I don't sure if it's possible though.
I have a Drupal website that I have integrated with a third party application. Currently I am required to execute 2-3 steps manually to ensure the new user account is integrated with my third party application. I can write a PHP application to execute these 2-3 steps. However I do not know how to execute this PHP file when a new user registers on Drupal.
This is done via the user hook. You can check for the different options from $op and do whatever you need to do. This needs to be done via a module, so read about how to create your own modules.
As you may know, Drupal permits to interact with its flow of operations via the so called "hooks". Whenever something relevant occurs, Drupal eventually fires these hook (they are functions, after all).
If you need to do something when a certain event occurs, you will create a module (third_party_integration.module, for example). In that module you'll write what you need to do when, for example, a new user is created.
In Drupal 7 the old hook_user() has been replaced with a whole set of more specific hooks: see http://api.drupal.org/api/search/7/hook_user_
The hook function must be called after your module name: for example: third_party_integration_user_insert()
The .module is just a plain PHP file, so you can add everything you need inside it (along with the actual hook function, of course).
Just remember to name EVERY functions in your module prepending your module name (with or without an prefixed _). This is just to play nice with other modules.