• Resolved mouli a11n

    (@mouli)


    I have been using the following to load default content when the_block() is empty:

    if (has_block('cta 1')) {
               the_block('cta 1');
     } else {
               echo "We'll load default content then");
     }

    This was working fine until recently when it appears that has_block() returns true even with an empty block so the default is not loaded.

    I have updated everything to the latest and greatest but still get no content.

    Any ideas much appreciated.

    https://wordpress.org/plugins/multiple-content-blocks/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Harold Angenent

    (@harold-angenent)

    That’s strange. Are you sure there’s no space (or non-breaking space) or new line in the editor?

    Thread Starter mouli a11n

    (@mouli)

    Thanks for your thoughts.
    It is indeed strange.
    The text editor shows nothing ie. no spaces or returns so I looked in the postmeta table for the page in question and there is a meta key as follows:
    _mcb-cta-1
    but the meta value is empty.
    I checked for new lines or spaces and there aren’t any.
    Can you suggest anything I can do to pin point the issue?

    Thread Starter mouli a11n

    (@mouli)

    OK I have a clue.
    I think I had some content in these block initially which I deleted hoping that it would return to the default content.
    When I inspect the code in the browser for the block it shows only an empty paragraph.
    This isn’t in the postmeta field so is it possible that wordpress itself is adding the <p> tags which is causing the has_block() to return TRUE and resulting in the output being blank since it is an empty <p>.
    In fact checking all the other pages that should have default content in these blocks, they all contain empty <p> tags which is why they are blank.
    Could it be that in this function
    get_the_block()
    used in the has_block() function, the defaults being set to:
    'apply_filters' => true
    enable the apply_filters switch which might be causing the issue.
    As a test I set
    apply_filters => false
    and the default content shows again. 🙂
    So I guess I need to figure out how to set apply_filters=>false when I call the has_content() but it’s to late now.

    Thread Starter mouli a11n

    (@mouli)

    So might this be correct:

    has_block( $name, array(
        'label'         =>'',
        'type'          => '',
        'apply_filters' => false,
    ) );

    `

    Thread Starter mouli a11n

    (@mouli)

    I tested this theory today and it works perfectly so can I suggest that this might be a bug and that the has_block() function should default to ‘apply_filters’ => false.

    Plugin Author Harold Angenent

    (@harold-angenent)

    Thanks for your research on this. I’ve created an issue on the Github page, we’ll look into this as soon as we have some time to do so.

    https://github.com/trendwerk/multiple-content-blocks/issues/65

    Plugin Author Harold Angenent

    (@harold-angenent)

    I have tried several things on the latest WordPress, but could not reproduce this bug. Upon removing all content from a block, WordPress doesn’t add the paragraphs.

    What WordPress version were you using? Could you give me some steps on how to reproduce this on a clean install?

    Plugin Author Harold Angenent

    (@harold-angenent)

    Cannot reproduce, so marking this as resolved for now. Let me know if you have any more information.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘has_block() not working as expected’ is closed to new replies.