Skip to main content
7 of 11
added 423 characters in body

How do I save meta key value in the save function?

The edit code saves the heading value in meta. How do I save it in the save function?

The render_callback is as below. I tried passing "render": "file: path-to/php.php" in block.json but it doesn'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_property_item");

function render_latest_post_property_item( $value, $object, $field_name ) {

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,
} );