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

Asked

Viewed 421 times

4

I have a little problem with 2 dropdowns selectors, so far so good, because I’m using the CMB2 plugin and a METABOX selector code, and everything is ok in the Wordpress Back-End, however, when I bring my METABOX to the Front-End it gives error in calling Ajax.

My guess is that the code is not recognizing my call Get_colors... this would be my value catcher. Below is the code snippet where I found the error in debug.

        // 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 via Front-End not permission to access the values page to call the success. If anyone can help me, I’d really appreciate it.

  • It also defined the wp_ajax_nopriv? Ideally show all relevant code.

1 answer

1


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

See below a code snippet taken from Codex : https://codex.wordpress.org/AJAX_in_Plugins

// 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 front-end use : https://wordpress.stackexchange.com/questions/190297/ajaxurl-not-defined-on-front-end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.