0

Let's say in my theme functions.php file, I included a php file that's only accessible in the admin control panel and named theme-options.php.

require_once ( get_template_directory() . '/theme-options.php' );

As I said, this file is accessible in the admin control panel only with this basic structure into it:

function my_theme_init(){
    register_setting('my_theme_group', 'my_theme');
}
add_action( 'admin_init', 'my_theme_init' );

function theme_page_function() {
    add_menu_page( 'My New Theme', 'mynewtheme', 'manage_options', 'mynewtheme', 'theme_options_do_page');
}
add_action( 'admin_menu', 'theme_page_function' );

function theme_options_do_page() {
    ?>
    <div class="content">
    <!-- options content -->
    </div>
    <?
}

My questions is, does this file get executed when browsing from the front end?

The file is included in the public functions.php file but the functions works only in the admin control panel.

I'm asking this question because the theme-options.php file I'm creating would contain alot of mysql queries and I don't want it to be run everytime a normal visitor visits the website.

2

My questions is, does this file get executed when browsing from the front end?

Well, yes and no.

Your file will be included on the front end when using the theme in question, but as your code is hooked to admin-only hooks, nothing really executes. The file is read but the code doesn't do anything except on the backend so all you've really done is used a 10,000ths of a second (if that) and a trivial amount of memory. I very much doubt you will notice the difference. The hooks should take care of the problem without your doing anything special.

|improve this answer|||||
  • Great! So we can say all the content and mysql queries located inside 'theme_options_do_page()' function -which is executed under 'add_action( 'admin_init', 'theme_options_init' ); add_action( 'admin_menu', 'theme_options_addpage' );'- would only work when the current user has admin privileges. – Ahmed Saad Jan 28 '14 at 14:44
  • 1
    Those hooks don't strictly have anything to do with admin privileges. They have to do with whether someone is on the backend of the site or not. – s_ha_dum Jan 28 '14 at 14:48
1

Yes, functions.php in the active theme directory is executed in front-end, but there is a simple solution to it. Use is_admin() to tell if user is viewing WP's backend

<?php
if(is_admin()) {
  require_once( get_template_directory() . '/theme-options.php' );
}

in your functions.php

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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