0

I'm making a plugin with a class and methods.

This class gets called through the theme ( hard coded ) to enqueue and localize my js. it also prepares it for ajax. However, i keep getting ERROR: 400

POST http://www.devsite.local/wp-admin/admin-ajax.php 400 (Bad Request)

I've ran ajax outside of a class in multiple plugins and at the functions.php with no issues, but i can't get it to work inside a class.

This is my code:

class CCYTFeatured {
public function __construct(){
        $this->cc_yt_scripts();
        add_action( 'wp_enqueue_scripts', array($this, 'cc_yt_scripts' ));

        add_action( 'wp_ajax_cc_yt_featured', array( $this, 'cc_yt_featured' ) );
        add_action( 'wp_ajax_nopriv_cc_yt_featured', array( $this, 'cc_yt_featured' ) );

}
// Load CSS AND JAVA FRONT END
public function cc_yt_scripts() {
    wp_register_style( 'cc_yt_style',
        plugins_url( '/css/cc_yt.css', __FILE__ ),
        array(),
        cc_yt_version()
        );
    wp_enqueue_style('cc_yt_style');

    // JAVASCRIPT
    wp_register_script( 'cc_yt_javascript',
        plugins_url( '/js/cc_yt.js', __FILE__ ),
        array('jquery'),
        cc_yt_version(),
        true
    );
    wp_enqueue_script('cc_yt_javascript');
    wp_localize_script(
        'cc_yt_javascript',
        'cc_yt_script',
        array(
            'ajaxurl'   => admin_url( 'admin-ajax.php' , __FILE__ ),
            'ajax_nonce' => wp_create_nonce('my_nonce'),
        )
    );
}
public function cc_yt_featured(){

    check_ajax_referer( 'my_nonce', 'security' );
    echo json_encode('IM HERE, YOU CAN FIND US');
}
}

This is my JavaScript ajax call:

jQuery.ajax({
    url: cc_yt_script.ajaxurl,
    type : 'POST',
    data: {
        action : 'cc_yt_featured',
        security: cc_yt_script.ajax_nonce,
        post: 'bananas',
    },
    success: function(response) {
        console.log('THIS IS THE RESPONSE:  '+ response);
    }
});

I keep getting the same error, i can't figure out what is wrong with my class:

This is how i call it.

$cc_yt = new CCYTFeatured;

I have that call hard coded inside a theme file. This does enqueue the styles and scripts successfully.

The ajax get is the following:

http://www.devsite.local/wp-admin/admin-ajax.php?action=cc_yt_featured&security=79e39910b8&post=bananas

Anyone have any clue what i'm doing wrong??

Thanks!

  • Note that i tested it out side the classs ( copied the enquee and localizing out of the class and ran it) It work perfectly.. I think i might have an error on the way i have my class structured. I still need to be able to run the ajax call successfully from the class. – Jhonatan Villena Nov 12 '18 at 23:20
  • What happens if you place $cc_yt = new CCYTFeatured; in your functions.php file? – czerspalace Nov 12 '18 at 23:29
  • Have you considered using the more modern REST API instead? It's easier to debug, WP Admin AJAX isn't so forgiving with mistakes – Tom J Nowell Nov 13 '18 at 0:03
  • Additionally, when you say "I have that call hard coded inside a theme file." which theme file are you referring to? – Tom J Nowell Nov 13 '18 at 0:05
  • If $cc_yt = new CCYTFeatured; is inside a template, then it's not going to be run during an AJAX request, which means your callbacks won't be hooked, leading to a 400 error. – Jacob Peattie Nov 13 '18 at 0:19
0

All the code is correct except the way i was calling.

I created a different method for __construct and then called it on the plugin it self so the scripts and styles get hooked before the templates. In my template i called the class again and the methods i need it. I no longer constructed my method all at once.

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.