0

I made a small script which echoes the content of all Wordpress Posts on post_updated, deleted_post and trashed_post actions.

add_action('post_updated', 'echoAllPosts');
add_action('deleted_post', 'echoAllPosts');
add_action('trashed_post', 'echoAllPosts'); 
function echoAllPosts($post = null) {
    $args = array(
        'post_type' => array('post','page'),
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'ignore_sticky_posts' => true,
    );
    $qry = new WP_Query($args);

    foreach ($qry->posts as $existingPost) {
        $postContent = $existingPost->post_content;

        $postContent = apply_filters('the_content', $content_markup);
        $postContent = str_replace(']]>', ']]>', $postContent);

        echo $postContent;
    }
}

This is working fine on post_updated action. But when I move a post to trash or delete one (triggering deleted_post or trashed_post action) the Block elements in the post are not parsed to HTML. Instead the textarea input is rendered.

For example

Correct parsing:

<h5 class="c-stack"><b></p> <p>Test Lead.</p> <p></b></h5>

Incorrect parsing:

<div class="acf-block-fields acf-fields"> 
<div class="acf-field acf-field-textarea acf-field-5f75ce4e1d0e2 is-required" data-name="lead_text" data-type="textarea" data-key="field_5f75ce4e1d0e2" data-required="1"> 
<div class="acf-label"> 
<label for="acf-block_61c0518326ca5-field_5f75ce4e1d0e2">Lead <span class="acf-required">*</span></label></div> 
<div class="acf-input"> <textarea id="acf-block_61c0518326ca5-field_5f75ce4e1d0e2" name="acf-block_61c0518326ca5[field_5f75ce4e1d0e2]" rows="8" required="required">Test Lead.</textarea>
<p class="description">Write your lead text here</p></p>  
</div>
</div>
</div>

Where is this odd behaviour coming from?

1
  • This seems to be specific to ACF. It probably thinks it's in the admin so is rendering an admin version of the block. You'd need to check with their support or their documentation about how to avoid this in your situation. Third party plugins like ACF are off topic here. Dec 21 '21 at 11:56

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.