-
Hello, I have developped a plugin that register one block
import {getTutorials} from '../utils';
class TutorialCard extends React.Component {
constructor() {
super();
this.state = { data: [], loaded : false };
}
async componentDidMount() {
let tutorials = await getTutorials();
console.log(tutorials);
this.setState({ data : tutorials, loaded : true });
}
render() {
const data = this.state.data;
// if(!this.state.loaded) {
// return (<p>Loading...</p>);
// }
// if(data.length == 0) {
// return (<p>{ 'This page offers special deals for our education clients. Contact us to be a part of our students community.' }</p>)
// }
return (data.map((item) => (
<a href={item.permalink} className="PM-tutorial-card">
<figure className="PM-tutorial-card-img"><img src={item.image} alt={"Tutorial " + item.title} /></figure>
<div className="PM-tutorial-card-info">
<h3>{item.title}</h3>
<p dangerouslySetInnerHTML={{__html: item.description}} />
</div>
</a>
)));
}
}
export default TutorialCard;
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* The save function defines the way in which the different attributes should
* be combined into the final markup, which is then serialized by the block
* editor into `post_content`.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
*
* @return {WPElement} Element to render.
*/
import TutorialCard from './model/tutorialCard';
export default function save() {
return (
<div { ...useBlockProps.save() }>
<TutorialCard />
</div>
);
}
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';
/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
import TutorialCard from './model/tutorialCard';
export default function Edit() {
return (
<div { ...useBlockProps() }>
<TutorialCard />
</div>
);
} I use @wordpress/create-block |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
I think If the API you have prepared is accessible in PHP and the block is not reactive on the front end, you could access the API with the render_callback function and render the results dynamically. |
Beta Was this translation helpful? Give feedback.
-
Uh in this case there is no way to call an API with it ?! (direclty from js) And that's weird that everything working well in edit.js mode but not in save.js And the API is an intern API (custom taxonomy) |
Beta Was this translation helpful? Give feedback.
-
If you need to retrieve your own data, apiFetch may be useful.
Since |
Beta Was this translation helpful? Give feedback.
-
I am already using it import apiFetch from "@wordpress/api-fetch";
export function getTutorials() {
return apiFetch({
path: '/tutorials/v1/search'
});
}
Yea I saw that during my researche that woocommerce achieve this by using a specific method |
Beta Was this translation helpful? Give feedback.
I think
save.js
defines the markup to be stored in the database. It will be output as static markup on the front end, so it will not work as a React app.If the API you have prepared is accessible in PHP and the block is not reactive on the front end, you could access the API with the render_callback function and render the results dynamically.