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 |1 Answer
Reset to default 0To 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
版权声明:本文标题:plugin development code standard not matching for SQL query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297918a1930115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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