Skip to main content
added 49 characters in body
Source Link
bueltge
  • 17.1k
  • 7
  • 61
  • 97

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

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

block.json code

Edit function

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

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

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 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_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

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

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

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,
} );
deleted 18 characters in body
Source Link
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""register_latest_post_item");

function render_latest_post_property_itemrender_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 ;
  

}
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 ) {
$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 ;
  

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

}
added 234 characters in body
Source Link
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 ) {
$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 ;
  

}
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 ) {
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 ) {
$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 ;
  

}
added 24 characters in body
Source Link
Loading
added 423 characters in body
Source Link
Loading
added 605 characters in body
Source Link
Loading
deleted 3 characters in body
Source Link
Loading
deleted 9 characters in body
Source Link
Loading
deleted 27 characters in body
Source Link
Loading
added 469 characters in body
Source Link
Loading
Source Link
Loading