I am building a plugin which allows admins to add staff members to a database. I have created a function in my plugin class file...
cp_libstaff_frontend_display()
...which displays a table of every staff member in the database to front-end users. This function is attached to shortcode which displays at the URL:
mysite/librarywp/staff-directory
Now what I would like to do is have a link on each staff member's name which takes the user to a page for just that staff member's information. For example,
mysite/librarywp/libstaff/1
Where '1' is the ID of the staff member whose individual page should be retrieved. I have created an endpoint for this like so:
public static function cp_libstaff_add_rewrites(){
add_rewrite_endpoint( 'libstaff', EP_PERMALINK );
}
public static function cp_libstaff_filter_request( $vars ){
if( !isset( $vars['libstaff'] ) ) $vars['libstaff'] = true;
return $vars;
}
public static function cp_libstaff_rewrite_catch_libstaff(){
if( get_query_var( 'libstaff' ) ){
echo 'WHAT NOW?';
exit();
}
}
add_action( 'init', array('Libstaff', 'cp_libstaff_add_rewrites') );
add_action( 'template_redirect', array('Libstaff', 'cp_libstaff_rewrite_catch_libstaff') );
add_filter( 'request', array('Libstaff', 'cp_libstaff_filter_request') );
After this point I am beyond confused about how to proceed with the callback function. Should there be a custom post type created in here somewhere? A template of some sort?
I am building a plugin which allows admins to add staff members to a database. I have created a function in my plugin class file...
cp_libstaff_frontend_display()
...which displays a table of every staff member in the database to front-end users. This function is attached to shortcode which displays at the URL:
mysite.com/librarywp/staff-directory
Now what I would like to do is have a link on each staff member's name which takes the user to a page for just that staff member's information. For example,
mysite.com/librarywp/libstaff/1
Where '1' is the ID of the staff member whose individual page should be retrieved. I have created an endpoint for this like so:
public static function cp_libstaff_add_rewrites(){
add_rewrite_endpoint( 'libstaff', EP_PERMALINK );
}
public static function cp_libstaff_filter_request( $vars ){
if( !isset( $vars['libstaff'] ) ) $vars['libstaff'] = true;
return $vars;
}
public static function cp_libstaff_rewrite_catch_libstaff(){
if( get_query_var( 'libstaff' ) ){
echo 'WHAT NOW?';
exit();
}
}
add_action( 'init', array('Libstaff', 'cp_libstaff_add_rewrites') );
add_action( 'template_redirect', array('Libstaff', 'cp_libstaff_rewrite_catch_libstaff') );
add_filter( 'request', array('Libstaff', 'cp_libstaff_filter_request') );
After this point I am beyond confused about how to proceed with the callback function. Should there be a custom post type created in here somewhere? A template of some sort?
The code in the original question was on the right track. The code below was taken from the linked tutorial. Basically all I had to do was create a template file in my plugin (I just copied some code from single.php from my main theme to use as a guide into:
dirname( __FILE__ ) . '/staff-template.php'
Then when the template_redirect is run it displays that template. This assumes, of course, that libstaff is set as a query var.
function cp_libstaff_rewrite_catch_libstaff() {
global $wp_query;
// if this is not a request for json or a singular object then bail
if ( ! isset( $wp_query->query_vars['libstaff'] ) || ! is_singular() )
return;
// include custom template
include dirname( __FILE__ ) . '/staff-template.php';
exit;
}
add_action( 'template_redirect', 'cp_libstaff_rewrite_catch_libstaff' );
Tutorial link: https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/