how to call secondary menu from a php page other than index.php

Hello there,

I am developing an ecommerce website. In this I am using two header files.
1. header-client.php : this part is working fine. In this I have no issues in this. Here I am using index.php
2. header-adm.php : this part is purely for Admins. Here I have a Admin login page "alogin.php" using ajax I checking the username and password of the admins. On successful login, page is redirected to "adm-main.php".
In the "header-adm.php" I have certain wordpress functiions, which is causing problem.

The error which I am facing is "Call to undefined function wp_enqueue_script()"

<?php include("header-adm.php"); ?>

Open in new window

Code of "header-adm.php" :
<?php include ("dbcon.php"); ?>
<!DOCTYPE html>
    <?php wp_enqueue_script("jQuery"); ?>
    <script src=""></script>
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/css/dsgncnv.css">
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/astyle.css">
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/amenustyle.css">		
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/skeleton.css">
   <div class="container-1">
       <div class="cont1div">
         <div class="lhdr-in">
           <a href="http://localhost/website/admin/login.php"><img src="http://localhost/website/wp-content/themes/website/images/logo.jpg" alt="" style="width:100%;height:100%;" /></a>
        <div class="rhdr-in"></div>	
   <div class="container">
      <div class="main-nav fifteen columns" id="show-nav"><a href="#">Toggle Navigation</a></div>
      <div class="fifteen columns" id="close-nav"><a href="#">X</a></div>
      <div class="fifteen columns nav-bar">
        <?php wp_nav_menu(array('menu' => 'Admin Menu', 'container_class' => 'main-nav', 'container' => 'nav', 'theme_location' => 'secondary')); ?>

Open in new window

Please help ?? Anybody who knows the solution please reply. I got stuck at this position for the last two days, still can't figure out where i am doing wrong
Suneet PantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John EastonDirectorCommented:
In your header-adm file you include 'dbcon.php' but no other files.  Assuming your dbcon only includes settings for your database, then nowhere do you define the function wp_enqueue_script.

If you are taking functions from WordPress into your own (I assume non-WordPress based) site then you need to take the actual function code from WordPress and include it in your site.
Suneet PantAuthor Commented:
yes you are right, Admin part is non-wordpress based. But how to include the actual function code from WordPress and include it in your site. What is the process??
John EastonDirectorCommented:
It depends on the function and where you are copying it from.  Sites like WordPress are fairly complex and functions are usually not stand alone.  

The principle is simple.  Firstly you have to find the function code in the WordPress source code.  This function appears to be in wp-includes\functions.wp-scripts.php.  Then in theory you copy and paste it to your own site.

However, looking at this code I can see some problems.  Firstly, it requires the global object $wp_scripts.  It also uses the WP_Scripts() class, so you will need all the definitions of these.  Therefore you would need to copy more functions, which often then have other reference.  It is highly likely you would end up copying a large chunk of WordPress.

Unfortunately not having done much WP development I don't know the function at all or what it does.  But it would likely be easier to recreate a function that does what you want.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
I am confused - if you are creating an admin page outside of wordpress then why are you using a Word Press function to include a javascript library?

wp_enqueue_script basically is going to include your JQuery script - so why not just do this instead

<script src=""></script>

Open in new window

Or use whatever link you would for your JQuery file?

For your other use of the WP functions like wp_nav_menu - again if this is a separate page from your WP site then why not just put the HTML for the menu directly in there instead of trying to run off WP?

Either you are creating a WP template or you are not - in your case you are sort of creating one - which is not going to cut it.

Solution: either go with a full WP template for your admin page in which case you will need to construct it in a manner that is consistent with the WP framework
Distance your admin script completely from WP - and code the bits that you need in the standard way independent of WP functions.

You might find this link useful
Suneet PantAuthor Commented:
Hi Julean,

Maybe I am using non-wordpress base. I myself don't have any idea about it. Actually the scenario is I am just trying to show secondary menu after an admin successfully logged in. This menu comprises of various pages that are all defined in the wordpress itself. And using page.php I am calling different pages according to the link selected by the admin. Below is the code I have in pagae.php. Index.php is being used in client side and that part is working fine. I am sure you will understand this what I am trying to accomplish.

Code of page.php :
<?php get_header('adm'); ?>
  <div class="fifteen columns row">
      //Order Management
      if (is_page('381')) { get_template_part('','page'); }
      else if (is_page('383')) { get_template_part('unpaid','page'); }
      else if (is_page('387')){ get_template_part('','page'); }
      else if (is_page('389')){ get_template_part('','page'); }
      else if (is_page('391')){ get_template_part('','page'); }
      else if (is_page('395')){ get_template_part('','page'); }
      else if (is_page('397')){ get_template_part('workorder','page'); }
      else if (is_page('399')){ get_template_part('','page'); }
      else if (is_page('401')){ get_template_part('','page'); }
      else if (is_page('405')){ get_template_part('','page'); }
      else if (is_page('403')){ get_template_part('','page'); }
      else if (is_page('407')){ get_template_part('','page'); }
      else if (is_page('410')){ get_template_part('','page'); }
      else if (is_page('412')) { get_template_part('subcatgA','page'); }
      else if (is_page('414')) { get_template_part('','page'); }
      else if (is_page('416')){ get_template_part('','page'); }
      else if (is_page('418')){ get_template_part('','page'); }
      else if (is_page('420')){ get_template_part('','page'); }
      //Client ws
      else if (is_page('422')){ header('Location:'); }
<?php get_footer(); ?>

Open in new window

Julian HansenCommented:
Fine, but that is not going to work outside of the WP framework.

Just so I understand - if the links are to be shown on an admin page - what is stopping someone from typing the link in manually to get to the pages that those links point to? In other words - if a user does not go through the Admin page and enters the url to one of the pages defined above - what happens?
Suneet PantAuthor Commented:
I am working on ecommerce website with a theme I am developing ere actually there are two types of persons who will be using the site. .
First of all users who want to buy something. These users will have to first-of-all have to register themselves and then logs in and make purchases accordingly. This client part is working fine as all things are done using wordpress. The cllent's main page is opened using index.php so accordingly primary menu can be seen and accessed properly, No issues in this part,


Second, Admin Panel, where after logging in admin takes full control of the database.. This is done in a separate page "alogin.php". Since no index.php is being used here, so whenever admin successfully logs in, page is redirected to "adm-main.php" which calls the header file using this code :  
<?php include("header-adm.php"); ?>

Open in new window

When this page is accessed, it shows this error message :
  "Call to undefined function wp_enqueue_script()".

I think this error is coming because I am outside of wordpress, Even if I comment this line, it then stucks in "wp_nav_menu()", another wordpress function.  How can I access wordpress functions?? Please help.
Julian HansenCommented:
We just went round in a circle - your latest post basically repeats your opening post.

To access the wordpress functions- as I said in my last post - you have to construct your admin page as a word press theme. That means you replicate the way a theme is created including all the relevant WP files as you would in a theme.
Suneet PantAuthor Commented:
You mean I should create a login page in wordpress and creates a link. Lets say this is my link :

Now how will i connect this link to my alogin.php. Because this page is altogether a separate page.

For your information I am attaching  login.php showing client login and admin login

According to you where should I show this link.

 Please guide ??
Julian HansenCommented:
Try adding this to your admin header

Open in new window

Change the path as required if your admin page does not run out of the root.

You also did not answer the other question I posted which was

* The links available from the admin page - what if you type those links directly into the browser while NOT logged in as Admin? Do you have measures in place to prevent access to those pages?
Suneet PantAuthor Commented:
right now development part is done on my system ie localhost, but yes I am using SESSIONS  to prevent unauthorize access. if any tries to access any page wthout login, page will automatically redirects to login page.
Julian HansenCommented:
Did you try including the wp_load.php file?
Suneet PantAuthor Commented:
I have included this code in my admin header file :
<?php require('../../../wp-load.php'); ?>

Open in new window

Now on running the code, no error is coming but instead showing the whole code of header-adm.php in "Javascript Console" which means it is not showing the secondary menu. Why this is happening when I have used wordpress function "wp_nav_menus();" to call the secondary menu. Please guide ??
Julian HansenCommented:
You have not provided enough information for an accurate diagnosis.

Do you have a link to the page?

You might want to open another question for this. This question was about dealing with the error relating to missing WP functions - this sounds more like a structural problem with your code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Suneet PantAuthor Commented:
yeah even I think there is some structural problem with my code, once found I will let you know
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.