Support » Plugin: Job Manager » Useful but one line of code could make it much better!

  • Very handy plugin for quickly getting job application functionality up and running.

    I would totally give this 5 stars if support for the WP REST API were included when registering the custom post types. I have to manually add this in to each install and do it again if/when the plugin updates which is really not ideal. Fortunately I am only using this in development at the moment but really hoping the author can send this out with the next update. I will gladly add that last star in if/when support for the WP REST API is added.

    Either way, thanks for your hard work.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Thomas Townsend

    (@smb-dev)

    Craig..how about emailing me with the specifics of what you need…if you have a suggestion on how where to add this and would like to participate in Beta testing it I am open ?

    Please send email to support at wp-jobmanager.com

    Thanks for your support and review.

    Hey Thomas,

    If you install the latest version of your plugin along with the WP REST API (which is already partially merged into core as a feature plugin), you are unable to access any data from the custom post types that this plugin registers.

    With both plugins installed, visit yourdomain.com/wp-json/wp/v2/jobman_job and you will see:

    {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

    register_post_type() takes a new argument show_in_rest which will expose some of the data.

    So if I update the register_post_type() call in jobman_page_taxonomy_setup() from
    register_post_type( 'jobman_job', array( 'exclude_from_search' => false, 'public' => true, 'show_ui' => false, 'singular_name' => __( 'Job', 'jobman' ), 'label' => __( 'Jobs', 'jobman' ) ) );
    to
    register_post_type( 'jobman_job', array( 'show_in_rest' => true, 'exclude_from_search' => false, 'public' => true, 'show_ui' => false, 'singular_name' => __( 'Job', 'jobman' ), 'label' => __( 'Jobs', 'jobman' ) ) );

    I am then able to access some of the data – I installed the plugin on a live site with that line updated and as you can see here http://dakota-code.com/wp-json/wp/v2/jobman_job.

    I emphasized “some” intentionally because the “one line of code” I suppose is a bit exaggerated if you want to go all the way with REST API support. In order to expose all of the fields in a job, you would need to use register_rest_field() hooked to rest_api_init see: http://v2.wp-api.org/extending/modifying/#examples for an example.

    Is this plugin on Github somewhere? I’d be happy to send a PR with these updates so you can test it out.

    So I played around with this a little more and I am now exposing the Job Information field (this was not available when I replied above) using the following code:

    add_action( 'rest_api_init', 'slug_register_job_content' );
    function slug_register_job_content() {
    	register_rest_field( 'jobman_job',
    		'information',
    		array(
    			'get_callback'    => 'slug_get_job_content',
    			'update_callback' => null,
    			'schema'          => null,
    		)
    	);
    }
    
    function slug_get_job_content( $object, $field_name, $request ) {
    	$jobs = get_posts( 'post_type=jobman_job&numberposts=-1' );
    	foreach( $jobs as $job ) {
    	    return get_post_meta( $job->ID, 'data5', $job->post_content, true );
    	}
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Useful but one line of code could make it much better!’ is closed to new replies.