Ajax callback not work

My files is :

synergyservice.php

register_activation_hook( __FILE__, array( 'Synergy', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Synergy', 'plugin_deactivation' ) );

if ( is_admin() || ( defined( 'WP_CLI' )  WP_CLI ) ) {
    require_once( __DIR__ . '/environments.php' );
    require_once( SYNERGY__PLUGIN_DIR . 'class.synergyservices.php' );
    require_once( wp_normalize_path (SYNERGY__PLUGIN_DIR . 'admin/Helpers/class.helper.php' ) );

    add_action( 'init', array( 'Synergy', 'init' ) );

    require_once(  wp_normalize_path(SYNERGY__PLUGIN_DIR . 'admin/Classes/class.synergy-admin.php') );
    add_action( 'init', array( 'Synergy_admin', 'init' ) );
}

class-synergyservice.php

?php

class Synergy{

    private static bool $initiated = false;

    public static function init(): void
    {
        if ( ! self::$initiated ) {
            self::init_hooks();
        }
    }

    /**
     * Initializes WordPress hooks
     */
    private static function init_hooks(): void
    {
        self::$initiated = true;

        Synergy_admin::init();

    }


    /**
     * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
     * @static
     */
    public static function plugin_activation(): void
    {
        if ( version_compare( $GLOBALS['wp_version'], SYNERGY__MINIMUM_WP_VERSION, '' ) ) {

            load_plugin_textdomain( 'synergy', false, SYNERGY__WP_PLUGIN . '/languages/' );

        } elseif ( ! empty( $_SERVER['SCRIPT_NAME'] )  false !== strpos( $_SERVER['SCRIPT_NAME'], '/wp-admin/plugins.php' ) ) {
            add_option( 'Activated_SynergyService', true );
        }
    }

    /**
     * Removes all connection options
     * @static
     */
    public static function plugin_deactivation( ): void
    {

    }

}

class-contact.php

?php    
if( !class_exists('Synergy_contact')) {
    class Synergy_contact {

        private static bool $initiated = false;

        public static function init(): void
        {
            if ( ! self::$initiated ) {
                self::init_hooks();
            }
        }

        public static function init_hooks(): void {

            self::$initiated = true;
            
            wp_register_script( 'contacts.js', plugin_dir_url( __FILE__ ) . '/../../js/contacts.js', array('jquery'), SYNERGY__VERSION );

            $title_nonce = wp_create_nonce( 'synergy_nonce_form_contact' );

            wp_localize_script(
                'contacts.js',
                'ajax_obj_form_contact',
                array(
                    'ajax_url' = admin_url( 'admin-ajax.php' ),
                    'nonce'    = $title_nonce,
                )
            );

            wp_enqueue_script( 'contacts.js' );

            add_action('wp_ajax_get_data_from_form', 'get_data_from_form' );
            add_action('wp_ajax_nopriv_get_data_from_form', 'get_data_from_form');

               // render my view to use script


            }

        }
            public function get_data_from_form(){
        echo a;
        wp_die('ub');

    }

}

My script:

jQuery.ajax({
    type: POST,
    url: ajax_obj_form_contact.ajax_url,
    // _ajax_nonce: ajax_obj_form_contact.nonce,
    // dataType: JSON,
    data: {
        action : 'get_data_from_form',
    }
  });

Error 404 bad request. Response 0 from wp-admin.

I see on $GLOBALS['wp_filter'] my hook and found it.

I guess problem is instance of class, because I try to NOT use auto-instance at end of file class, and wp-admin cannot found method. I try run do_action(my_hook) and it's work.

Is it a bad practice not to instantiate the class at the end of the file?

Anyone can help me?

Best regards,

Topic ajax actions Wordpress

Category Web


The problem is a basic PHP issue, you told WordPress to call a function named get_data_from_form when the wp_ajax_get_data_from_form action happens]

add_action('wp_ajax_get_data_from_form', 'get_data_from_form' );

But no function with that name exists.

Sure you have a class with a function inside it that exists, but that's not the same. Think of it this way, if there was a function get_data_from_form() {} in another file, how would it know the difference between that and the static member function inside your class? What if another class had a function with the same name? PHP doesn't know, and it can't know unless you tell it.

So instead, you need to change it from a string that contains the name of a top level function, to a reference to your static class function. E.g. [ 'Foo', 'Bar' ]. I strongly recommend reading the callable type documentation, particularly the examples at:

https://www.php.net/manual/en/language.types.callable.php

Is it a bad practice not to instantiate the class at the end of the file?

It's good practice not to instantiate the class, but at no point in your code do you ever instantiate it. All your class methods are static, and there is no object oriented code here. Functions in a namespace would have been functionally identical and simpler.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.