WP Ultimate Member hook/function not running - wordpress

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 );

Related

wp_cronjob should run a special function

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

Infinite loop error with publish_post and wp_insert_post

I got the infinite loop error. I require to insert the post in German language when the post is created for default English language.
I used publish_post action hook for catch the english posting event. But the publish_post hook is also executed by wp_insert_post() fucntion while creating the German post. So the infiniter error occured. Could anyone help ? Thank you. Below is the code i have used.
add_action( 'publish_post', 'save_in_all_sites' );
function save_in_all_sites( $post_id ){
global $sitepress;
$my_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => $post_status
);
$def_trid = $sitepress->get_element_trid($post_id);
$ru_post_id1 = wp_insert_post( $my_post );
// insert the post in German language
$sitepress->set_element_language_details($ru_post_id1, 'post_post', $def_trid, 'de');
}
It should work to remove the hook right before you wp_insert_post and then add it back right after.
Example
remove_action( 'publish_post', 'save_in_all_sites' );
$ru_post_id1 = wp_insert_post( $my_post );
add_action( 'publish_post', 'save_in_all_sites' );
where is the code for english language ? is it $def_trid ? if yes then you are setting both english and german languages to element_language_details so when you get it, it returns you everything that is in english and german language trying setting these up separately with separate columns in your database too.

Issue on wpdb queries, wp_insert_post. The function adds duplicate post

Anyone encountered this issue?
I have created a custom php script which adds a new post by using wp_insert_post() but everytime I run the code it creates a 2 new post where in it should be only be one, its like it runs twice, so I tested another function using wpdb->query and its also the same its really weird. Please help I dont know what causes and how to fix this issue.
Thank you very much.
Here is a sample code i did just to test the issue and it still creates 2 identical post
function testtest(){
$ads_data = array(
'post_title' => "test",
'post_content' => "test",
'post_status' => 'publish',
'post_type' => 'ads',
//'post_author' => $user_id
);
// Insert the post into the database
$ads_id = wp_insert_post( $ads_data );
}add_shortcode('testtest','testtest');
SOLVED
I have figured it out now, whats causing the issue is on my header.php it is this line of code:
<link rel="icon" type="image/png" href="<?php echo esc_url( get_theme_mod( 'favicon' ) ); ?>" />
I find it really weird, i have no idea why that line of code is causing the issue, anyway thanks everyone!
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('test','OBJECT','ads') == NULL )
$my_post= array(
'post_title' => 'test',
'post_name' => 'test',
'post_type' => 'ads',
'post_status' => 'publish'
);
wp_insert_post( $my_post );
}

Add a page to wordpress

I'm currently in the process of building my own WordPress plugin.So i need to create a new page(or post) that will be automatically added to word press when the plugin is activated.And this will be removed when plugin is deactivated.Content in the page is what content i am typing in the plugin.
HOW CAN I DO THAT?
you can use wp_insert_post function to create page or post check this http://codex.wordpress.org/Function_Reference/wp_insert_post
ex.
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post);
you can use $post_id withing add_post_meta or update_post_meta or use post_meta varible to install and uninstall the page.
Something like this? There's some code referenced in the linked post for dealing with page creation. There's also the codex.

Creating a post using PHP in one of the blogs in WP MU

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();
}

Resources