Skip to main content
added 372 characters in body
Source Link

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this:

$keys = $wpdb->get_results(
        $wpdb->prepare(
            'SELECT * FROM %1s WHERE key_type = "geo_key";',
            self::MAP_KEY_TABLE
        ),
        OBJECT
    );  // db call ok.
    $map_key_geo = $keys[0]->key_value;

I get a warning that: "message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

As suggested in a comment I tried $1$s but the warning still persists.

WP Version 6.1.1, PHP Version 8.0.18, PHP_CodeSniffer version 3.7.1

I don't know if the guidelines are version specific but they were installed like this.

composer require --dev wp-coding-standards/wpcs
composer require --dev dealerdirect/phpcodesniffer-composer-installer

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this:

$keys = $wpdb->get_results(
        $wpdb->prepare(
            'SELECT * FROM %1s WHERE key_type = "geo_key";',
            self::MAP_KEY_TABLE
        ),
        OBJECT
    );  // db call ok.
    $map_key_geo = $keys[0]->key_value;

I get a warning that: "message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this:

$keys = $wpdb->get_results(
        $wpdb->prepare(
            'SELECT * FROM %1s WHERE key_type = "geo_key";',
            self::MAP_KEY_TABLE
        ),
        OBJECT
    );  // db call ok.
    $map_key_geo = $keys[0]->key_value;

I get a warning that: "message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

As suggested in a comment I tried $1$s but the warning still persists.

WP Version 6.1.1, PHP Version 8.0.18, PHP_CodeSniffer version 3.7.1

I don't know if the guidelines are version specific but they were installed like this.

composer require --dev wp-coding-standards/wpcs
composer require --dev dealerdirect/phpcodesniffer-composer-installer

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

added 211 characters in body
Source Link

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this I get a warning that:

"message": "Complex$keys placeholders= used$wpdb->get_results(
 for values in the query string in $wpdb->prepare() 
 will NOT be quoted automagically. Found:      'SELECT * FROM %1s WHERE key_type = "geo_key";',
            self::MAP_KEY_TABLE
        ),
        OBJECT
    );  // db call ok."
    $map_key_geo = $keys[0]->key_value;

I get a warning that: "message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this I get a warning that:

"message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this:

$keys = $wpdb->get_results(
        $wpdb->prepare( 
            'SELECT * FROM %1s WHERE key_type = "geo_key";',
            self::MAP_KEY_TABLE
        ),
        OBJECT
    );  // db call ok.
    $map_key_geo = $keys[0]->key_value;

I get a warning that: "message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.

Source Link

$wpdb->prepare referencing a const without a coding guideline warning

I have defined my database table names as constants in a base class and use them throughout my code.

protected const MAP_KEY_TABLE = 'eds_map_keys';

If I use them like this I get a warning to use place holders.

$keys   = $wpdb->get_results(
        'SELECT * FROM ' . self::MAP_KEY_TABLE . ' WHERE key_type = "map_key"',
        OBJECT
    );
    $mapkey = $keys[0]->key_value;

if I use place holders like this I get a warning that:

"message": "Complex placeholders used for values in the query string in $wpdb->prepare() will NOT be quoted automagically. Found: %1s."

I'd really like to get rid of the messages about coding style but have been stuck on this one for a while. Any suggestions greatly appreciated.