Support » Plugin: Category Icons » Here's how to solve "Warning: Missing argument 2 for wpdb::prepare()"

  • The Category Icons plugin has evidently been abandoned, and has not kept up with syntax changes in more recent versions of WP. I’m still using it because I haven’t found anything else that does what I want, and if you’re still using it you know that the plugin now triggers a message that looks like this:
    Warning: Missing argument 2 for wpdb::prepare(), called in [...]\wp-content\plugins\category-icons\category_icons.php on line 1047 and defined in [...]

    This first part of the message tells you the location of the code that is at the root of the problem. The rest of message (“and defined in…”) is just listing the place(s) that are triggering the warning by referring to the problem.

    Please note that I am not a developer, and I’m only starting to learn PHP. But I did a lot of googling, and I was able to extract the solution from here:
    https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/
    That article is written for theme/plugin authors, and it’s full of information about why the warning happens when it didn’t used to, but the important point is this:
    To solve the warning about a missing 2nd argument… add a 2nd argument.

    ==========
    NOTE: This is a workaround that involves editing at least one of the actual plugin files, which is not something I would normally consider doing [because any changes would be lost in a subsequent upgrade], but since this plugin seems to have been abandoned, I decided I wasn’t going to worry about losing my changes.
    ==========

    So, onward. The warning message tells you which line of the file has the problem — in this case, Line 1047. Open the problematic file category_icons.php in a real text editor — NOT a word processor. (I recommend Notepad++, because it helpfully displays the file with line numbers. You can also use the Plugin Editor from the WP Dashboard, but it doesn’t have line numbers, and you’ll have to search for the following line of code.)

    Line 1047 is this:
    $wpdb->query($wpdb->prepare("CREATE TABLE IF NOT EXISTS$wpdb->ig_caticons(cat_idINT NOT NULL ,priorityINT NOT NULL ,iconTEXT NOT NULL ,small_iconTEXT NOT NULL , PRIMARY KEY (cat_id))"));

    (You don’t need to understand what all this means, but it helps if you understand how parentheses are nested and how quotes and commas are used to structure an expression.)

    Everything between the double quotes — one is near the beginning, just before CREATE, and the other is the 3rd character from the end, before the last two parentheses — is the first argument. Back when this plugin was developed, only one argument was necessary. But WordPress has advanced significantly, and this prepare function now requires two arguments (again, see that reference for plugin authors, above).

    What we need to do is insert something as a placeholder for the 2nd argument, to satisfy the function so that the warning will not be triggered.

    Go to the end of Line 1047 and insert a comma, a space, and two single quote marks between the double quotes and the second-to-last parenthesis.

    The very end of Line 1047 should go from this:
    ))"));

    To this:
    ))", ''));

    That’s all there is to it. The two single quotes create an empty (Null) 2nd argument for the function, and it’s perfectly happy with that.

    Before you close your file, go to Line 1338 and make exactly the same change — this is another instance of prepare, and if you skip it you’ll get exactly the same warning message, but with the new line number.

    Now save your file and upload it back to your server, and you’ll be good to go.

    https://wordpress.org/plugins/category-icons/

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Here's how to solve "Warning: Missing argument 2 for wpdb::prepare()"’ is closed to new replies.