Support » Plugin: WP Job Manager » Need Help Creating Fully Custom Job Slug/Permalinks

  • Resolved Mark

    (@markob17)


    Hello,

    I’ve been trying all day to create completely custom slug/permalinks for jobs but have not been successful, wondering if anyone with coding knowledge can lend a hand? Would really appreciate it.

    Basically I want to use job post custom field values to create a custom slug/permalink for jobs. For example – http://www.mysite.com/job/custom-permalink-using-custom-fields/

    The base code (found 99% of it on WP Job Manager site) I am trying to use is:

    
    function custom_job_post_type_link( $post_id, $post ) {
    
    $jobtitle = get_post_meta($post->ID, '_job_title', true);
    $jobcity = get_post_meta($post->ID, 'geolocation_city', true);
    $jobstate = get_post_meta($post->ID, 'geolocation_state_long', true);
    $jobcompany = get_post_meta($post->ID, '_company_name', true);
    
        	// don't add the id if it's already part of the slug
        	$permalink = $post->post_name;
        	if ( strpos( $permalink, strval( $post_id ) ) ) {
        		return;
        	}
        	
        	// unhook this function to prevent infinite looping
        	remove_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
    
     	// add the id to the slug
        	$permalink .= '-' . $post_id;
        
        	// update the post slug
        	wp_update_post( array(
            	'ID' => $post_id,
            	'post_name' => $permalink
        	));
    
        	// re-hook this function
        	add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
    }
    add_action( 'save_post_job_listing', 'custom_job_post_type_link', 10, 2 );
    

    I tried all sorts of combinations of code but nothing seems to work. My coding skills are one step above copy + paste. Any insight would be highly appreciated! I know this is probably simpler than I am making it out to be.

    Regards,

    mark

Viewing 4 replies - 1 through 4 (of 4 total)
  • You’re going about that all wrong, you don’t need to update it yourself, just filter the data being passed by the core plugin and update the post_name value in the array.

    I actually integrated this exact feature into my WP Job Manager Visibility plugin (to set custom permalink slug from custom fields), I’ll be nice and provide you with the PHP documentation block and very basic example of how I do this in my Visibility addon plugin.

    
    // Priority set to 9999999 to make sure this is last hook ran
    add_filter( 'submit_job_form_save_job_data', 'frontend_set_permalink', 9999999, 5 );
    
    /**
     * Customize Permalink Structure
     *
     * This method is called to format the slug to use for the post permalink based on
     * configured structure in settings.
     *
     * @since 1.4.1
     *
     * @param $data             Array of data to pass to wp_insert_post or wp_update_post (post_title, etc)
     * @param $post_title       Same value as key post_title in $data array
     * @param $post_content     Same value as key post_content in $data array
     * @param $status           Normally would be 'preview' by default, unless it's an update to the listing
     * @param $values           Array of values from the submit listing page
     *
     * @return \Array
     */
    function frontend_set_permalink( $data, $post_title, $post_content, $status, $values ) {
    
    	if ( ! is_array( $data ) || ! array_key_exists( 'post_type', $data ) ) {
    		return $data;
    	}
    
    	$data['post_name'] = 'something custom';
    
    	return $data;
    }
    

    If you wanted to, you could buy a license for the plugin and just go through the code base to see exactly how I do it, i don’t mind explaining where it’s at to you:
    http://plugins.smyl.es/wp-job-manager-visibility/

    • This reply was modified 3 years, 10 months ago by tripflex. Reason: Had admin filter not frontend one

    Couple things to point out too, when you have a WP_Post object, you can directly call meta keys of the post and it will return a value if there is one set, so you don’t need to use get_post_meta if you have a WP_Post object available.

    Basically this:
    get_post_meta( $post->ID, '_company_name', true );

    is the same as this:
    $post->_company_name;

    So it could be written like:
    $jobcompany = $post->_company_name;

    But as i’m sure you can assume from that, you don’t really need to set it to a variable since it’s already available directly through the $post object.

    strpos( $permalink, strval( $post_id ) )

    Can also be written like this using Type Casting (which is about 6x faster):
    strpos( $permalink, (string) $post_id )

    And when using strpos() you should always check that it’s equal to false as the first position is 0 not 1, so there’s a chance it will return 0 which in that if statement would be considered false.

    This would be the best way to write that if statement:

    
    if ( strpos( $permalink, (string) $post_id ) === false ) {
        return;
    }
    
    • This reply was modified 3 years, 10 months ago by tripflex.

    You also need to have code to handle it when saving from the admin area, so this is your code tweaked to work for the admin area:

    
    <?php
    
    // Changed to <code>job_manager_save_job_listing</code> action, and set to priority 30 to run after everything else
    add_action( 'job_manager_save_job_listing', 'custom_admin_job_post_type_link', 30, 2 );
    
    function custom_admin_job_post_type_link( $post_id, $post ) {
    
    	// don't add the id if it's already part of the slug
    	$permalink = $post->post_name;
    	if ( strpos( $permalink, (string) $post_id ) === false ) {
    		return;
    	}
    
    	// unhook this function to prevent infinite looping
    	remove_action( 'job_manager_save_job_listing', 'custom_job_post_type_link', 30, 2 );
    
    	// add the id to the slug
    	$permalink .= '-' . $post_id;
    	
    	// update the post slug
    	wp_update_post( array(
    		                'ID'        => $post_id,
    		                'post_name' => $permalink
    	                ) );
    
    	// re-hook this function
    	add_action( 'job_manager_save_job_listing', 'custom_job_post_type_link', 30, 2 );
    }
    
    
    Thread Starter Mark

    (@markob17)

    Wow, this is way too much information for my brain. Really appreciate your insight. I may just have to leave the links to default or buy your plugin to make it easier. I try to avoid more plugins if possible but I think if this is something I want to do, will have to either buy your plugin or hire a wordpress dev to create me the entire function from scratch. Thanks again for sharing the insight and providing a link to your plugin.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need Help Creating Fully Custom Job Slug/Permalinks’ is closed to new replies.