Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active November 5, 2022 21:52
Show Gist options
  • Save patrickfreitasdev/02f0b0b16437fa246c85157fdc70af3e to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/02f0b0b16437fa246c85157fdc70af3e to your computer and use it in GitHub Desktop.
<?php
/**
** Script to assign post to new user when Post Data field and user registration are used together.
**/
if (!defined('ABSPATH'))
{
exit;
}
elseif (defined('WP_CLI') && WP_CLI)
{
return;
}
add_action( 'plugins_loaded', 'wpmudev_assing_new_post_to_new_user_id', 100 );
function wpmudev_assing_new_post_to_new_user_id() {
if ( class_exists( 'Forminator' ) ) {
class WPMUDEV_Dynamically_assign_id {
private $new_post;
public function __construct() {
add_filter( 'forminator_cform_user_registered', array( $this, 'handle_user_id' ) , 10, 1 );
add_action( 'forminator_post_data_field_post_saved', array( $this, 'post_update_handler' ) , 20, 1 );
}
public function handle_user_id( $user_id ) {
$post_id = $this->new_post;
if( ! empty( $post_id ) ) {
$postarr = ['ID' => $post_id, 'post_author' => $user_id ];
wp_update_post($postarr);
}
}
public function post_update_handler( $post_id ) {
$this->new_post = $post_id;
}
}
$run = new WPMUDEV_Dynamically_assign_id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment