0

The edit code saves the heading value in meta. How do I display the saved data using PHP "renderblock" function?

The render_callback is as below. I tried passing "render": "file: path-to/php.php" in block.json, but it didn't work.

function register_latest_post_item() {
    register_block_type("create-block/items", [ "render_callback" => "render_latest_post_item"
    ]);
}
add_action("plugins_loaded", "register_latest_post_item");

function render_latest_post_item( $value, $object, $field_name ) {
$post_id = get_the_ID(); 

$heading        = get_post_meta( $post_id, "heading", true );
$posts_output   = "<p>Post ID:'.$post_id.'</p><br/> <p>Post ID:'.$heading.'</p>";
return $posts_output ;
}

block.json code

{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "create-block/items",
"version": "0.1.0",
"title": "Items",
"category": "widgets",
"icon": "smiley",
"description": "Example block scaffolded with Create Block tool.",
"attributes": {
    "heading": {
        "type": "string",
        "default": ""
    
    }
},
"supports": {
    "html": false
},
"textdomain": "property",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:../render/render.php"
}

Edit function

import { __ } from '@wordpress/i18n';
import { useEntityProp } from "@wordpress/core-data";
import { useSelect } from "@wordpress/data";
const {
    TextControl,
} = wp.components;

import { useBlockProps } from '@wordpress/block-editor';
import './editor.scss';

export default function Edit(props) {

    const [meta, setMeta] = useEntityProp("postType", "listing", "meta");

    const heading = meta["heading"];

    const onChangeheading = (newValue) => {
        setMeta({ ...meta, heading: newValue });
    };

    return (
        <div {...useBlockProps()}>
        
            <div>
                <TextControl
                    tagName="h1"
                    label="title"
                    className="card-title"
                    value={heading}
                    onChange={onChangeheading}
                />
            </div>
        </div>
    );
}

index.js

import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";

import './style.scss';
import Edit from './edit';
import save from './save';
import metadata from './block.json';

registerBlockType( metadata.name, {
    edit: Edit,
    save,
} );
5
  • You don’t. The save function returns the HTML that is to be saved for the block. If your block saves a value as meta and outputs it on the server using get_post_meta() in a render function then the save function for your block should be null. Commented Dec 31, 2022 at 5:35
  • When I use render-callback the data is displayed in the front-end but the block in the editor gives the error 'Your site doesn’t include support for the "create-block/items" block.' I added more code to the question. Could you please check and let me what is wrong?
    – Ramesh
    Commented Dec 31, 2022 at 9:26
  • I also noticed that you have wrong parameters in render_callback function. Parameters are standardised and you cannot change them.
    – Lovor
    Commented Jan 2, 2023 at 22:53
  • @Ramesh What is the code for file:./index.js? You've only shared the edit function. Commented Jan 3, 2023 at 3:51
  • Jacob Peattie, I have updated the code. Code for index.js and PHP code.
    – Ramesh
    Commented Jan 3, 2023 at 17:25

1 Answer 1

0

render_callback is intended just for displaying block. All other operations, including saving metadata, should be done in the Edit function.

I come across many of such questions because developers who ask are familiar with PHP and not familiar with block editor WP - React ecosystem, and they want to do something in PHP which is not meant to be done in that way.

It would be better that you rephrase your question and ask how to save what you want in Edit function. If you just need that data in render_callback, then there is no need to use metadata at all. Just save what you require in block attributes and read it in render_callback, it has attributes parameter inside function call ($block_attributes).

4
  • @bueltge, the edit function saves the data as expected. The problem is I am not able to display it in the frontend using PHP rendercallback function. When I use "register_block_type" the block in the backend stops working.
    – Ramesh
    Commented Jan 3, 2023 at 17:17
  • @bueltge, I am building a dynamic block hence I need to save it in meta. one block adds the data in the custom post and another block displays the data from the custom post on a page. but i also want the post data to be visible when the post is viewed. Just like a blog post works.
    – Ramesh
    Commented Jan 3, 2023 at 17:20
  • @Ramesh >When I use "register_block_type" the block in the backend stops working. That's odd. You did not show PHP render_callback so I can't even guess why.
    – Lovor
    Commented Jan 4, 2023 at 18:49
  • @Ramesh I understand that where you show your data on page, you have to have dynamic block, since it has to update value from database on opening the page. However, this block which saves data to meta and is probably intended to be shown only on custom post type "listing" does not need to be dynamic. And it is going to be slower if it saves the data in the way you wrote. Save data to attributes and save data to meta only upon page saving, in useEffect hook. In save function, show data from attributes.
    – Lovor
    Commented Jan 4, 2023 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.