Skip to content

Instantly share code, notes, and snippets.

@derweili
Last active April 29, 2022 17:09
Show Gist options
  • Save derweili/7f674b3055fd0759f002d892e614ea60 to your computer and use it in GitHub Desktop.
Save derweili/7f674b3055fd0759f002d892e614ea60 to your computer and use it in GitHub Desktop.
Disable Links in ACF Block-Edit Method
/*
* Wrap all ACF Blocks in Disabled block to disable all links in edit view
*/
const { createHigherOrderComponent } = wp.compose;
const { Fragment } = wp.element;
import { Disabled } from '@wordpress/components';
const withDisabledCompontent = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
// only wrap block when it's a acf block in preview mode
if (
! props.name.startsWith('acf/')
|| 'preview' != props.attributes.mode
) {
return <BlockEdit { ...props } />;
};
return (
<Fragment>
<Disabled>
<BlockEdit { ...props } />
</Disabled>
</Fragment>
);
};
}, "withInspectorControl" );
wp.hooks.addFilter( 'editor.BlockEdit', 'derweili/disabled-acf-blocks', withDisabledCompontent );
@NRDay
Copy link

NRDay commented Dec 16, 2019

Thanks..

@fabien-corail
Copy link

Thanks but not working on my end... How should that be enqueued ?

@nicmare
Copy link

nicmare commented Aug 26, 2021

how to use that piece of code please?

@derweili
Copy link
Author

derweili commented Aug 27, 2021

@nicmare

how to use that piece of code please?

You need to add this code to a plugin or theme. This is ESNext JavaScript Code so you need a built setup.

WordPress has a documentation on this: https://developer.wordpress.org/block-editor/how-to-guides/javascript/js-build-setup/
And here's the link to the general JavaScript Block Editor Guide: https://developer.wordpress.org/block-editor/how-to-guides/javascript/

You can also use create-guten-block as a boilerplate

https://github.com/ahmadawais/create-guten-block

But insted of registering a new block, you register a filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment