-1

I am gradually implementing PHP on WordPress sites that I manage, and it is very common for me to see PHP Warnings such as "PHP Warning: Undefined array key 'SOME-TERM'" tied to specific themes and plugins. Typically, I have just reported these to the developers, but in this case I am working with a plugin that is no longer being supported. I am hoping someone can help me with fixing the code on the script.

Here are the lines of code throwing off the error:

if( $class->args[ 'display_category_description' ] ){
            $desc = empty( $cat->description ) ? null : $cat->description;

            if( !empty( $desc ) ){
                $header .= '<p class="category-description">'
                           . $desc .
                           '</p>';
            }
        }

I am guessing that this error is being thrown because the "category description" field is blank in some of the database entries --- but I'm not sure of the appropriate syntax to fix this.

miken32
  • 42,008
  • 16
  • 111
  • 154
Armar21
  • 11
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Progman May 01 '21 at 09:25

1 Answers1

0

OK -- answering my own question, I seemed to have resolved the problem by wrapping the code snippet like this:

if(isset($UNDEFINED-VARIABLE)) {

       [MY CODE SECTION]
}

I tested this and it looks like everything is functioning the way I want without throwing off errors.

Armar21
  • 11
  • That does not look like valid PHP code – Nico Haase May 01 '21 at 09:28
  • How would you write it better? I'm not a coder and I just modeled my code after another person's suggestion that I found online -- and as noted it seems to work. The script seems to be functioning as intended, and no more error messages in my log. But I'm open to suggestions -- the point is that with my beginner-level of experience, I really need things spelled out for me. – Armar21 May 02 '21 at 12:02
  • Yeah, and that's the problem: for beginners, `[MY CODE SECTION]` might look like valid code, which it is not – Nico Haase May 02 '21 at 12:40
  • You're fine, Armar. Nico has apparently never seen example code before. – As If Aug 18 '23 at 20:03