0

My client's website has a single post that when updated/saved using the block editor displays the following in the browser console: XHR Post Status 500 https://www.wanderwoman.ca/wp-json/wp/v2/posts/5206?_locale=user. I have tested a few other posts and they have all saved correctly using the block editor. This same post fails to display any content after the post title and author. No errors are displayed in the browser console when viewing the post on the front-end.

The post can be updated and saved when using the Quick Edit function on the All Posts page.

The post contains paragraphs, images, YouTube embeds, and Amazon referral links in the custom HTML block.

Here is the full JSON request displayed in the browser console:

content `<!-- wp:paragraph -->\n<p>Hiking the Wave in Arizona is an experience of a lifetime, but you have to win a lottery to get a permit to do it. According to the Bureau of Land Management (BLM), 200,589 people applied for 7,300 permits to hike the wave in 2019. The odds of success in 2019 were about 3.5 percent and the number of applicants is similar today. My husband and I were in that 3.5 percent. The BLM recently announced that as of Feb 1, 2021 they are increasing the number of permits from 20 people per d…low">Lotsa Motsa Pizza</a>. At the time we visited, they had an all-you-can-eat pizza buffet for $5 per person. The food was good - not great, but we couldn't complain about the price. </p>\n<!-- /wp:paragraph -->\n\n
<!-- wp:paragraph -->\n<p><strong>Related: </strong>If you like fascinating rock formations, you'll love Bryce Canyon. Read our post, <a href="https://www.wanderwoman.ca//bryce-canyon/" target="_blank" rel="noreferrer noopener">The Best Hikes in Bryce Canyon</a>. </p>\n<!-- /wp:paragraph -->`
id  5206

Here is the full JSON response displayed in the browser console:

XHRPOST https://www.wanderwoman.ca/wp-json/wp/v2/posts/5206?_locale=user
[HTTP/2 500  921ms]
code "internal_server_error"
message '<p>There has been a critical error on this website.</p><p><a href="https://wordpress.org/support/article/faq-troubleshooting/">Learn more about troubleshooting WordPress.</a></p>'
data Object { status: 500 }
additional_errors []

Update with error debugging

#1 [internal function]: forqy_image_content_attributes_callback()
#2 /var/www/staging.domain.ca/wp-content/themes/salmon/vendor/forqy/core/lib/functions/image.php(173): preg_replace_callback()
#3 /var/www/staging.domain.ca/wp-includes/class-wp-hook.php(324): forqy_image_content_attributes()
#4 /var/www/staging.domain.ca/wp-includes/plugin.php(205): WP_Hook->apply_filters()
#5 /var/www/staging.domain.ca/wp-content/plugins/wordpress-seo/src/builders/indexable-link-builder.php(117): apply_filters()
#6 /var/www/staging.domain.ca/wp-content/plugins/wordpress-seo/src/integrations/watchers/indexable-post-watcher.php(209): Yoast\WP\SEO\Builders\Indexable_Link_Builder->build()
#7 /var/www/staging.domain.ca/wp-includes/class-wp-hook.php(326): Yoast\WP\SEO\Integrations\Watchers\Indexable_Post_Watcher->build_indexable()
#8 /var/www/staging.domain.ca/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#9 /var/www/staging.domain.ca/wp-includes/plugin.php(517): WP_Hook->do_action()
#10 /var/www/staging.domain.ca/wp-includes/post.php(4771): do_action()
#11 /var/www/staging.domain.ca/wp-includes/post.php(4862): wp_insert_post()
#12 /var/www/staging.domain.ca/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(880): wp_update_post()
#13 /var/www/staging.domain.ca/wp-includes/rest-api/class-wp-rest-server.php(1193): WP_REST_Posts_Controller->update_item()
#14 /var/www/staging.domain.ca/wp-includes/rest-api/class-wp-rest-server.php(1041): WP_REST_Server->respond_to_request()
#15 /var/www/staging.domain.ca/wp-includes/rest-api/class-wp-rest-server.php(431): WP_REST_Server->dispatch()
#16 /var/www/staging.domain.ca/wp-includes/rest-api.php(424): WP_REST_Server->serve_request()
#17 /var/www/staging.domain.ca/wp-includes/class-wp-hook.php(324): rest_api_loaded()
#18 /var/www/staging.domain.ca/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#19 /var/www/staging.domain.ca/wp-includes/plugin.php(565): WP_Hook->do_action()
#20 /var/www/staging.domain.ca/wp-includes/class-wp.php(418): do_action_ref_array()
#21 /var/www/staging.domain.ca/wp-includes/class-wp.php(813): WP->parse_request()
#22 /var/www/staging.domain.ca/wp-includes/functions.php(1336): WP->main()
#23 /var/www/staging.domain.ca/wp-blog-header.php(16): wp()
#24 /var/www/staging.domain.ca/index.php(17): require('...')
#25 {main} thrown in /var/www/staging.domain.ca/wp-content/themes/salmon/vendor/forqy/core/lib/functions/image.php on line 20

Update with problematic code

function forqy_image_placeholder( ?int $width = 4, ?int $height = 3 ) {
    return esc_attr( 'data:image/svg+xml,%3Csvg%20width%3D%22' . $width . '%22%20height%3D%22' . $height . '%22%20xmlns%3D%22http://www.w3.org/2000/svg%22%20viewBox%3D%220%200%20' . $width . '%20' . $height . '%22%3E%3C/svg%3E' );
}
add_filter( 'forqy_image_placeholder', 'forqy_image_placeholder', 10, 2 );

function forqy_image_content_attributes( $content ) {
    if ( apply_filters( 'forqy_image_content_lazy', true ) && ! is_admin() && current_theme_supports( 'forqy-image' ) ) {
        return preg_replace_callback( '/<(img)([^>]+?)(>(.*?)<\/\\1>|[\/]?>)/si', 'forqy_image_content_attributes_callback', $content );
    } else {
        return $content;
    }
}
add_filter( 'the_content', 'forqy_image_content_attributes' );

if ( ! empty( $attributes[ 'width' ] ) && ! empty( $attributes[ 'height' ] ) ) {
    $attributes[ 'src' ] = forqy_image_placeholder( $attributes[ 'width' ], $attributes[ 'height' ] );
} else {
    // check if 'src' is a file to prevent error 'Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden' with getimagesize() function
    if ( is_file( $attributes_o[ 'src' ] ) ) {
        list ( $width, $height ) = getimagesize( $attributes_o[ 'src' ] );
        $attributes[ 'src' ]    = forqy_image_placeholder( $width, $height );
        $attributes[ 'width' ]  = $width;
        $attributes[ 'height' ] = $height;
    }
}

Updated with additional function

function forqy_image_content_attributes_callback( $matches ) {
    // $matches[0] = original
    // $matches[1] = tag
    // $matches[2] = attributes
    // $matches[3] = >

    // original attributes
    $attributes_o_kses_hair = wp_kses_hair( $matches[ 2 ], wp_allowed_protocols() );

    if ( empty( $attributes_o_kses_hair[ 'src' ] ) ) {
        return $matches[ 0 ];
    }

    // old attributes
    $attributes_o = array();

    foreach ( $attributes_o_kses_hair as $name => $attribute ) {
        $attributes_o[ $name ] = $attribute[ 'value' ];
    }

    // new attributes
    $attributes = $attributes_o;

    // check if not an external image & image file exists
    if ( strpos( $attributes[ 'src' ], '//' ) !== false && file_exists( $attributes[ 'src' ] ) ) {

        // image type is *.png
        if ( @exif_imagetype( $attributes[ 'src' ] ) == IMAGETYPE_PNG ) {

            if ( ! empty( $attributes[ 'class' ] ) ) {
                $attributes[ 'class' ] = 'fy-image--png ' . $attributes[ 'class' ];
            } else {
                $attributes[ 'class' ] = 'fy-image--png';
            }
        }
    }

    // class
    if ( get_post_type() == 'slide' ) {
        $attributes[ 'class' ] = 'fy-content__image swiper-lazy ' . $attributes[ 'class' ] ?? $attributes[ 'class' ];
    } else {
        $attributes[ 'class' ] = 'fy-content__image fy-lazy js-lazy ' . $attributes[ 'class' ] ?? $attributes[ 'class' ];
    }

    // size
    // add width/height attribute if missing
    if ( ! isset( $attributes[ 'width' ] ) && ! isset( $attributes[ 'height' ] ) && ! empty( $attributes[ 'src' ] ) ) {
        $attachment_id = attachment_url_to_postid( $attributes[ 'src' ] );

        if ( is_int( $attachment_id ) ) {
            $attachment_meta = wp_get_attachment_metadata( $attachment_id );

            if ( isset( $attachment_meta[ 'width' ] ) ) {
                $attributes[ 'width' ] = $attachment_meta[ 'width' ];
            }
            if ( isset( $attachment_meta[ 'height' ] ) ) {
                $attributes[ 'height' ] = $attachment_meta[ 'height' ];
            }
        }
    }

    // size & placeholder
    if ( ! empty( $attributes[ 'width' ] ) && ! empty( $attributes[ 'height' ] ) ) {
        $attributes[ 'src' ] = forqy_image_placeholder( $attributes[ 'width' ], $attributes[ 'height' ] );
    } else {
        // check if 'src' is a file to prevent error 'Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden' with getimagesize() function
        if ( is_file( $attributes_o[ 'src' ] ) ) {
            list ( $width, $height ) = getimagesize( $attributes_o[ 'src' ] );

            $attributes[ 'src' ]    = forqy_image_placeholder( $width, $height );
            $attributes[ 'width' ]  = $width;
            $attributes[ 'height' ] = $height;
        }
    }

    // 'src' to 'data-src'
    $attributes[ 'data-src' ] = $attributes_o[ 'src' ];

    // 'srcset' to 'data-srcset'
    if ( ! empty( $attributes[ 'srcset' ] ) ) {
        $attributes[ 'data-srcset' ] = $attributes_o[ 'srcset' ];
        unset( $attributes[ 'srcset' ] );
    }

    // 'sizes' to 'data-sizes'
    if ( ! empty( $attributes[ 'sizes' ] ) ) {
        $attributes[ 'data-sizes' ] = $attributes_o[ 'sizes' ];
        unset( $attributes[ 'sizes' ] );
    }

    // reformat attributes
    $attributes_s = array();

    foreach ( $attributes as $attribute => $value ) {

        if ( '' === $value ) {
            $attributes_s[] = sprintf( '%s', $attribute );
        } else {
            $attributes_s[] = sprintf( '%s="%s"', $attribute, esc_attr( $value ) );
        }

    }

    $attributes_s = implode( ' ', $attributes_s );

    // loading
    $loading = forqy_image_loading();

    return sprintf( '<img %1$s>%2$s', $attributes_s, $loading );

}

Any ideas on what could be causing this issue?

2 Answers 2

1

Based on the error logs and the behavior you've described, the issue with the WordPress post not updating properly and causing a 500 internal server error seems to be related to a function in your theme or a plugin that is interacting with the content, particularly images. Here's a breakdown of what the logs suggest and some steps you can take to resolve the issue:

Error Source: The error trace points to a function forqy_image_content_attributes_callback() in your theme's image.php file. This function is likely modifying image attributes and is being called by a regular expression match (preg_replace_callback()).

Interaction with Other Plugins: It seems that this function is being triggered during a filter application in the wordpress-seo plugin (specifically Yoast SEO's Indexable Link Builder). The error occurs when the post is being saved and the SEO plugin tries to process the content.

Problematic Content: Since the issue is isolated to a specific post, it's possible that there's something unique in this post's content (like a specific image or HTML structure) that's causing the function to fail.

Steps to Resolve: Check the Specific Post's Content: Look for any unusual HTML or image attributes in the post. You mentioned custom HTML blocks; ensure that they are correctly formatted.

Debug the Theme Function: Examine the forqy_image_content_attributes_callback() function in your theme. You might need to temporarily comment out parts of this function to see if it resolves the issue. This will help isolate the problem within that function.

Theme and Plugin Conflict Check: Temporarily switch to a default WordPress theme (like Twenty Twenty-One) and try updating the post. If it works, the issue is likely within your current theme. Also, temporarily disable plugins, especially Yoast SEO, to see if there's a conflict.

Check for Updates: Ensure that your theme, WordPress core, and all plugins are up to date. Sometimes these issues are resolved in updates.

Consult Theme/Plugin Support: If the problem persists, reach out to the support teams for your theme and the Yoast SEO plugin. They might be aware of this issue and could offer a specific solution.

Custom Fix: If you're comfortable with PHP, you might modify the forqy_image_content_attributes_callback() function to handle whatever edge case is causing the issue. Remember to back up the original file before making changes.

Server Error Logs: Check your server error logs for any additional information that might not be visible in the WordPress debug or console output.

Restore from Backup: If you have a recent backup from before the issue started occurring, consider restoring that version of the post and then carefully reapplying any changes.

Remember, when debugging, it's always good to have a staging environment so that your live site is not affected.

Try replacing the function with this one:

function forqy_image_placeholder( ?int $width = 4, ?int $height = 3 ) {
    try {
        // Validate width and height
        if ($width <= 0 || $height <= 0) {
            throw new InvalidArgumentException('Width and Height must be positive integers.');
        }

        // Generate SVG
        $svg = '<svg width="' . $width . '" height="' . $height . '" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' . $width . ' ' . $height . '"></svg>';

        // Encode SVG for use in URL
        $svg_encoded = rawurlencode($svg);

        // Construct the data URL
        $data_url = 'data:image/svg+xml,' . $svg_encoded;

        // Return the escaped URL
        return esc_attr($data_url);
    } catch (Exception $e) {
        // Log the error
        error_log('Error in forqy_image_placeholder: ' . $e->getMessage());

        // Optionally, return a default placeholder or false
        return false;
    }
}
6
  • I have added the problematic code to my original question in case you can offer some insight. Jan 25 at 16:13
  • Please add the contents of this function for me to see: forqy_image_content_attributes_callback It should be in the same file Jan 25 at 17:14
  • I have added the callback function to my question Jan 26 at 11:48
  • Mike, try replacing that function with the one I posted here at the bottom of this answer: wordpress.stackexchange.com/a/422061/238918 Then if you get an error check your error_log to see what it was Jan 26 at 15:27
  • I have added your code and the post saves correctly, but now the post's output on the front-end is broken. The featured image is cutoff in the source code: <img class="fy-lazy js-lazy" alt="Hiking the Wave in Arizona Just Got Easier!" src=". No other HTML is rendered after. Jan 26 at 22:27
0

I am going to add a list of things to try below but the first thing would try is increasing your memory limit. I have had the same issue and it was resolved with memory limit increase. PHP Memory Limit: A common issue is the PHP memory limit. Increase the limit in the wp-config.php file by adding define('WP_MEMORY_LIMIT', '256M');.

The issue you're experiencing with the specific post on your WordPress site, indicated by the HTTP 500 error when saving in the block editor and the inability to display content beyond the title and author, suggests a server-side problem. Here are several steps you can take to diagnose and possibly resolve the issue:

1. Check for Plugin Conflicts Deactivate Plugins: Sometimes, plugins can conflict with the block editor. Deactivate all plugins and then try editing the post. If it works, reactivate plugins one by one to identify the culprit. Specific Focus: Pay special attention to plugins that interact with the editor, such as those adding blocks, modifying editor behavior, or SEO plugins.

2. Theme Conflict Test Switch Themes: Temporarily switch to a default WordPress theme (like Twenty Twenty-One) and try editing the post. If it works, the issue might be with the theme.

3. Investigate Server Logs Error Logs: Check the error logs on your server for any specific error messages at the time of the update attempt. These logs can provide detailed insights into what's causing the 500 error. PHP Memory Limit: A common issue is the PHP memory limit. Increase the limit in the wp-config.php file by adding define('WP_MEMORY_LIMIT', '256M');.

4. Check Post Content Content Size: A very large post or excessive metadata can cause issues. If the post is extremely long or contains many embeds and images, try splitting it into smaller posts. Invalid HTML or Shortcodes: Sometimes, invalid HTML or broken shortcodes can cause issues in rendering or saving posts.

5. Debugging Enable WP_DEBUG: Modify wp-config.php to enable debugging and log errors. Add the following lines:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

After this, try updating the post again and then check the debug.log file in the wp-content directory for errors.

6. REST API Specific Since the error occurs with an XHR request to the REST API, it's possible the issue is specific to the REST API. Look for: Security Plugins: Some security plugins block REST API requests. Hosting Restrictions: Some hosting providers have restrictions or special settings for REST API usage.

7. Revisions and Autosave Clear Revisions: Excessive revisions can sometimes cause issues. You can clear revisions with a plugin or via a SQL command (backup before doing this).

8. WordPress and PHP Version Ensure WordPress and PHP are up to date. Sometimes, compatibility issues can arise from outdated software.

9. Check Custom Code If you have custom code (in functions.php, a site-specific plugin, or a child theme), ensure it’s not causing the issue. Particularly focus on any code that modifies editor behavior.

10. User Permissions Verify that your user role has the appropriate permissions to edit the post.

3
  • 1
    Thanks for the tips. I changed the theme on the staging site to 2024 and the issue was resolved. I have enabled debugging and have added the output to my original question for review. Jan 25 at 3:15
  • I will post my response below to a new answer Jan 25 at 3:32
  • Check the answer I just posted, it is a reply to your error log entry Jan 25 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.