Accessing customizer values in javascript

Given a setting with an ID of “foo” you can obtain the value via: var value = wp.customize( ‘foo’ ).get() To ensure that the setting is registered before you attempt to get its value, you can use this deferred pattern: wp.customize( ‘foo’, function( setting ) { var value = setting.get(); // … }); This should … Read more

How to set translations in javascripts for my plugin?

The WordPress way to do this is the wp_localize_script() function. When you enqueue a script, also add a call to wp_localize_script(): wp_register_script( ‘my-script’, ‘path/to/script.js’ ); wp_localize_script( ‘my-script’, ‘myScript’, array( ‘msg_must_match’ => __( ‘Message must match’, ‘my-plugin-domain’ ), ) ); This creates a global JavaScript object called myScript that will contain the keys and values passed … Read more

How to place script in footer?

If you want to output a single line of javascript, you might not need to put it in a js file and go through enqueuing it and stuff. Simply output it by using the wp_footer() action hook: add_action(‘wp_footer’,’print_my_script’); function print_my_script(){ echo ‘<script> // Your script here </script>’; } However, this is good just for small … Read more

var is undefined in a Gutenberg block

I think you have to pass in menu_selected edit: withAPIData( ( props ) => { return { menu_selected: `/menus/v1/menus/${ props.attributes.menu }` // custom endpoint }; } ) ( ( { menu_selected } ) => { I was able to do a very similar thing like so: edit: withAPIData( () => { return { posts: ‘/wp/v2/images?per_page=4&_embed’ … Read more

WP REST API Post Status Using JavaScript

In this tutorial, the post var status=”draft”; (see code). So I am just worried that won’t anyone able to hack that status? It depends who it is. A logged out user cannot create any posts at all. A subscriber cannot either. A contributor could create the post, but not publish it. An author or editor … Read more

Changes to JS not reflected on site

You have to look through your theme to see where the js file is enqueued. Usually there is a functions.php file in the theme which will have all the special functions and actions. Sometimes the developer will put all the enqueued scripts into the theme template (i.e. single.php) before get_header() is called. In any event, … Read more

Modals using loops and ACF [closed]

You’re right you should be using an id. I would use a counter in conjunction with get_the_ID(); $arr_posts = new WP_Query( $args ); if ( $arr_posts->have_posts() ) : $index = 0; while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); echo $index . ‘_’ . get_the_ID(); endwhile; endif; This will give you a unique id which you can … Read more