admin管理员组文章数量:1129005
I'm developing gutenberg block as plugin. I what to ensure that when my block added to the post, category that I created on plugin init is selected.
For now I found some useful information here: Stackoverflow link
For now I'm doing like this, but problem is that I need to get category's id, because for now I can set category selected by it's name.
So I need to receive somehow category id, that was created by me. I guess I know it's name and slug.
// some meta info about category taxnomy, I don't know how to use.
const allCategories = select('core').getTaxonomy('category');
//here we receive a list of already selected categories ids.
const categories = select( 'core/editor' ).getEditedPostAttribute( 'categories' );
if(!(allCategories && categories)){
return;
}
//rest og code is taken from the link above. except line with dispatch
if(! categories.includes(CATEGORY_NAME)){
const newCategories = [...categories, CATEGORY_NAME];
//Here I have slight different syntax from link above. I need to pass category id to select it, not name. I have 5 because for now I know my category id.
dispatch( 'core/editor' ).editPost( { 'categories': [5] } );
const is_category_panel_open = select( 'core/editor' ).isEditorPanelOpened( 'taxonomy-panel-category' );
if ( is_category_panel_open ) {
// Close and re-open the category panel to reload data / refresh the UI
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
}
}
for newcomers select, dispatch come from wordpress package wordpress dev docs
category is created on 'init' action with wp_insert_term in PHP.
I already tried to receive meta information about category with selecting 'core' and 'core/editor'
Looking through wordpress core store docs
wordpress post editor store docs
wordpress editor's ui store
MY SOLUTION WITH API FETCH
import {CATEGORY_SLUG} from '../../consts';
import {select, dispatch} from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';
export function ensureCategorySelected(){
const categoriesSelected = select( 'core/editor' ).getEditedPostAttribute( 'categories' );
if(!categoriesSelected) return;
ensure(categoriesSelected);
}
/** @param {Array} categoriesSelected */
async function ensure(categoriesSelected){
var id = 0;
const categories = await apiFetch({
path: `/wp/v2/categories`,
method: 'GET',
});
if(!categories){
return;
}
for(const category of categories){
if(category.slug === CATEGORY_SLUG){
id = category.id;
break;
}
}
if(id === 0){
return;
}
//Create new array for updating selected categories
const newCategories = [...categoriesSelected, id];
//Update Post with new categories
dispatch( 'core/editor' ).editPost( { 'categories': newCategories } );
//Get State of Category Panel
const is_category_panel_open = select( 'core/editor' ).isEditorPanelOpened( 'taxonomy-panel-category' );
// Verify if the category panel is open
if ( is_category_panel_open ) {
// Close and re-open the category panel to reload data / refresh the UI
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
}
}
I'm developing gutenberg block as plugin. I what to ensure that when my block added to the post, category that I created on plugin init is selected.
For now I found some useful information here: Stackoverflow link
For now I'm doing like this, but problem is that I need to get category's id, because for now I can set category selected by it's name.
So I need to receive somehow category id, that was created by me. I guess I know it's name and slug.
// some meta info about category taxnomy, I don't know how to use.
const allCategories = select('core').getTaxonomy('category');
//here we receive a list of already selected categories ids.
const categories = select( 'core/editor' ).getEditedPostAttribute( 'categories' );
if(!(allCategories && categories)){
return;
}
//rest og code is taken from the link above. except line with dispatch
if(! categories.includes(CATEGORY_NAME)){
const newCategories = [...categories, CATEGORY_NAME];
//Here I have slight different syntax from link above. I need to pass category id to select it, not name. I have 5 because for now I know my category id.
dispatch( 'core/editor' ).editPost( { 'categories': [5] } );
const is_category_panel_open = select( 'core/editor' ).isEditorPanelOpened( 'taxonomy-panel-category' );
if ( is_category_panel_open ) {
// Close and re-open the category panel to reload data / refresh the UI
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
}
}
for newcomers select, dispatch come from wordpress package wordpress dev docs
category is created on 'init' action with wp_insert_term in PHP.
I already tried to receive meta information about category with selecting 'core' and 'core/editor'
Looking through wordpress core store docs
wordpress post editor store docs
wordpress editor's ui store
MY SOLUTION WITH API FETCH
import {CATEGORY_SLUG} from '../../consts';
import {select, dispatch} from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';
export function ensureCategorySelected(){
const categoriesSelected = select( 'core/editor' ).getEditedPostAttribute( 'categories' );
if(!categoriesSelected) return;
ensure(categoriesSelected);
}
/** @param {Array} categoriesSelected */
async function ensure(categoriesSelected){
var id = 0;
const categories = await apiFetch({
path: `/wp/v2/categories`,
method: 'GET',
});
if(!categories){
return;
}
for(const category of categories){
if(category.slug === CATEGORY_SLUG){
id = category.id;
break;
}
}
if(id === 0){
return;
}
//Create new array for updating selected categories
const newCategories = [...categoriesSelected, id];
//Update Post with new categories
dispatch( 'core/editor' ).editPost( { 'categories': newCategories } );
//Get State of Category Panel
const is_category_panel_open = select( 'core/editor' ).isEditorPanelOpened( 'taxonomy-panel-category' );
// Verify if the category panel is open
if ( is_category_panel_open ) {
// Close and re-open the category panel to reload data / refresh the UI
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
dispatch( 'core/editor' ).toggleEditorPanelOpened( 'taxonomy-panel-category' );
}
}
Share
Improve this question
asked Jan 8 at 11:44
Egor DemeshkoEgor Demeshko
112 bronze badges
New contributor
Egor Demeshko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1 Answer
Reset to default 0You can also cache the category ID to reduce API calls, add error handling for reliability, and optimize performance by only updating categories when necessary.
本文标签: wordpressselect specific parameter when block is addedStack Overflow
版权声明:本文标题:wordpress - select specific parameter when block is added - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736732940a1950096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论