1

I am creating a plugin for wordpress themes that loads the templates in its own directory rather than placing templates in the theme that makes me independent to include templates in themes, for that I have created shortcodes to load the different templates on conditions. below is the code:

add_shortcode('template', 'add_template');

function add_template( $atts) {
extract( shortcode_atts( array( 'template' => ''
), $atts ) );

switch ($template) {

  case 'template1':
    include 'templates/template1.php';        
    break;

  case 'template2':
    include 'templates/template2.php';            
     break;

  default:
    include 'templates/template1.php';        
     break;   
   }  
}

My problem is in some themes my plugin start to display the page within the admin panel is there anything I am doing wrong? please help....

1 Answer 1

1

Found a solution we just need to add a check that the user is not admin before including template.

add_shortcode('template', 'add_template');

function add_template( $atts) {
extract( shortcode_atts( array( 'template' => ''
), $atts ) );

switch ($template) {

  case 'template1':
     if ( !is_admin() ) {
         include 'templates/template1.php';
     }
     break;

  case 'template2':
       if ( !is_admin() ) {
        include 'templates/template2.php';
       }            
       break;

  default:
     if ( !is_admin() ) {
       include 'templates/template1.php';
     }        
     break;   
   }  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.