1

Preamble

I tried to make sure this question was not asked before. I'm new to using StackExchange so I may have failed in doing so. I don't mind rebukes; thick skin and I learn fast :)

THE PROBLEM

I wrote a function and placed in my theme's functions.php

function serverConnect() {

$serverAccess = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$serverAccess) {
    die("Access Denied");
}

return $serverAccess; 
}

The idea was to be able to have the ability to log into the DB abstracted to a function so whenever my unconventional theme needed it I could log in and do what needed to be done.

As I built more functions, I felt it would be advantageous to break off some functionality into external php files. This script is called when a form is posted in the admin cp.

In theme-root/scripts/hypothetical-theme-function.php:

$switch = mysql_select_db( 'differentDB', serverConnent());

The problem here is that hypothetical-theme-function.php has no access to functions.php to be able to call serverConnect();

I've tried include(/path/to/functions.php), include_once(/path/to/functions.php), require(/path/to/functions.php) and require_once(/path/to/functions.php). The errors I get with the once commands is:

Fatal error: Call to undefined function serverConnent() ...

The problem I get with include or require is:

Fatal error: Cannot redeclare serverConnect() ...

THE QUESTION

How do I call any function in Functions.php from an external php script?

2 Answers 2

1

You're much better off with using a (mu-)plugin. Just add a folder named mu-plugins in your wp-content directory and place your files there. Then add a plugin header comment to it:

<?php
/**
 * Plugin Name: Some Name
 * Plugin URI: http://example.com
 * Description: Loads something
 * Version: 0.1
 * Author: Akamaozu
 * Author URI: http://example.com
 * License: GNU GPL 2 <https://gist.github.com/1365159>
 */
// deny direct file access if WP not loaded
! defined( 'ABSPATH' ) AND exit;

// define your functions here
function foo()
{
    print '<h1>'.__FILE__.' loaded successfully!</h1>';
}
add_action( 'shutdown', 'foo' );
6
  • I'm not trying to build a plugin though. I wanted this baked into a theme for a particular reason. I appreciate the help, but I decided very early in I'm not trying to build a plugin so this solution is not a viable one for me.
    – akamaozu
    Commented May 4, 2012 at 10:44
  • @Akamaozu Are you selling it, so it can't reside in a plugin folder? Because else, I'd suggest to rethink it: Plugins add functionality and themes are for markup and design. EDIT: Is this for an AJAX callback?
    – kaiser
    Commented May 4, 2012 at 16:29
  • 1. I will be selling it sooner or later. Highly unlikely to be in the form that it is. It's planned to be a SAAS but I'm building this for my own benefit and modelling til the real software engineer starts work. I may release a Wordpress Theme for sale later. Maybe. 2. There will be some AJAX functionality to the final version.
    – akamaozu
    Commented May 4, 2012 at 21:02
  • Ad 2) Is the above ↑ DB stuff for an AJAX call?
    – kaiser
    Commented May 5, 2012 at 9:40
  • Currently isn't an AJAX call, but it will be when I'm done. I skirted the issue by putting serverConnect() in an external script, so that the clash doesn't happen. Still would love to know how to do this for the sake of learning, but it's not a pressing concern right now. I suspect the problem is wp-load.php or wp-blog-header.php load up the whole WP environment which calls functions.php. Redoing that gives errors, yet the one they call is outside the scope of the external file. Might be able to avoid the issue by specifically calling wp-config.php and functions.php only.
    – akamaozu
    Commented May 5, 2012 at 10:56
0

From within your external script try calling wp-load.php - example;

require( '../your-path-to-your-wp-base-directory/wp-load.php' );

See how you fair then...

3
  • I've tried that. I've tried wp-load.php and wp-blog-header.php (which I believe is the recommended way of calling the WP Environment). They work because I used a Wordpress function print_r (get_all_page_ids()); and it works perfectly. My problem still persists though :(
    – akamaozu
    Commented May 4, 2012 at 2:43
  • 1
    Odd. I ran a test for example sake and created a function named foo(); in my functions.php file with a $var = bar; and run echo foo(); on external.php with the above require to wp-load.php (or '..blog-header.php' if you prefer) and my result was // bar. So its working for me on a custom function and its working for you on WP's inbuilt functions. Have you done an include 'hypothetical-function.php'; within your actual functions.php file?
    – Adam
    Commented May 4, 2012 at 3:22
  • I'd really appreciate it if you could please paste the function snippet from functions.php with the include(/path/to/script.php) and external.php please. I've tried many variants and I either get it trying to redeclare my functions.php internal function or it doesn't see it at all. Thanks a lot for your help so far, @userabuser. I really appreciate it.
    – akamaozu
    Commented May 4, 2012 at 3:58

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.