Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created one plugin as Store Locator, now i want to create page in wordpress and assign it template on activation of the plugin. the Code is like as follows :

$plugin_url = plugins_url('', __FILE__);
define('TEMPLATEPATH',$plugin_url);

function my_plugin_activate() 
{

    $post = array(
          'comment_status' => 'closed',
          'ping_status' =>  'closed' ,
          'post_author' => 1,
          'post_date' => date('Y-m-d H:i:s'),
          'post_name' => 'Store Locator',
          'post_status' => 'publish' ,
          'post_title' => 'Store Locator',
          'post_type' => 'page'
    );  
    //insert page and save the id
    $page_id = wp_insert_post( $post, false );
    //save the id in the database

    add_action( 'template_redirect', 'custom_template_redirect' );    
}

function custom_template_redirect() 
{

    global $wp;
    if ($wp->query_vars['pagename'] == 'store-locator') { // check the page slug
        status_header(200); // a 404 code will not be returned in the HTTP headers if the page does not exists

        include(TEMPLATEPATH . "/template-my-page.php"); // include the corresponding template
        die();
    }
}

but this thing is not working. is there any other way i can do it?

share|improve this question
    
I would take a look at this github.com/tommcfarlin/page-template-example –  Brad Dalton Jan 3 at 20:02
    
what is your plugin folder name ? –  Shazzad Jan 3 at 20:44
    
when you add an action, the life of that action is a single request - the current request. adding an action inside of an activation function will never work. –  Milo Jan 3 at 23:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.