I am creating a new wordpress plugins and I want to use the cronjob system from wordpress. But I get a little bit confused: Everytime the cronjob gets executed I want to run a special function. But the action will not be done. No post will be inserted.
The cronjob itselfs works correct. Maybe I do something wrong. (I manually tested the cron by opening the url wp-cron.php?immo_import_check_import_folders, and it shows me blank page. (Seems this is normal)
Code:
register_activation_hook(__FILE__, 'immo_import_activation');
register_deactivation_hook(__FILE__, 'immo_import_deactivation');
function immo_import_activation() {
wp_schedule_event( time(), 'minutely', 'immo_import_check_import_folders' );
add_action( 'immo_import_check_import_folders', 'immo_import_check_import_folders2' );
}
function immo_import_deactivation() {
wp_clear_scheduled_hook('immo_import_check_import_folders');
}
function immo_import_check_import_folders2() {
$my_post = array(
'post_title' => wp_strip_all_tags( 'Test' ),
'post_content' => 'test .......',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
wp_insert_post($my_post);
}
Move your add_action out of immo_import_activation function. If you need one time cron action, look into wp_schedule_single_event
Related
I am using the Ultimate Member plugin and trying to trigger things after the registration form is filled in successfully.
As a test, I am simply creating a new post if the hook is run: um_registration_complete.
https://docs.ultimatemember.com/article/1234-umregistrationcomplete
function my_registration_complete( $user_id, $args ) {
// Create post object
$my_post = array(
'post_title' => 'function working',
'post_content' => 'hello world.',
'post_status' => 'publish',
'post_author' => 1,
);
// Insert the post into the database
wp_insert_post( $my_post );
}
add_action( 'um_registration_complete', 'my_registration_complete', 10, 2 );
Nothing happens after a successful registration. No post.
I tried adding die(); as a test to break the site on purpose if the hook runs, still nothing.
How best to debug this issue, I see nothing wrong with how I am using the hook but still it's not running.
Figured out my issue, and something I should have mentioned originally too. I am using the roots.io framework.
Functions etc namespaced, my add_action function name was not namespaced. Here is the working line:
add_action( 'um_registration_complete', __NAMESPACE__ . '\\my_registration_complete', 10, 2 );
I have a wordpress site where users can upload products. The default product status is pending,however I want to change it to published if it has passed 12 hours after the post_time.
I am using woocommerce.
I´ve tried to use wp_insert_post_data:
function reset_post_date_wpse_121565($data,$postarr) {
if ($data['post_type'] == 'product'){
$data['post_date'] = '2018-06-03 22:50:00';
}
return $data;
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565');
But it sets the post to published, what can I do to accomplish this auto update?
You can try WordPress Cron run to achieve this,
function myprefix_custom_cron_schedule( $schedules ) {
$schedules['every_tewlve_hours'] = array(
'interval' => 43200, // Every 12 hours
'display' => __( 'Every 12 hours' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
wp_schedule_event( time(), 'every_tewlve_hours', 'myprefix_cron_hook' );
}
///Hook into that action that'll fire every six hours
add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
//create your function, that runs on cron
function myprefix_cron_function() {
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'post_status' => array( 'pending' ),
'author' => $userid
);
$pending_posts = get_posts( $args );
//update status code
}
Hope this will helps you.
For more example visit,
wp cron demo
I want this plugin to only create the post with title "Test Post" if it doesn't already exist. For some reason it always creates the post when I activate the plugin. Seems get_page_by_title('Test Post') is always false even after the post has been created. Can someone please explain why?
<?php
/* Plugin Name: Create Test Post */
function create_post(){
if(get_page_by_title('Test Post')){
return;
}
$my_post = array(
'post_title' => 'Test Post',
'post_content' => 'This is a test post',
'post_status' => 'publish'
);
wp_insert_post($my_post);
} // end create_post
register_activation_hook(__FILE__, create_post);
The function get_page_by_title() can only be used on pages, not posts without setting the post type argument.
This will set the appropriate argument for that function. If 'post' is not working try using 'posts'.
The full documentation for this function is found here.
https://developer.wordpress.org/reference/functions/get_page_by_title/
function create_post(){
$prev_post = get_page_by_title('Test Post', 'OBJECT', 'post');
if ($prev_post != NULL) {
$prev_post_status = get_post_status( $prev_post->ID );
}
if (!isset($prev_post_status) || !in_array($prev_post_status, array('trash', 'published')) ) {
$my_post = array(
'post_title' => 'Test Post',
'post_content' => 'This is a test post',
'post_status' => 'publish'
);
wp_insert_post($my_post);
}
} // end create_post
register_activation_hook(__FILE__, create_post);
I'm building a simple WP plugin that's supposed to create a new post and save some meta for the post. I created a function with the functionality and for the time being I hooked it to the 'init' event to check if it works.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
$inserted_post_id = wp_insert_post($my_post);
if($inserted_post_id != 0) {
add_post_meta($inserted_post_id, 'some-key', 'some-value');
add_post_meta($inserted_post_id, 'some-other-key', 'some-other-value');
echo 'SUCCESS';
} else {
echo 'ERROR';
}
}
Now, whenever I reload the admin page, I get the 'SUCCESS' message echoed out, and I also get 4-6 new post called 'Some Post Title', and 4-6*2 new entries in the postmeta table. The 'SUCCESS' message echoes only once, meaning the function runs only once, still I get the data inserted to the database multiple times. What am I doing wrong?
Check before going to insert new post if already exists or not. So get_page_by_title('Some Post Title') == false then only insert new post.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title('Some Post Title','OBJECT','custom-post-type') == NULL )
$my_post= array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
wp_insert_post( $my_post );
}
I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.
If I use wp_insert_post() function, it's creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.
I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks
The following used as Must Use plugin does the job:
<?php
/* Plugin Name: New Post on Site Creation */
add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
restore_current_blog();
}