Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/c29c91d06df9b3367685ffae20c43fd5 to your computer and use it in GitHub Desktop.
Save Garconis/c29c91d06df9b3367685ffae20c43fd5 to your computer and use it in GitHub Desktop.
WordPress | Add WP Job Manager posts to Ultimate Member user profile tab
<?php
/** SHORTCODE TO LIST ALL JOBS OF A USER
--- Job Manager shortcode to display a list of "Jobs" (Campaigns) based on the User ID that is set
--- Usage: [campaign-type author="3"]
------ ... You can add additional parameters to the shortcode to override settings if desired
--- Note: Used within the custom Snippet for Ultimate Member tabs.
*/
add_shortcode( 'campaign-type', 'fs_job_type_shortcode' );
function fs_job_type_shortcode( $atts ) {
global $post;
//ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'post_type' => 'job_listing',
'order' => 'ASC',
'orderby' => 'date',
'posts_per_page' => -1,
'post_status' => 'publish',
'author' => get_current_user_id(),
), $atts ) );
// define query parameters based on attributes above
$options = array(
'post_type' => $post_type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts_per_page,
'author' => $author,
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) {
echo'<ul class="fs-job-listing job_listings">';
while ( $query->have_posts() ) : $query->the_post();
echo '<li class="job_listing type-job_listing">';
echo '<a href="'. get_permalink() .'">';
if ( has_post_thumbnail( $post->ID ) ) {
echo get_the_post_thumbnail( $post->ID, array('class' => 'company_logo') );
}
else {
echo '<img class="company_logo" src="/wp-content/plugins/wp-job-manager/assets/images/company.png">';
}
echo '<div class="position"><h3>'. get_the_title() .'</h3></div>';
echo '<ul class="meta">';
// list job type
$jobtype = get_the_term_list( $post->ID, 'job_listing_type', '', ', ' );
// strip the anchor links around each type
$jobtype = strip_tags( $jobtype );
echo '<li class="job-type">' . $jobtype . '</li>';
// if has expiration date set, then show it
if($post->_job_expires) {
echo '<li class="date"><i class="fa fa-calendar-times-o"></i> Expires: <strong>' . mysql2date('F j, Y', $post->_job_expires) . '</strong></li>';
}
echo '</ul>';
echo '</a>';
echo '</li>';
endwhile;
wp_reset_postdata();
echo '</ul>';
//$myvariable = ob_get_clean();
//return $myvariable;
}
else {
echo'<p class="profile-with-no-campaigns"><i class="fa fa-frown-o" aria-hidden="true"></i> Sorry, this Brand doesn\'t currently have any active Campaign listings</p>';
}
}
/** NOTES ABOUT WHAT IS HERE:
--- This is adding custom tabs to the Ultimate Member profile pages
--- 1) If the profile's role is for a Brand or Administrator, it displays a "My Campaigns" tab
--- 2) If the profile's role is for an Influencer or Administrator, it displays a "My Applications" tab
------ ... but ONLY if the currently logged in user is viewing their own profile
*/
/** ALSO:
---- 1) Currently the "My Campaigns" is outputting a custom shortcocde for WP Job Manager
------ ... It outputs only the profile's jobs (based on their user ID) that is currently being viewed
*/
/* First we need to extend main profile tabs */
add_filter('um_profile_tabs', 'add_custom_profile_tab', 1000 );
function add_custom_profile_tab( $tabs ) {
// UM spits out the user ID of the profile
$profile_id = um_profile_id();
// get user data of the user that belongs to the profile's user ID
$profile_user = get_user_by( 'id', $profile_id );
// get the user ID of the CURRENT user that is logged in
$current_user = get_current_user_id();
// check the roles
$allowed_roles_brand = array('um_brand', 'administrator');
$allowed_roles_influencer = array('um_influencer', 'administrator');
// check the roles that belong to the profile user. Are they a Brand?
if( array_intersect($allowed_roles_brand, $profile_user->roles ) ) {
$tabs['campaigns'] = array(
'name' => 'Our Campaigns',
'icon' => 'um-faicon-thumb-tack',
'custom' => true,
);
}
// check the roles that belong to the profile user. Are they an Influencer?
// and does the profile ID match the logged in user?
if( $profile_user->id === $current_user && array_intersect($allowed_roles_influencer, $profile_user->roles ) ) {
$tabs['applications'] = array(
'name' => 'My Applications',
'icon' => 'um-faicon-thumb-tack',
'custom' => true,
);
}
// return the tabs (and any of the ones above)
return $tabs;
}
/* Then we just have to add content to the CAMPAIGN tab using this action */
add_action('um_profile_content_campaigns_default', 'um_profile_content_campaigns_default');
function um_profile_content_campaigns_default( $args ) {
$profile_id = um_profile_id();
$profile_user = get_user_by( 'id', $profile_id );
if ( ! empty( $profile_user ) ) {
echo '<p>Currently viewing <strong>' . $profile_user->display_name . '</strong> Campaigns. Rather view <a href="/campaigns/"><strong>ALL</strong> Campaigns</a> instead?</p>';
}
echo do_shortcode('[campaign-type author="' . $profile_id . '"]');
}
/* Then we just have to add content to the APPLICATIONS tab using this action */
// based on the tab rules above, this only shows if they are an Influencer or Admin, AND if they are logged in
add_action('um_profile_content_applications_default', 'um_profile_content_applications_default');
function um_profile_content_applications_default( $args ) {
$profile_id = um_profile_id();
$profile_user = get_user_by( 'id', $profile_id );
// echo $profile_id;
if ( ! empty( $profile_user ) ) {
echo '<p>Currently viewing your submitted Applications. Don\'t worry, other users can\'t see this.</p>';
}
echo do_shortcode('[past_applications]');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment