0

I'm working on a plugin which needs a custom Preview page. In this page i call the neccessary wp functions(wp-load.php & wp-admin/includes/admin.php).

The problem is, that the admin bar is not visible. So my question is, how to display the admin bar on this page? Here is the preview.php file:

<?php

//Include wordpress core files
for ($i = 0; $i < $depth = 10; $i++) {
    $wp_root_path = str_repeat( '../', $i );
    if ( file_exists("{$wp_root_path}wp-load.php" ) ) {
        require_once("{$wp_root_path}wp-load.php");
        require_once("{$wp_root_path}wp-admin/includes/admin.php");
        break;
    }
}

//Redirect if user is not logged in
auth_redirect();

//Die if user can't edit posts
if(!current_user_can('edit_posts') ) die(__("You don't have the neccessary permission to preview forms.", "nemus_slider"));

//And the actual preview code
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Preview</title>
    <?php wp_head(); ?>
</head>
<body>
    Some preview stuff...
    <?php wp_footer(); ?>
</body>
</html>

2 Answers 2

0

have you looked at the codex ?? : http://codex.wordpress.org/Function_Reference/add_menu

you do NOT need to load any WP files like you are doing if you're code is for a plugin!!!

3
  • The code is for a preview.php file which i load to display some data, thats why i need to include the core wp files. And the add_menu is not what i'm looking for, i don't want to add an item to the admin bar, i want to display the whole thing.
    – passatgt
    Jun 23, 2013 at 21:56
  • the admin bar is actually inserted via wp_footer(), so as long as your preview.php is a valid template page for a theme, then it should load. edit so the issue is that you are trying to make your plugin display a page template, as if it were a theme... which I don't think is possible... or at least, is not done easily. Inclulding the WP cores files in a file that is loaded by WP, is not going to change how WP functions. In fact, the WP cores are most certainly already loaded by the time your plugin loads up preview.php.
    – br3nt
    Jun 23, 2013 at 22:01
  • you'd probably be better off doing the following: - creating a post(page) dynamically - then adding a filter to the_content() that checks for that page you created and then returns all of your preview info. Another way to do it would be via a shortcode, you could even add the shortcode to the page you created. either way, creting your own "template" like preview.php will not work imho
    – br3nt
    Jun 23, 2013 at 22:07
0

I agree with @br3nt that you are going about this wrong. I'd suggest that you do something like this:

function display_my_preview() {
  if (!isset($_GET['my_preview'])) return;

  //Redirect if user is not logged in
  if (!is_user_logged_in()) {
    auth_redirect();
  }

  //Die if user can't edit posts
  if(!current_user_can('edit_posts') ) die(__("You don't have the neccessary permission to preview forms.", "nemus_slider"));

  //And the actual preview code
  ?>
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="utf-8" />
      <title>Preview</title>
      <?php wp_head(); ?>
  </head>
  <body>
      Some preview stuff...
      <?php wp_footer(); ?>
  </body>
  </html>
  <?php 
  exit();
} 
add_action('template_redirect','display_my_preview');

By doing this, you do not have to deal with including any Core files and you can get to your preview page by adding my_preview=true to the URL. Obviously, this is a crude implementation but it works as a proof of concept.

Note: I know that auth_redirect should be checking for logged in user status but I can't get it to function correctly. I am trying to work out why. Until, check explicitly.

2
  • Thanks for this, i tried it but as you mentioned, the auth_direct is not working and i think its because some of the core stuff is missing, for example the do_shortcode function also not working here. And the admin bar is still not visible:)
    – passatgt
    Jun 24, 2013 at 7:23
  • It does work as I wrote it when I test it. You must use it like I wrote it, hooked to template_redirect.
    – s_ha_dum
    Jun 24, 2013 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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