Please note that all code examples on this site are provided for reference/guidance only and we cannot guarantee that they will always work as expected. Our support policy does not include assistance with modifying or debugging code from any code examples, and they may be changed or removed if we find they no longer work due to changes in our plugins.

A common request is to give a free package to newly signed-up users. This can be done with a small snippet added to a plugin like Code Snippets:

add_action( 'user_register', 'give_wcpl_user_package_on_registration' );

function give_wcpl_user_package_on_registration( $user_id ) {
	global $wpdb;

	$wpdb->insert(
		"{$wpdb->prefix}wcpl_user_packages",
		array(
			'user_id'          => $user_id,
			'product_id'       => 0,
			'package_count'    => 0,
			'package_duration' => 30,
			'package_limit'    => 1,
			'package_featured' => 0,
			'package_type'     => 'job_listing'
		)
	);
}

Optionally you may want to only give a free package to employers. If that’s the case, use this instead:

add_action( 'user_register', 'give_wcpl_user_package_on_registration' );

function give_wcpl_user_package_on_registration( $user_id ) {
	global $wpdb;
	
	if ( wpjm_check_user_role( 'employer', $user_id ) ) {
		$wpdb->insert(
			"{$wpdb->prefix}wcpl_user_packages",
			array(
				'user_id'          => $user_id,
				'product_id'       => 0,
				'package_count'    => 0,
				'package_duration' => 30,
				'package_limit'    => 1,
				'package_featured' => 0,
				'package_type'     => 'job_listing'
			)
		);
	}
}

function wpjm_check_user_role( $role, $user_id = null ) {
    if ( is_numeric( $user_id ) ) {
		$user = get_userdata( $user_id );
    } else {
        $user = wp_get_current_user();
    }
    if ( empty( $user ) ) {
		return false;
    }
    return in_array( $role, (array) $user->roles );
}