Can’t get JS code to work with shortcode

You need to return the Shortcode generated string, not echo, like this: function gg_product_front_end() { wp_register_script( ‘gg_loadProducts_frontEnd’, plugins_url( ‘js/front_end.js’, __FILE__ ), array( ‘jquery’ )); wp_enqueue_script( ‘gg_loadProducts_frontEnd’ ); return ‘<p id=”test”>Test!</p>’; } Also, you need to call the JavaScript function, like this: function gg_loadProducts_frontEnd() { console.log( ‘Test!’ ); } gg_loadProducts_frontEnd(); Otherwise Test! will not be logged … Read more

Best spot for wp_register_script() and wp_register_style()

Scripts and styles can be registered on the wp_loaded hook and then later enqueued using wp_enqueue_scripts. Once the scripts and styles have been registered, they can be enqueued later using just the handles that they were originally registered with. // Register scripts/styles. They can be optionally enqueued later on. add_action( ‘wp_loaded’, ‘wpse_register_scripts’ ); function wpse_register_scripts() … Read more

Is it possible to add an action to the currently running action?

You have to use a later (higher) priority. So use current_filter() to get the current hook and priority, add 1 to that priority, and register the next action: add_action( ‘init’, ‘func_1’ ); function func_1() { global $wp_filter; $hook = current_filter(); add_action( $hook, ‘func_2’, key( $wp_filter[ $hook ] ) + 1 ); } function func_2() { … Read more

Pass $this to function nested in another public function of the same class

There are multiple ways to accomplish this. I’m showing you a way that will not fundamentally change how you are doing it: First in My_Plugin class: class My_Plugin { // … private function define_public_hooks() { $plugin_public = new My_Plugin_Public( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( ‘init’, $plugin_public, ‘init’ ); // … } // … } Then in … Read more