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.
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.
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); } });` ); });
I inserted the code directly into a .js file that is loaded into the blog pages