我將 Gutenberg 與 WordPress 一起用于學生網站。
我想顯示一個包含所有學生的串列 ( roles : student),并從串列中排除已登錄的學生。
我嘗試了兩種解決方案。
第一個具有
getUsers()功能的解決方案。當我像管理員一樣登錄時,一切正常,但是當學生登錄時,他沒有查看串列的權限。只有管??理員才有權限。使用自定義 API 路由的第二種解決方案。我得到了一個未決的承諾。
第一個解決方案:
import { __ } from '@wordpress/i18n';
import { CheckboxControl } from '@wordpress/components';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useState, setState, useEffect } from '@wordpress/element';
const metaboxStudents = () => {
const postType = useSelect( ( select ) => {
return select( 'core/editor' ).getCurrentPostType();
});
if ( postType !== 'subject-imposed' ) {
return null;
}
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
const authors = useSelect( ( select ) => {
return select( 'core' ).getUsers( { roles: 'student' } );
}, [] );
if ( !posts ) {
return null;
}
const handleCheckboxChange = (data) => {
const isChecked = meta._metafield_students.some(checkedCheckbox => checkedCheckbox === data);
if (isChecked) {
setMeta( { _metafield_students: meta._metafield_students.filter( ( checkedCheckbox) => checkedCheckbox !== data) } );
} else {
setMeta( { _metafield_students: meta._metafield_students.concat(data) } );
}
};
return(
<PluginDocumentSettingPanel
name="list-students"
title={ __( 'List of students', 'ccn-gut' ) }
className='editor-styles-metabox'
>
<div className="gut-checkboxes-group">
{ posts.map( ( data ) => (
wp.data.select("core").getCurrentUser().id !== data.id
? (
<CheckboxControl
label={ data.name }
key={`student-${data.id}`}
value={ data.id }
checked={ meta._metafield_students.some(checkedCheckbox => checkedCheckbox === data.id) }
onChange={ () => handleCheckboxChange(data.id) }
/>
) : null
) ) }
</div>
</PluginDocumentSettingPanel>
);
};
registerPlugin('plugin-document-students', {
render: metaboxStudents,
icon: null
});
第二種解決方案:
用于我的 WordPress 插件的 PHP:
wp_localize_script( 'wp-api', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
));
用于 API 路由的 PHP:
function student_api_rest() {
register_rest_route('api/v1/', 'students', array(
'methods' => 'GET',
'callback' => 'student_api_results'
));
}
function student_api_results($data) {
....
}
index.js:
import apiFetch from '@wordpress/api-fetch';
wp.apiFetch.use( apiFetch.createNonceMiddleware( wpApiSettings.nonce ) );
const [users, setUsers] = useState( null );
useEffect( () => {
wp.apiFetch( { path: '/api/v1/students' } ).then(
(result) => {
setUsers( result );
}
)
}, []);
console.log(users);
選擇哪種解決方案以及如何解決這兩種解決方案之一?許可VS承諾待處理
uj5u.com熱心網友回復:
您應該能夠為“學生”的自定義用戶型別添加功能。尋找學生角色被激活的位置,它應該看起來像這樣
add_role( $role, $display_name, $capabilities );
list_users我相信您希望將其添加為一項功能。您可以在此處找到完整的功能串列https://wordpress.org/support/article/roles-and-capabilities/并直接鏈接到該list_users部分的鏈接https://wordpress.org/support/article/roles -and-capabilities/#list_users
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487982.html
下一篇:具有任何順序重復組的正則運算式
