METABOX with dropdown state / city in Front End using Cmb2 Plugin

4

I have a little problem with 2% with selectors, so that's fine, because I'm using the CMB2 plugin and a METABOX selector code, and everything is ok in the Wordpress Backend, but when I bring my METABOX for the Front End it gives an error in the AJax call.

My guess is that the code is not recognizing my call dropdowns ... that would be my value grabber. Here is the code snippet where I found the error in Get_colors .

        // ajaxurl is already defined in wp-admin, so no special stuff needed.
    $.post( ajaxurl, {
        action: 'get_colors',
        value: new_val
    }, app.handle_response, 'json' );
};

I also think that Front-End does not allow permission to access the values page to call debug . If anyone can help me I am very grateful.

    
asked by anonymous 25.07.2015 / 17:07

1 answer

1

The variable ajaxurl is usually only available in wp-admin. One way to create an equivalent variable on the front end would be via the wp_localize_script function.

Below is a snippet of code taken from the codex: link

// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );

Here you can find a complete example of use on the front end: link

    
05.05.2017 / 18:51