admin 管理员组文章数量: 1086019
I have a shortcode from functions, which retrieves a list of all users from active role. I have managed to get the list into the dropdown list with a little trickery if I wrap a div around the users and name it userdiv.
The problem is that no matter how I try to get the users into the list, they either show up as one choice (all users) or all the letters show up as a separate choice.
I have this in functions:
function userLooping($role)
{
$user_query = new WP_User_Query( array( 'role' => $role ) );
// User Loop
if ( !empty( $user_query->results ) ):
echo '<div id="userdiv">';
foreach ( $user_query->results as $user ):
echo '"';
echo $user->display_name; // display user name
echo '",';
endforeach;
echo '</div>';
endif;
}
add_shortcode( 'users_role', 'userLooping' );
I have put in [users_role]
to fetch the users to the page (may not need this).
The dropdown has ID input_5_13
Script to get the users to the dropdown:
<script>
var select = document.getElementById("input_5_13"),
arr = [document.getElementById("userdiv").innerHTML];
for(var i = 0; i < arr.length; i++)
{
var option = document.createElement("OPTION"),
txt = document.createTextNode(arr[i]);
option.appendChild(txt);
option.setAttribute("value",arr[i]);
select.insertBefore(option,select.lastChild);
}
</script>
When the script collects the text from userdiv
, it ends up as an option. A long line. If I remove [ ]
from the script, all letters are fetched as an alternative.
Tried adding "
before each user and ","
after, but I can't get it to work.
If I enter it manually in the script, it works. Then each user is added as a choice.
var select = document.getElementById("input_5_13"),
arr = ["user1","user2","user3"];
What am I doing wrong?
本文标签: functionsGet all users from role and add to dropdown (select)wordpress javascript
版权声明:本文标题:functions - Get all users from role and add to dropdown (select) - wordpress, javascript 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1737528849a1839055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论