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:
<?php
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, // This should be set to the ID of a package in WooCommerce if you want it to show a package name!
'package_count' => 0, // Leave
'package_duration' => 30, // How long job listings last
'package_limit' => 1, // How many jobs can be posted for free
'package_featured' => 0, // 0 = not featured
'package_type' => 'job_listing'
)
);
}
Optionally you may want to only give a free package to employers. If that’s the case, use this instead:
<?php
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, // This should be set to the ID of a package in WooCommerce if you want it to show a package name!
'package_count' => 0, // Leave
'package_duration' => 30, // How long job listings last
'package_limit' => 1, // How many jobs can be posted for free
'package_featured' => 0, // 0 = not featured
'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 );
}
If you notice any issues after adding the code, trying removing the first line with the php tag inside it.