How to add A template from a plug-in folder?

How do I add a template from my plug-in folder to the created page? Or can I add php code to the created page? The code below will create a page but the add_action and the function below that does not add the content to the page.

$author_id = 1;
$slug = ‘slider’;
$title = ‘slider’;

// Check if page exists, if not create it
if ( null == get_page_by_title( $title )) {

$uploader_page = array(
‘comment_status’ => ‘closed’,
‘ping_status’ => ‘closed’,
‘post_author’ => $author_id,
‘post_name’ => $slug,
‘post_title’ => $title,
‘post_status’ => ‘publish’,
‘post_type’ => ‘page’
);

$post_id = wp_insert_post( $uploader_page );

if ( !$post_id ) {

wp_die( ‘Error creating template page’ );

} else {

update_post_meta( $post_id, ‘_wp_page_template’, ‘custom-uploadr.php’ );

}
} // end check if

add_action( ‘template_include’, ‘uploadr_redirect’ );
function uploadr_redirect( $template ) {

$plugindir = dirname( __FILE__ );

if ( is_page_template( ‘custom-uploadr.php’ )) {

$template = $plugindir . ‘/templates/custom-uploadr.php’;
}

return $template;
}

Read more here:: How to add A template from a plug-in folder?

Leave a Reply

Your email address will not be published. Required fields are marked *