1

I am trying to use a plugin function ( wp-bannerize() ) inside my custom plugin. but when i write:

if( function_exists('wp_bannerize') ){
            echo "exist";
        }else{
            echo "not exist";
        } 

The output is not exists

How can i get this function inside my custom plugin?

UPDATE:

Here the code of my plugin:

class Infinite_posts{

    public function __construct() { 

        add_action('wp_ajax_is_bannerize', 'load_plugins');
//when plugin receive an AJAX call with 'is_bannerize'parameter load_plugins() is triggered

    }

    public function load_plugins(){
        add_action( 'plugins_loaded', 'show_bannerize' );
//when all plugin are loaded show_bannerize() is triggered
    }

    public function show_bannerize(){
        if( function_exists( 'wp_bannerize' ) ) {
            echo "exist";
        } else {
            echo "not exist";
        }       
    }
}

if( !is_admin() )
    $infinitePosts = new Infinite_posts();

here my javascript too:

$('.gallerie h2').on('click', function(){
  jQuery.post(
      'http://localhost/~sdibenedetto/d-art/htdocs/wp-content/plugins/infinite-posts/infinite-posts.php', 
      {
          'action': 'is_bannerize',
          'data':   'foobarid'
      }, 
      function(response){
          alert('The server responded: ' + response);
      }
  );

});

Is this correct? How to make the AJAX call and pass the parameter "is_bannerize" to trigger the plugin? With this javascript i have this error in the alert:

<b>Fatal error</b>:  Call to undefined function add_action() in <b>/Users/sdibenedetto/Sites/d-art/htdocs/wp-content/plugins/infinite-posts/infinite-posts.php</b> on line <b>44</b><br />

Pls help. Thanks

3
  • 1
    The answer by @Dero is almost certainly on the right track. Post your code in context. There must be some detail missing.
    – s_ha_dum
    Commented Oct 17, 2013 at 13:43
  • @dero @ s_ha_dum here some updates Commented Oct 17, 2013 at 13:58
  • There's simply not enough relevant code here to provide context. From the code here, there's no way to know where/how/when this code is interacting with WordPress. And without knowing that, there's no way to know why function_exists() is returning false. Commented Oct 17, 2013 at 14:39

2 Answers 2

1

You're probably making the check too early. Make sure all plugins are loaded before trying to access the methods they provide. You can hook into plugins_loaded action to make sure all plugins are loaded before trying to use the functions they define.

class My_Plugin {
    public static function init() {
        if( function_exists( 'wp_bannerize' ) ) {
            echo "exist";
        } else {
            echo "not exist";
        } 
    }
    public function __construct() { ... }
}
add_action( 'plugins_loaded', array ( 'My_Plugin', 'init' ), 10 );
6
  • thank you Dero...but even with this i got "not exist" Commented Oct 17, 2013 at 12:16
  • @SalvatoreDibenedetto Sorry to hear that. Are you sure you have WP Bannerize plug-in activated? I just tried to create a testing plug-in using my code and it detects the wp_bannerize function correctly with the latest version of WP Bannerize plug-in.
    – Dero
    Commented Oct 17, 2013 at 12:30
  • WP Bannerize 3.0.52? Commented Oct 17, 2013 at 13:00
  • plugins_loaded might still be too soon if this plugin is loaded before WP Bannerize is loaded. Try changing the priority from the default 10 to 20 (which is working for me), or even try a later hook, such as init Commented Oct 17, 2013 at 13:14
  • @helgatheviking plugins_loaded run when all plugins are loaded. If it works using an lower priority and not using standard 10, probably is because target plugin use same hook to load file containing the function... This is the reason why when in a plugin we need another plugin function we should use the proper hook. And what's the proper hook depends on third party plugin... normally wp_loaded is a good choice, but there isn't a general rule...
    – gmazzap
    Commented Oct 17, 2013 at 14:11
1

Your syntax is wrong. You cannot hook object methods like this:

add_action('wp_ajax_is_bannerize', 'load_plugins');

You need to hook them like this:

add_action('wp_ajax_is_bannerize', array($this,'load_plugins'));

I am puzzled how you get output at all.

Secondly, you are calling the plugin file directly. Don't do that. Loading a file by a direct URL like this-- http://localhost/~sdibenedetto/d-art/htdocs/wp-content/plugins/infinite-posts/infinite-posts.php-- will cause the file to load outside of a WordPress context. You will have all kinds of "undefined function" errors and other functionality problems. Your class appears to be trying to use the AJAX API but your Javascript is not. You need to use the API.

Correct those issues and come back if there are still other problems.

5
  • 2
    +1 and yes, will be other problems, at least one, plugins_loaded run before wp_ajax_* actions...
    – gmazzap
    Commented Oct 17, 2013 at 14:25
  • And probably also wp_ajax_no_priv_* is needed here...
    – gmazzap
    Commented Oct 17, 2013 at 14:28
  • @G.M. : :) I thought of that possibility but didn't look it up to be sure, and decided to take this one a few steps at a time :)
    – s_ha_dum
    Commented Oct 17, 2013 at 14:29
  • @s_ha_dum how it should be my javacript to use AJAX API? i added below: $infinitePosts = new Infinite_posts(); this line: wp_enqueue_script( 'ajax-script', plugins_url( 'infinite-posts.js', __FILE__ ), array('jquery')); but nothing changed and no file was added to my page... Commented Oct 17, 2013 at 14:46
  • Read the Codex entry carefully, and do some research. All AJAX requests should go through /wp-admin/admin-ajax.php
    – s_ha_dum
    Commented Oct 17, 2013 at 14:51

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.