WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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__);

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

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 '14 at 20:02
what is your plugin folder name ? – Shazzad Jan 3 '14 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 '14 at 23:30

Your Answer


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

Browse other questions tagged or ask your own question.