1

I try to include a js-file in a shortcode function in my plugin, unfortunately without success. :-(

plugin:

function send_mail(){

$input = "<input type='submit' value='submit' class='btn btn-primary'>";
wp_register_script('myscript', plugins_url('/js/myscript.js', __FILE__), array('jquery'), '1.0', true);

return $input;
}

add_shortcode('send', 'send_mail');

front-end-page.php

<?php echo do_shortcode('[send]');

When i load the front-end-page, there is no js included. :-( What I am doing wrong?

Thanks for your helf, Yab86

2 Answers 2

1

Check shrotcode existence in a post using has_shrtocode

function send_mail(){
    $input = "<input type='submit' value='submit' class='btn btn-primary'>";
    return $input;
}
add_shortcode('send', 'send_mail');

function check_shortcode_existence() {
    global $post;
    if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'send') && !is_admin() ) {
        wp_register_script('myscript', plugins_url('/js/myscript.js', __FILE__), array('jquery'), '1.0', true);
        wp_enqueue_script( 'myscript');
    }
}
add_action( 'wp_enqueue_scripts', 'check_shortcode_existence');
Sign up to request clarification or add additional context in comments.

Comments

0

To include a js File, first you need to define the File path in core-init.php File.

define('ITSW_CORE_JS',plugins_url('assets/js/', __FILE__ ));

then declare a function like the below.

function itsw_register_core_js(){
// Register Core Plugin JS  
wp_enqueue_script('itsw-core', ITSW_CORE_JS . 'jhp-core.js',null,time(),true);
};
add_action('wp_enqueue_scripts', 'itsw_register_core_js'); 
add_action('admin_enqueue_scripts', 'itsw_register_core_js');

after doing this you can write your js code in jhp-core.js file and can call from plugin main file.

onkeypress="return itsw_isNumber(event);"
New contributor
EMILY MARCIANO is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.