admin管理员组

文章数量:1425868

unfortunately I have not found a solution here and via google. I get the following error message when calling:

Uncaught ReferenceError: ait_php_var is not defined ../assets/js/ait_buttons.js?wp-mce-4940-20190515 line 2

add_action( 'wp_enqueue_scripts', 'ait_load_scripts' );
function ait_load_scripts() {
    $ait_pfad = plugin_dir_url( __FILE__ ) . '/assets/js/ait_buttons.js';
    $ait_add_options = 'here';
    // Enqueued script with localized data.
    wp_register_script('ait_js_script', $ait_pfad );
    wp_enqueue_script( 'ait_js_script', $ait_pfad );
    // Localize the script with new data
    wp_localize_script( 'ait_js_script', 'ait_php_var',
        array(
            'external_link' => __( 'External Link', 'aittec_language' ),
            'event_category' => __( 'Event Category', 'aittec_language' ),
            'internal_link' => __( 'Internal Link', 'aittec_language' ),
            'ackids' => $ait_add_options,
        )
    );
}

Here is the relevant (sample) snippet from the JS file:

// JavaScript Document //
alert( ait_php_var.ackids );

The file is in the correct folder, otherwise the error would not appear in line 2. The same error message also occurs when the other variable (e. g. external_link) is called in the same or similar form. I am looking forward to any help to solve the problem.

unfortunately I have not found a solution here and via google. I get the following error message when calling:

Uncaught ReferenceError: ait_php_var is not defined ../assets/js/ait_buttons.js?wp-mce-4940-20190515 line 2

add_action( 'wp_enqueue_scripts', 'ait_load_scripts' );
function ait_load_scripts() {
    $ait_pfad = plugin_dir_url( __FILE__ ) . '/assets/js/ait_buttons.js';
    $ait_add_options = 'here';
    // Enqueued script with localized data.
    wp_register_script('ait_js_script', $ait_pfad );
    wp_enqueue_script( 'ait_js_script', $ait_pfad );
    // Localize the script with new data
    wp_localize_script( 'ait_js_script', 'ait_php_var',
        array(
            'external_link' => __( 'External Link', 'aittec_language' ),
            'event_category' => __( 'Event Category', 'aittec_language' ),
            'internal_link' => __( 'Internal Link', 'aittec_language' ),
            'ackids' => $ait_add_options,
        )
    );
}

Here is the relevant (sample) snippet from the JS file:

// JavaScript Document //
alert( ait_php_var.ackids );

The file is in the correct folder, otherwise the error would not appear in line 2. The same error message also occurs when the other variable (e. g. external_link) is called in the same or similar form. I am looking forward to any help to solve the problem.

Share Improve this question edited May 26, 2019 at 5:10 nmr 4,5672 gold badges17 silver badges25 bronze badges asked May 25, 2019 at 20:46 hagehage 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should remove trailing slash / from 3rd line.

$ait_pfad = plugin_dir_url( __FILE__ ) . '/assets/js/ait_buttons.js';

this should be

$ait_pfad = plugin_dir_url( __FILE__ ) . 'assets/js/ait_buttons.js';

wp_plugin_dir() function returns url including a trailing slash ("/") so you should not add it again.

Please read wp_plugin_dir() function reference to learn more about this function.

本文标签: wp localize scriptIssue with wplocalizescript