Wordpress Cron Job Custom Hook Not Working - wordpress

I am trying to run a simple cron job using a custom hook but it does not run. It get's scheduled (viewing via wp cron plugin) but doesn't run. If I change the hook out to wp_loaded for example it runs fine. This is in a themes functions.php file:
// Schedule event.
if (!wp_next_scheduled ('my_hourly_event')) {
wp_schedule_event(time(), 'hourly', 'my_hourly_event', array(), true);
}
add_action('my_hourly_event', 'do_this_hourly', 10); // Works with action being 'wp_loaded'
function do_this_hourly() {
error_log("test"); // Not working.
$log = __DIR__ . '/error_log.txt';
file_put_contents($log, "Response: \n", FILE_APPEND); // Works with making action 'wp_loaded'
}
I have a few other websites using similiar code with custom hooks and they work no problem. What am I doing wrong here? Did a wordpress update break cron jobs with custom hooks? I am on wordpress version 5.7.1

Try with init hook.
function schedule_my_cron(){
wp_schedule_event(time(), 'hourly', 'my_hourly_event', array(), true);
}
if(!wp_next_scheduled('my_hourly_event',$args)){
add_action('init', 'schedule_my_cron');
}
add_action('my_hourly_event', 'do_this_hourly', 10); // Works with action being 'wp_loaded'
function do_this_hourly() {
error_log("test"); // Not working.
$log = __DIR__ . '/error_log.txt';
file_put_contents($log, "Response: \n", FILE_APPEND); // Works with making action 'wp_loaded'
}

Related

admin_init hook not working as expected (cannot write to log, cannot add actions)

I'm trying to add some actions once after plugin activation. I found out that this should be done more or less like so:
register_activation_hook( __FILE__, 'activation_function' );
add_action('admin_init', 'after_activation_function');
function activation_function(){
add_option( 'activated_plugin_xyz', 'plugin xyz activated' ); //option is added to database
}
function after_activation_function(){
if (is_admin() && get_option ('activated_plugin_xyz') == 'plugin xyz activated'){
//do some things
wp_schedule_event(time(), 'daily', 'some_cron'); //cron event is added
add_action('wp_login', 'xyz_login_action');
add_action('some_cron', 'xyz_cron_job'); //cron job is correctly hooked
//delete_option('activated_plugin_xyz');
}
error_log("nothing in debug.log log...");
echo ("echoing works");
//wp_die('dying works');
}
function xyz_cron_job(){
error_log('cron job not logging anything...'); //nothing in log...
}
I can see the cron job in wp crontrol, the function xyz_cron_job is hooked, but when I trigger it manually nothing is written to the log. xyz_login_action also seems not to work.
If I do other things in xyz_cron_job they seem to have no effect as well...
after_activation_function is called bcs dying works if I uncomment it. Can anybody help me? It seems to me like I am missing something fundamental...
fixed it:
actions have to be added outside after_activation_function()
(I guess actions have to be hooked every time wp loads and if hooking happens in after_activation_function() the hook runs before the action is hooked)
working code looks like this:
register_activation_hook( __FILE__, 'activation_function' );
add_action('admin_init', 'after_activation_function');
//hook outside other functions
add_action('some_cron', 'xyz_cron_job');
add_action('wp_login', 'xyz_login_action');
function activation_function(){
add_option( 'activated_plugin_xyz', 'plugin xyz activated' ); //option is added to database
}
function after_activation_function(){
if (is_admin() && get_option ('activated_plugin_xyz') == 'plugin xyz activated'){
//do some things
wp_schedule_event(time(), 'daily', 'some_cron');
delete_option('activated_plugin_xyz');
}
}
function xyz_cron_job(){
error_log('logging works');
}

Wordpress: wp_set_object_terms() not work from included php

I use wp_set_object_terms to set terms for user custom taxonomy, this function only works from the plugin main php file, but not from included php file.
for example this works wp-content/plugins/my_lugin/my_plugin.php:
function set_user_term_a()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_a');
but if function included from another php it not work
wp-content/plugins/my_lugin/included_file.php:
echo "the file was successfully included"; // ok
function set_user_term_b()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_b');
wp-content/plugins/my_lugin/my_plugin.php:
include_once("included_file.php");
Any ideas what wrong?

Admin-side hooks don't work (WordPress)

I want to send an email whenever a file is attached to a certain CPT, however I can't make add_attachment hook work. In fact I can't seem to make any dashboard hook (such as post_updated) work. The code below does nothing whenever a file is attached to a post or post gets updated:
add_action( 'add_attachment', 'goldorak' );
add_action( 'post_updated', 'goldorak' );
function goldorak() {
echo 'Fired!';
echo "<script>alert('Fired!');</script>";
}
Note: my attachment is a file field created with Advanced Custom Fields plugin.
I am not sure ACF fires the same actions as the normal wordpress. Here is the ACF version of your code:
add_action( 'acf/save_post', 'goldorak', 15 ); // The saving is done with priority 10, so 15 is after the save to DB, 5 before it.
function goldorak() {
die('test');
}
But in your case, the hook acf/update_value/type=file would simplify your task:
add_action('acf/update_value', 'acf_hook_update_value', 1, 3);
function acf_hook_update_value($new_value, $post_id, $field_options) {
$key = $field_options['key']; // internal key name
$name = $field_options['name']; // pretty name
$old_value = get_field($key, $this->post_id, false);
$new_value = stripslashes($new_value);
if ($new_value != $old_value) {
die('test'); // Do something ...
}
}

enqueue script in wordpress plugin for specific hook

When custom php function is called, need to register and enqueue script from within that called function. Then the whole thing gets added to wp_footer hook.
the echoed div in code below shows up in the developer tool, but the script is not showing or even giving any errors, i.e.- if this were an issue with the file path, then there would be resource error, yes? Any comments as to why there wouldn't be an error in loading the script?
The code:
if(get_option('show_content')) {
function add_time() {
echo '<div id="txt">' . '</div>';
// add script tut pro word plugin dev ch12.3
function py_enqueue_script () {
wp_register_script( 'timescript', plugin_url('../time.js', __FILE__));
wp_enqueue_script( 'timescript');
} // end py_enqueue_script
add_action('wp_enqueue_scripts', py_enqueue_script);
} // end show add_time
add_action("wp_footer",add_time);
} // end if
Try this (spotted some syntax errors):
if(get_option('show_content')) {
function add_time() {
echo '<div id="txt">' . '</div>';
// add script tut pro word plugin dev ch12.3
function py_enqueue_script () {
wp_register_script( 'timescript', plugins_url('../time.js', __FILE__), false, null, false));
wp_enqueue_script( 'timescript');
} // end py_enqueue_script
add_action('wp_enqueue_scripts', 'py_enqueue_script');
} // end show add_time
add_action('wp_footer', 'add_time');
} // end if
Brackets on add_action custom action (i.E. 'add_time' instead of add_time).
Plugins URL Function goes plugins_url not plugin_url
Some very good additional article on enqueueing scripts: http://wp.tutsplus.com/tutorials/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and-plugins/
Some additional info on plugins_url:
http://codex.wordpress.org/Function_Reference/plugins_url

Why won't WordPress load thickbox and media-upload scripts?

I'm working on a plugin that uses thickbox and media-upload to set some images. Neither will load using this code:
function add_my_files() {
echo 'happy happy happy';
wp_register_style( 'adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style( 'adminstyles' );
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs',plugins_url('/js/slider.js', __FILE__),array('media-upload','thickbox'),'',true);
wp_enqueue_script('hdjs');
}
add_action( 'admin_init', 'add_my_files' );
my css and js files load but not thickbox and media-upload.
Thanks
The correct hook to include your asset files in WP is admin_enqueue_scripts:
NOTE: I recommend you too use get_current_screen (see is_my_admin_screen() definition below) to just include your js/css files when you actually needed.
add_action('admin_enqueue_scripts', 'add_my_files');
function add_my_files()
{
/*
* a good WP citizen only loads
* their javascript/css where it is needed
*/
if ( ! is_my_admin_screen()) // defined below
return;
wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__));
wp_enqueue_style('adminstyles');
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('hdjs', plugins_url('/js/slider.js', __FILE__), array('media-upload', 'thickbox'), '', true);
wp_enqueue_script('hdjs');
}
function is_my_admin_screen()
{
$screen = get_current_screen();
if (is_object($screen) && $screen->id == 'my_plugin_page_id') // var_dump($screen->id); find your own id
return true;
else
return false;
}
ref: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
ref: http://codex.wordpress.org/Function_Reference/get_current_screen
Besides hopefully you are using a class to wrap all your plugin or you will have worse problems than this.
Please feedback. I am very interested in this issue because WP plugins puts food and beers on my table.

Resources