forked from WordPress/WordPress-Coding-Standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for WordPress.DB.RestrictedFunctions
See WordPress#1722
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0"?> | ||
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd" | ||
title="Restricted Database Functions" | ||
> | ||
<standard> | ||
<![CDATA[ | ||
Avoid touching the database directly with PHP library functions. Use the $wpdb object and associated functions instead. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Invalid: Using MySQLi library to fetch posts"> | ||
<![CDATA[ | ||
$results = <em>mysqli_query</em>( | ||
$mysql, | ||
"SELECT * FROM wp_posts LIMIT 5" | ||
); | ||
]]> | ||
</code> | ||
<code title="Valid: Use WordPress functions"> | ||
<![CDATA[ | ||
$results = <em>get_posts()</em>; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
<code_comparison> | ||
<code title="Invalid: Using MySQLi library to insert a post"> | ||
<![CDATA[ | ||
<em>mysqli_query</em>( | ||
$mysql, | ||
"INSERT INTO wp_posts (post_title) | ||
VALUES ('Title')" | ||
); | ||
]]> | ||
</code> | ||
<code title="Valid: Use WordPress functions"> | ||
<![CDATA[ | ||
<em>wp_insert_post</em>( | ||
array( 'post_title' => 'Title' ) | ||
); | ||
// or... | ||
global $wpdb; | ||
<em>$wpdb->insert</em>( | ||
"{$wpdb->prefix}_posts", | ||
array( 'post_title' => 'Title' ), | ||
array( '%s' ) | ||
); | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |