{"id":4833,"date":"2025-01-07T15:26:03","date_gmt":"2025-01-07T15:26:03","guid":{"rendered":"https:\/\/helpcenter.greyd.io\/custom-actions-in-global-forms\/"},"modified":"2025-01-30T18:17:11","modified_gmt":"2025-01-30T17:17:11","slug":"custom-actions-in-global-forms","status":"publish","type":"post","link":"https:\/\/helpcenter.greyd.io\/custom-actions-in-global-forms\/","title":{"rendered":"How to create custom actions in globally connected forms"},"content":{"rendered":"

Greyd.Suite enables you to synchronize posts across several websites with Global Content. However, you can customize specific meta fields \u2014 like a forms admin mail_to<\/code> field \u2014 locally for individual posts without disrupting global synchronization.<\/p>\n

How it works<\/h4>\n

With a simple code snippet added to your child theme's functions.php<\/code> you can:<\/p>\n

    \n
  1. enable local editing of any meta field in the admin interface.<\/li>\n
  2. preserve local field values during global synchronization.<\/li>\n
  3. control updates to linked posts based on specific meta field conditions.<\/li>\n<\/ol>\n

    That\u2019s how it looks<\/h4>\n

    \"Backend<\/p>\n

    Adapting for Any Meta Field<\/h4>\n

    This approach is not limited to mail_to<\/code> or not even to the post type forms. You can customize any meta field for any post type by replacing mail_to<\/code> with your desired meta key and also adjusting the post type value. Adjust the snippet logic as needed for your use case.<\/p>\n

    Example Use Case<\/h4>\n

    Synchronize a global post type while allowing localized values, such as region-specific email addresses or descriptions, ensuring consistency and flexibility.<\/p>\n

    <\/p>\n

    \/**\n * Enable editing of the "mail_to" field in the Forms post type.\n * This functionality ensures that the "mail_to" field can be customized locally, even for globally synchronized forms.\n * It also ensures that buttons like "publish" remain accessible for posts with global content links.\n *\/\nfunction admin_forms_enable_metabox_field() {\n\tglobal $post_type;\n\tif ( 'tp_forms' === $post_type ) {\n\t\techo '<style>\n\t\t\ttextarea[name="mail_to"],\n\t\t\tbody.gc_linked .editor-post-publish-button__button {\n\t\t\t\tcursor: initial !important;\n\t\t\t\tpointer-events: unset !important;\n\t\t\t\topacity: 1 !important;\n\t\t\t}\n\t\t<\/style>';\n\t}\n}\nadd_action( 'admin_head', 'admin_forms_enable_metabox_field' );\n\n\/**\n * Prevent overwriting of the "mail_to" meta value when syncing forms via global content.\n * This ensures the locally defined "mail_to" value remains intact.\n *\n * @param mixed   $meta_value The current meta value for "mail_to".\n * @param int     $post_id    The ID of the post being synced.\n * @param WP_Post $post       The post object being synced.\n *\n * @return mixed The "mail_to" value from the local post.\n *\/\nfunction filter_mail_to_field_before_import( $meta_value, $post_id, $post ) {\n\treturn get_post_meta( $post_id, 'mail_to', true );\n}\nadd_filter( 'greyd_import_post_meta-mail_to', 'filter_mail_to_field_before_import', 10, 3 );\n\n\/**\n * Allow updates to linked posts only if the "mail_to" field is defined.\n * This provides granular control over synchronization behavior.\n *\n * @param bool $allow   Whether the update should be allowed.\n * @param int  $post_id The ID of the post being checked.\n *\n * @return bool Whether to allow the update of the linked post.\n *\/\nfunction allow_update_of_linked_post_with_mail_to_field( $allow, $post_id ) {\n\t$meta = get_post_meta( $post_id, 'mail_to', true );\n\tif ( ! empty( $meta ) ) {\n\t\t$allow = true;\n\t}\n\treturn $allow;\n}\nadd_filter( 'gc_allow_update_of_linked_post', 'allow_update_of_linked_post_with_mail_to_field', 10, 2 );\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

    Greyd.Suite enables you to synchronize posts across several websites with Global Content. However, you can customize specific meta fields \u2014 like a forms admin mail_to field \u2014 locally for individual posts without disrupting global synchronization. How it works With a simple code snippet added to your child theme's functions.php you can: enable local editing of […]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"greyd_block_editor_preview":[],"footnotes":""},"categories":[1],"tags":[],"topics":[586,584,590],"class_list":["post-4833","post","type-post","status-publish","format-standard","hentry","category-general","topics-forms-popups","topics-general-questions","topics-website-management"],"lang":"en","translations":{"en":4833},"pll_sync_post":[],"_links":{"self":[{"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/posts\/4833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/comments?post=4833"}],"version-history":[{"count":1,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/posts\/4833\/revisions"}],"predecessor-version":[{"id":4888,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/posts\/4833\/revisions\/4888"}],"wp:attachment":[{"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/media?parent=4833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/categories?post=4833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/tags?post=4833"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/helpcenter.greyd.io\/wp-json\/wp\/v2\/topics?post=4833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}