• 0

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Can I access attributes with source meta from the Save class?

I've been building a plugin for a client that allows the user to select some colours and a set of fonts per post that is then applied throughout the post in every block.

The information is stored in the post's metadata

This is done through a sidepanel inside the post and since all blocks on the page need to access the data, the information is stored in the post's metadata.

Storing and retrieving the information is fine from the side panel and the retrieving information from the Edit class of each block is fine too (The blocks don't need to change the data).

Unfortunately, I'm having trouble with the Save class.

I know that I can't use the data module and withSelect from within the Save class, but the information in the documentation led me to believe that if I define the information as an attribute with source "meta" I could use it in the same way as a normal attribute.

"From here, meta attributes can be read and written by a block using the same interface as any attribute..." - Documentation

This is what I've done with the Edit class successfully, but it doesn't work with the Save class. Instead, the attributes simply aren't available (ie. when I console log the attributes it doesn't include those ones).

My thought at the moment

I'm thinking the documentation must mean "only within the edit class", but I'd like to know if anyone else knows for sure...

The code

I don't think the code is very relevant because it works everywhere except the Save class, but here are my attribute definitions just fyi:

attributes: {     buttons: {         type: "array",         source: "query",         selector: "a",         query: {             text: {                 type: "string",                 source: "html",                 attribute: "a"             },             url: {                 type: "string",                 source: "attribute",                 attribute: "href"             },             colorKey: {                 type: "string",                 source: "attribute",                 attribute: "data-color-key"             },         },     },     fontSetKey: {         type: "string",         source: "meta",         meta: "eimMeta_fontSetKey",     },      primaryColor: {         type: "string",         source: "meta",         meta: "eimMeta_primaryColor",     },     primaryContrastingShade: {         type: "string",         source: "meta",         meta: "eimMeta_primaryContrastingShade",     },      secondaryColor: {         type: "string",         source: "meta",         meta: "eimMeta_secondaryColor",     },     secondaryContrastingShade: {         type: "string",         source: "meta",         meta: "eimMeta_secondaryContrastingShade",     }, }, 

And here is my save function

import { Component } from "@wordpress/element"; import { RichText } from "@wordpress/editor";  import plugin from "../../plugin"; import category from "../category"; import block from "./block"; import { preparePostMetaForUse, getColorHexWithKey } from "../../common/values";  class Save extends Component {     render() {         const { attributes } = this.props;         const { className, buttons } = attributes;          console.log("Save attributes:");         console.log(attributes);          const postMeta = preparePostMetaForUse( attributes );           let jsxButtons = buttons.map((button) => ( <>                            ));          return 
; } } export default Save;
Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]