admin管理员组

文章数量:1122832

In WordPress before submitting plugin need to check code with the PCP

It keeps showing warning of Use of a direct database call is discouraged

I tried to follow all the coding standard given by WordPress and also tried to solve the error from previous question and other suggestions.

Things i have tried already is adding this comment after my code /* db call ok; no cache ok */ still it showing warning on this line of my code $scripts = $wpdb->get_results

This is my code

global $wpdb;
$table_name = $wpdb->prefix . 'my_table';
$current_post_type = get_post_type();
$current_post_id = get_the_ID();

$script_column = 'header_script';

$cache_key = 'my_custom_scripts_cache_' . $current_post_type;
$scripts = wp_cache_get($cache_key);
if ($scripts === false) {
    $scripts = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT %i, postsid_to_exclude, inclscripttag_header, inclscripttag_footer FROM %i WHERE FIND_IN_SET(%s, target_post_types) AND status = %d",
            $script_column,
            $table_name,
            $current_post_type,
            1
        )
    ); /* db call ok; no cache ok */
    wp_cache_set($cache_key, $scripts);
}

Also i have checked this solution suggested here , but using this can reject my plugin from WordPress? or it is safe to use? is there any other solution for this?

In WordPress before submitting plugin need to check code with the PCP

It keeps showing warning of Use of a direct database call is discouraged

I tried to follow all the coding standard given by WordPress and also tried to solve the error from previous question and other suggestions.

Things i have tried already is adding this comment after my code /* db call ok; no cache ok */ still it showing warning on this line of my code $scripts = $wpdb->get_results

This is my code

global $wpdb;
$table_name = $wpdb->prefix . 'my_table';
$current_post_type = get_post_type();
$current_post_id = get_the_ID();

$script_column = 'header_script';

$cache_key = 'my_custom_scripts_cache_' . $current_post_type;
$scripts = wp_cache_get($cache_key);
if ($scripts === false) {
    $scripts = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT %i, postsid_to_exclude, inclscripttag_header, inclscripttag_footer FROM %i WHERE FIND_IN_SET(%s, target_post_types) AND status = %d",
            $script_column,
            $table_name,
            $current_post_type,
            1
        )
    ); /* db call ok; no cache ok */
    wp_cache_set($cache_key, $scripts);
}

Also i have checked this solution suggested here , but using this can reject my plugin from WordPress? or it is safe to use? is there any other solution for this?

Share Improve this question asked Aug 14, 2024 at 7:15 Dev ThakkarDev Thakkar 211 bronze badge 2
  • Try establishing the $wpdb->prepare() first, then the $wpdb->get_results() - I don't know if that'll make a difference but I have never seen the prepare inside of the get_results. – Tony Djukic Commented Aug 14, 2024 at 11:29
  • Tried that but showing same notice. – Dev Thakkar Commented Aug 14, 2024 at 12:16
Add a comment  | 

1 Answer 1

Reset to default 0

To be honest, I'm not sure if you can trust this plugin 100%.

I run the test on my plugin, that is already in repo, and it showed me few false alarms. I put your code inside my plugin code and run the test again and... it didn't show me the error you have.

Also, my Code Sniffer in Visual Studio code shows different errors that are not reported by the plugin:

  • You do not need to use double quote
  • You are not using Yoda condition
  • %i is available only for WordPress 6.2 and above, so it is safer to use %s
  • = are not aligned

Code after changes:

    global $wpdb;
    $table_name        = $wpdb->prefix . 'my_table';
    $current_post_type = get_post_type();
    $current_post_id   = get_the_ID(); // this one is unused

    $script_column = 'header_script';

    $cache_key = 'my_custom_scripts_cache_' . $current_post_type;
    $scripts   = wp_cache_get( $cache_key );

    if ( false === $scripts ) {
        $scripts = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT %s, postsid_to_exclude, inclscripttag_header, inclscripttag_footer FROM %s WHERE FIND_IN_SET(%s, target_post_types) AND status = %d',
                $script_column,
                $table_name,
                $current_post_type,
                1
            )
        ); /* db call ok; no cache ok */
        wp_cache_set( $cache_key, $scripts );
    }

(I recommend to use Code Sniffer inside your IDE, will save you time.

I'm using this one: https://github.com/wongjn/vscode-php-sniffer And you need to use it with WordPress Coding Standards (WPCS): https://github.com/WordPress/WordPress-Coding-Standards?tab=readme-ov-file

本文标签: plugin development code standard not matching for SQL query