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 am trying to include a template from plugin which required to display some of my custom information's. I wrote function to insert a page and included the template. It's working for me but once i created the page it automatically displaying in navbar menu list(currently using thwentythirteen theme).

So actually i don't want to display my custom page from NAVBAR (menu-list) as well as from Dashboard all pages list (if possible) too. There is any way to get over from this situations ?

Here is my code:

function create_user_content_page() {

$post_id = -1;

// Setup custom vars
$author_id = 1;
$slug = 'user-data-content';
$title = 'Data Content';

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

    $dataContent_page = array(
            'comment_status'   => 'closed',
            'ping_status'      => 'closed',
            'post_author'      => $author_id,
            'post_name'        => $slug,
            'post_title'       => $title,
            'post_status'      => 'private',
            'post_type'        => 'page'
    );

    $post_id = wp_insert_post( $dataContent_page );


    if ( !$post_id ) {

            wp_die( 'Error creating template page' );

    } else {

            update_post_meta( $post_id, '_wp_page_template', 'content-template.php' );

    }
  } // end check if

}


add_action( 'template_include', 'content_template' );

function content_template( $template ) {
global $post;


 if ($post->post_type == 'user-data-content')  {

    $template = dirname( __FILE__ ). '/content-template.php';
  }
  return $template;

}
share|improve this question
add comment

2 Answers

I do not completely understand what you are doing, but I get the feeling that you have greatly over-complicate this. You don't have to create a "Page" at all to load a custom template. You can hook to template_include and simply include a plugin file with much less effort than you are currently going through.

For example (stolen form another answer):

add_filter( 'template_include', 'portfolio_newpage_template', 99 );
function portfolio_newpage_template( $template ) {  
  get_header();
  include('/some/file/path/file.php');
  get_footer();
  die;
}

Honestly, I think that is all you need to do.

However, if you need parts of the page to be editable you can register a post type such that it does not show up in the menus but is still accessible if you know the correct URL. These two arguments to register_post_type will do it:

'show_ui'            => true,
'show_in_menu'       => false,

You will need to create some kind of interface for associating the posts with your users (I think) and for accessing the edit screens.

share|improve this answer
    
Thanks s_ha_dum for your kind reply. The code you given is works to include the template but it override for all templates in site. So any page i open (including my home page) it shows this template. If you see my test server :test.omicron-it.com/wordpress-3.6.1/approved-data-table where you can click the title(board member) from table & it goes to my custom template page with an id & there showing some information's. Actually you got my point, i don't want to create page. I just want my template to display this information when click that title. That's all i need. Please help. Thanks –  mehedi doha Dec 2 '13 at 12:32
    
Yes, it would override all templates. It is just a sample, and one copied from another answer no less. It should be obvious how to modify your content_template function based on that model. –  s_ha_dum Dec 2 '13 at 14:26
    
Yes.. i understand. But what is the best possible way to solve my issue ? Do you want me to post with a new topic ? Please i need a favor. Thanks & Regards. –  mehedi doha Dec 2 '13 at 20:27
    
If you need a plugin to handle this, then use this solution. @MarkKaplun suggested a shortcode or a child theme and I agree that that might be a better approach but I don't have all the information. –  s_ha_dum Dec 2 '13 at 21:05
add comment

To add to @s_ha_dum's answer, your basic problem is that you are trying to generate in a plugin presentation code, something that should be left to themes. whatever method you apply you will always be in risk that there will be a theme that behaves in a way you don't expect and your plugin will output a broken page.

If you need to be able to output only some content then your plugin should supply a shortcode. and if you truly need to have a full control over the design of a page then maybe creating a child theme will be better then a plugin.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.