asked 7 years ago

Hi,
This time I want to ask how to overwrite dwqa-single-question.js. I actually copied the whole template folder to my theme/dwqa-templates
The PHP files in my theme/dwqa-templates/styles/default/ is working. But it seems the plugin is still loading JS files from plugin path, not the one I copied into my theme/dwqa-templates/assets/js/.

3 Answers
DominicStaff
answered 7 years ago

At the moment, you can not overwrite Js file from the plugin. If you want to custom js, you can insert the js code into footer.php of your theme.


answered 7 years ago

Well, my client wants me to add a notice on the vote component which can give the user some notes if the vote is failed. Something like “you need to login to vote” or “you just have already voted”.
I see your JS does return data in console.log which does have those notes. So I made updates myself like this

test2 test2 test2222222

I made changes in the JS file in your plugin folder. Once you release a new version of this plugin I will have to do it again. This is what I’m asking if it is possible to put that JS file in the theme(since you already include it in the template folder).

I understand you do not prefer your clients to edit those JS files because they might make mistakes which can give your support team a lot more workload… What I want to say is I’m totally responsible to my changes in my themes and I’m a front-end developer myself. I won’t bother you if I make errors in the JS updates. But I’m not really good at PHP. What I have tried is something like this:

add_action( 'wp_enqueue_scripts', 'dwqa_replace_scripts',100 );
function dwqa_replace_scripts(){
global $dwqa_sript_vars;
$script_version = 99.9;
wp_dequeue_script( 'dwqa-single-question');
$single_script_vars = $dwqa_sript_vars;
$single_script_vars['question_id'] = get_the_ID();
wp_localize_script( 'dwqa-single-question', 'dwqa', $single_script_vars );
wp_enqueue_script( 'dwqa-single-question', get_template_directory_uri() . '/dwqa-templates/assets/js/dwqa-single-question.js', array('jquery'), $script_version, true );
}

but it is not working. Is it possible you have a look at the code above and help me to make it work?
Thank you!

DominicStaff
answered 7 years ago

We have checked and you can use the following code: 
 

add_action( 'wp_enqueue_scripts', 'dwqa_replace_scripts',100 );
function dwqa_replace_scripts(){
global $dwqa_sript_vars;
$script_version = 99.9;
wp_dequeue_script( 'dwqa-single-question');
$single_script_vars = $dwqa_sript_vars;
$single_script_vars['question_id'] = get_the_ID();
wp_dequeue_script('dwqa-single-question');
wp_localize_script( 'dwqa-single-question-new', 'dwqa', $single_script_vars );
wp_enqueue_script( 'dwqa-single-question-new', get_template_directory_uri() . '/dwqa-templates/assets/js/dwqa-single-question.js', array('jquery'), $script_version, true );
}
Powered by DW Question & Answer Pro