Skip to content

Commit a289638

Browse files
committed
Add documentation for WordPress.DB.RestrictedFunctions
See WordPress#1722
1 parent 29488fe commit a289638

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Restricted Database Functions"
5+
>
6+
<standard>
7+
<![CDATA[
8+
Avoid touching the database directly with PHP library functions. Use the $wpdb object and associated functions instead.
9+
]]>
10+
</standard>
11+
<code_comparison>
12+
<code title="Invalid: Using MySQLi library to fetch posts">
13+
<![CDATA[
14+
$results = <em>mysqli_query</em>(
15+
$mysql,
16+
"SELECT * FROM wp_posts LIMIT 5"
17+
);
18+
]]>
19+
</code>
20+
<code title="Valid: Use WordPress functions">
21+
<![CDATA[
22+
$results = <em>get_posts()</em>;
23+
]]>
24+
</code>
25+
</code_comparison>
26+
<code_comparison>
27+
<code title="Invalid: Using MySQLi library to insert a post">
28+
<![CDATA[
29+
<em>mysqli_query</em>(
30+
$mysql,
31+
"INSERT INTO wp_posts (post_title)
32+
VALUES ('Title')"
33+
);
34+
]]>
35+
</code>
36+
<code title="Valid: Use WordPress functions">
37+
<![CDATA[
38+
<em>wp_insert_post</em>(
39+
array( 'post_title' => 'Title' )
40+
);
41+
42+
// or...
43+
44+
global $wpdb;
45+
<em>$wpdb->insert</em>(
46+
"{$wpdb->prefix}_posts",
47+
array( 'post_title' => 'Title' ),
48+
array( '%s' )
49+
);
50+
]]>
51+
</code>
52+
</code_comparison>
53+
</documentation>

0 commit comments

Comments
 (0)