Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to use a plugin function in a custom php file. Pasting this code at the top of my file i can use all the wordpress functions:

<?php 
$file = dirname(__FILE__);
$file = substr($file, 0, stripos($file, "wp-content") );
require( $file .'/wp-blog-header.php');

But i get this error when i call plugin function:

Fatal error: Call to undefined function wp_bannerize() in mysite/my-file.php

How to get wp_bannerize() in my-file.php ?

UPDATE:

If i call my-file.php in my browser like this:

http://localhost/~sdibenedetto/d-art/htdocs/wp-content/themes/d-art/infinite-posts.php?offset=3

I have no errors and wp_bannerize() works correctly

At the AJAX call i got the error... May depends that infinite-scroll jquery plugin uses load() instead $.ajax()?

share|improve this question
    
What is your main goal. Maybe you can avoid this kind of integration. You can try to include the plugin main file, usually "wp_contents/plugin_name/plugin_name.php" –  SpartakusMd Oct 16 '13 at 11:21
    
my-file.php answer to an ajax call and return an array of posts ( this is used for the "infinite-scroll" ). Beside this array i want to show a ADV banner. I use this plugin to menage ADV and banner... –  Salvatore Dibenedetto Oct 16 '13 at 12:27
1  
see AJAX in Plugins in Codex for the correct way to do AJAX with WordPress. –  Milo Oct 16 '13 at 16:46

3 Answers 3

You should make ajax requests via admin-ajax.php.

http://wp.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/

In this way you'll have access to all wordpress functions.

share|improve this answer

Salvatore! You can edit wp_bannerize manually (see file plugins/wp-bannerize/main.php) in order to look like this:

require_once( trailingslashit( dirname( __FILE__ ) ) . 'main.h.php' );
require_once( trailingslashit( dirname( __FILE__ ) ).'Classes/wpBannerizeClass.php' );

if ( @isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) {
    if ( is_admin() ) {
        require_once( trailingslashit( dirname( __FILE__) ) . 'Classes/wpBannerizeAdmin.php' );
        $wpBannerizeAdmin = new WPBannerizeAdmin( __FILE__ );
        require_once( trailingslashit( dirname( __FILE__) ) . 'Classes/wpBannerizeAjax.php' );
    }else{
        require_once( trailingslashit( dirname( __FILE__ ) ) . 'Classes/wpBannerizeFrontend.php' );
        $wpBannerizeFrontend = new WPBannerizeFrontend( __FILE__ );
        require_once( trailingslashit( dirname( __FILE__ ) ) . 'Classes/wpBannerizeFunctions.php' );

    }
}
else {
    if ( is_admin() ) {
        require_once( trailingslashit( dirname( __FILE__ ) ) . 'Classes/wpBannerizeAdmin.php' );
        //
        $wpBannerizeAdmin = new WPBannerizeAdmin( __FILE__ );
        $wpBannerizeAdmin->register_plugin_settings( __FILE__ );
        register_activation_hook( __FILE__, array( &$wpBannerizeAdmin, 'pluginDidActive' ) );
        register_activation_hook( __FILE__, array( &$wpBannerizeAdmin, 'pluginDidDeactive' ) );
    }
    else {
        require_once( trailingslashit( dirname( __FILE__ ) ) . 'Classes/wpBannerizeFrontend.php' );
        $wpBannerizeFrontend = new WPBannerizeFrontend( __FILE__ );
        require_once( trailingslashit( dirname( __FILE__ ) ) . 'Classes/wpBannerizeFunctions.php' );
    }
}

This will definitely help. The main reason why it's not working properly is condition in the 4th line (check for ajax-request).

share|improve this answer

If you are intent on calling into Wordpress functionality from outside of the usual WP workflow, you can put the following two lines in your code:

define("WP_USE_THEMES", false);
require_once("../../../wp-blog-header.php");

Of course, the above assumes I'm calling this from within a root theme or plugin folder (the ../ indicates reverse folder traversal). It looks as though you've already included wp-blog-header.php, and the define is an indication to Wordpress not to invoke certain functionality related to normal page/post queries. For an example of how Wordpress functionality can be implemented in a disparate PHP codebase, I would suggest you look at this Laravel+Wordpress post or similar attempts to utilize Wordpress functionality from outside of Wordpress itself. If you need the functionality Wordpress offers (including that of its activated plugins) this might be the best solution.

Otherwise, depending on your scenario, invoking Wordpress code as Ajax, as suggested by other users, may be the appropriate route to go. In particular, if you wanted to implement a simple CRUD API, such as a RESTful operation, this would be the suggested route. This is the route I would personally recommend if all you are interested in doing is moving data from points A and B.

Your code may have failed because you didn't define the WP_USE_THEMES flag properly; under normal circumstances, functionality offered by plugins isn't available until after a certain point (probably the init action, based on http://codex.wordpress.org/Plugin_API/Action_Reference), and trying to call plugin functionality before that action has occurred may result in what you describe.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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