I’m working on a simple plugin, and it’s the first time I use OOP and namespace, so I’ll include the whole code.
I’m trying to load JS only when the shortcode is processed… but it doesn’t work, the script isn’t loaded!
Any suggestion? What I’m doing wrong?
Thank you very much.
Simone.
namespace xxsimoxx\smyp;
class Send_Position {
public function __construct() {
add_action( 'wp_enqueue_scripts', [$this, 'register_script'] );
add_shortcode( 'smyp', [$this, 'button_shortcode'] );
}
public function register_script() {
wp_register_script( 'smyp-script', plugins_url( '/scripts/smyp.js', __FILE__ ), array(), '1.0.0', false );
wp_register_style( 'smyp-style', plugins_url( '/styles/smyp.css', __FILE__ ), array(), '1.0.0', 'all' );
}
public function button_shortcode( $atts, $content = null ) {
wp_enqueue_script('smyp-script');
wp_enqueue_style('smyp-style');
return '<button class="smyp-button" onclick="smypSend()">' . $content .'</button>';
}
}
new Send_Position;
Did you create the script? The above works for me after placing the file into it’s own directory and creating the script. I can’t upload a zip here… sent to you on Slack instead.
By the way, good show for loading your scripts and styles performantly for shortcodes!
Because it’s my first ever OOP code (i just used some perl classes in some scripts) and the code wasn’t working on two different sites with different setup I belived that was the code… but the problem was in themes.
Both themes were not calling wp_footer() in the footer file that is inserted by get_footer(), so everything that ClassicPress wanted to insert in the footer was not honored.