The effects of alcohol on
sustanon 250 leucine for – real weight loss & bodybuilding benefits?
Question How to set value on field dynamically with JS
(@danihml)
Active Member
Joined: 3 years ago
i'm facing a trouble setting dynamically in name and email fields the value of my logged clients. I'm not using the wordpress login, there are some APIs on my website and i get the information from there.
There are some ways to set the value on the fields with JS? I'm tryed to set on value of the input field, but it don't worked.
(@asti)
Illustrious Member
Joined: 8 years ago
@danihml,
You can use the js code below. Just change the red-marked part in the code:
add_filter('wpdiscuz_js_options', function($jsOption) {
if (true) {//condition if user logged in other API and user exist
$jsOption['other_api_user_login'] = 'user_login';
$jsOption['other_api_user_email'] = 'user@email.com';
}
return $jsOption;
});
add_action('wpdiscuz_front_scripts', function() {
$wpdz = wpDiscuz();
$handle = 'wpdiscuz-ajax-js';
if ($wpdz->options->general['loadComboVersion']) {
$handle = 'wpdiscuz-combo-js';
}
wp_add_inline_script($handle, 'jQuery(document).ready(function ($) {
if(wpdiscuzAjaxObj.other_api_user_login){
$("#wpdcom .wc_name").val(wpdiscuzAjaxObj.other_api_user_login);
$("#wpdcom .wc_email").val(wpdiscuzAjaxObj.other_api_user_email);
Cookies.set("comment_author_email_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.other_api_user_email);
Cookies.set("comment_author_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.other_api_user_login);
}
});');
});
Pay attention to the commented part next to the red-marked value.
(@danihml)
Active Member
Joined: 3 years ago
thanks for the answer @Asti, but my console is getting an error on that part:
if ($wpdz -> options -> general['loadComboVersion'])
Uncaught SyntaxError: Unexpected token '>'. How that works exactly?
Here is my code modified for get the value from API:
const userInfo = getLastUserInfo();
add_filter('wpdiscuz_js_options', function ($jsOption) {
if (userInfo) {
$jsOption['username'] = userInfo.fullName;
$jsOption['uid'] = userInfo.uId;
for (let i = 0; i < userInfo.contactList.length; i++) {
const mail = userInfo.contactList[i];
if (documentCPF.type === 'email') {
$jsOption['usermail'] = mail;
}
}
}
return $jsOption;
});
add_action('wpdiscuz_front_scripts', function () {
$wpdz = wpDiscuz();
$handle = 'wpdiscuz-ajax-js';
if ($wpdz -> options -> general['loadComboVersion']) { //error part
$handle = 'wpdiscuz-combo-js';
}
wp_add_inline_script($handle,
`jQuery(document).ready(function ($) {
if (wpdiscuzAjaxObj.username) {
$("#wpdcom .wc_name").val(wpdiscuzAjaxObj.username);
$("#wpdcom .wc_email").val(wpdiscuzAjaxObj.usermail);
$("#wpdcom .wc_uid").val(wpdiscuzAjaxObj.uid);
Cookies.set("comment_author_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.username);
Cookies.set("comment_author_email_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.usermail);
Cookies.set("comment_author_uid_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.uid);
}
});`
);
});
(@asti)
Joined: 8 years ago
Illustrious Member
Posts: 8190
Mar 09, 2023 7:53 am
@danihml,
Please let us know where you have added the code. Is it added to the active theme functions.php file?
(@danihml)
Active Member
Joined: 3 years ago
I inserted the code directly into a .js file that is loaded into the blog pages
(@asti)
Joined: 8 years ago
Illustrious Member
Posts: 8190
Mar 10, 2023 1:12 pm
@danihml,
It's a PHP code and should be added in the active theme functions.php file.
(@danihml)
Joined: 3 years ago
Active Member
Posts: 4
Mar 14, 2023 6:42 pm
@asti there's no way to do that only on javascript? All my sources and data are on localStorage and JS apis, i dont know how to get all this data to PHP.