• Resolved majal

    (@majal)


    Hi Shea!

    First of all I’d like thank you for your awesome plugin.

    For a while after using code-snippets instead of hacking functions.php, i noticed this in my NGINX error logs:

    2019/06/11 01:54:54 [error] 8537#8537: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Invalid argument supplied for foreach() in /home/nginx/wordpress/wp-content/plugins/code-snippets/php/snippet-ops.php
    (361) : eval()'d code on line 5" while reading response header from upstream, client: 172.68.47.124, server: www.majlovesreg.one, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock
    :", host: "www.majlovesreg.one", referrer: "https://majlovesreg.one/"

    I checked some of the posts in your forum and found out that this may be caused by one of the snippets. I’ve identified one snippet with a foreach() in line 5. I tested it but all seems good, and the pages where the code should be running looks okay. Here’s the snippet:

    function maj_code_tag($content) {
      $post_tags = get_the_tags();
      $codecontent = $content;
    
      foreach( $post_tags as $tag) {
        if ( $tag->name === '{ code }' ) {
          ob_start();
            echo do_shortcode( '[majprism]' );
            $prepend = ob_get_contents();
          ob_end_clean();
    
          $append = '<p style="font-family: monospace; text-align: center;"><a href="/tag/code" style="color: #3c4858;"></></a></p>';
          $codecontent = $prepend . $content . $append;
        } 
      }
      return $codecontent;
    }
    
    add_filter('the_content', 'maj_code_tag');

    All it does is format all my posts tagged with { code }. It calls the shortcode [majprism] that links prism.js and prism.css to the page.

    All seems to work fine, but my OC-ness sometimes wants me to find the reason for, and then get rid of the error in nginx logs.

    Thanks for the help!

    Here are samples of my posts with { code } tag: https://www.majlovesreg.one/tag/code

    • This topic was modified 5 years, 5 months ago by majal.
    • This topic was modified 5 years, 5 months ago by majal. Reason: minor typo edit

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hey,

    Sorry for taking so long to respond to this.

    The cause of this error is that calling the get_the_tags() function will not always return an array. When the the_content filter can is applied outside of the loop, or when the post has no tags, it will return false, and if an error occurs it will return an instance of WP_Error, both of which foreach don’t know how to work with.

    To resolve this, you should add a check to the beginning of your function:

    function maj_code_tag($content) {
      $post_tags = get_the_tags();
      $codecontent = $content;
    
      if ( ! is_array( $post_tags ) ) {
      	return $content;
      }
    
      foreach( $post_tags as $tag) {
        if ( $tag->name === '{ code }' ) {
          ob_start();
            echo do_shortcode( '[majprism]' );
            $prepend = ob_get_contents();
          ob_end_clean();
    
          $append = '<p style="font-family: monospace; text-align: center;"><a href="/tag/code" style="color: #3c4858;"></></a></p>';
          $codecontent = $prepend . $content . $append;
        } 
      }
      return $codecontent;
    }
    
    add_filter('the_content', 'maj_code_tag');
    Thread Starter majal

    (@majal)

    Hi Shea!

    This is awesome! Thank you for giving this attention. I’m sure you’re also busy. Thank you for this nice code correction and making me understand why. It’s all good now in the server. Ta!

    Majal

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘FastCGI error from snippet-ops.php’ is closed to new replies.