Problem in wp_localize_script

First of all, you are NOT enqueuing the script.. Try this code, working in my test install.

function load_my_scripts() {

    // Create the localizations array
    $localize = array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
    );

    // Register The Script First
    wp_register_script('custom-js', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'));

    wp_enqueue_script('twentyfifteen-custom-js', get_template_directory_uri() . '/js/custom.js', array(), '20141010', true);

    // Register the localization
    wp_localize_script( 'custom-js', 'MyAjax', $localize );

    // Enqueue the script
    wp_enqueue_script( 'custom-js' );
}

add_action('wp_enqueue_scripts', 'load_my_scripts');

Leave a Comment