1

As in wordpress Block Editor Handbook

I added a "meta block" to my theme, and it worked,

    function duke_blocks() {

    wp_register_script(
        'wb-blocks-js',
        get_template_directory_uri() . '/inc/meta-block.js',
        array( 'wp-blocks', 'wp-editor', 'wp-element','wp-components' )
    );
    register_block_type( 'dukeyin/audio-url', array(
        'editor_script' => 'wb-blocks-js',
    ) );

}
add_action( 'init', 'duke_blocks' );

function duke_register_block_meta() {

    register_meta( 'post', 'audio-url', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
        'object_subtype' => 'audio',
    ) );

}

add_action( 'init', 'duke_register_block_meta' );

and js:

( function( wp ) {
    var el = wp.element.createElement;
    var registerBlockType = wp.blocks.registerBlockType;
    var TextControl = wp.components.TextControl;

    registerBlockType( 'dukeyin/audio-url', {
    title: 'Audio file URL',
    icon: 'media-audio',
    category: 'layout',
    attributes: {
        blockValue: {
            type: 'string',
            source: 'meta',
            meta: 'audio-url',
        }
    },

    edit: function( props ) {
        var setAttributes = props.setAttributes;

        function updateBlockValue( blockValue ) {
            setAttributes({ blockValue });
        }

        return el(
           'div',
           { className: "audio-url" },
            el( 'h5',{}, 'URL to audio file:'),
            el (TextControl,
            {
                onChange: updateBlockValue,
                value: props.attributes. blockValue,
            })
        );
    },

    save: function( props ) {
         return null;
    },
    } );
} )( window.wp );

but the porblem is: this is an "Audio file" meta block, I want to show it only when "Audio" post format is selected, when I select other post formats, I want to hide this block, how to do it?

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.