WordPress Code Snippets

WordPress is a content management system (CMS) that enables you to create a website or blog from scratch or to improve an existing website. WordPress is free and open source software released under the GPL.

A WordPress developer is someone who creates or modifies WordPress websites and/or plugins. They may also be involved in the testing or support of WordPress websites. WordPress developers can be employed by a company, be self-employed, or work as freelance contractors.

If you’re a WordPress developer, then you know that there are always new code snippets to learn. Here is a curated list of some of the best code snippets for WordPress developers. This list includes everything from creating custom post types to adding custom fields to posts. So, whether you’re a beginner or an experienced WordPress developer, you’ll find something here that will help you build better websites.

Curated List of WordPress Code Snippets

Database

Order Results by Meta Value
$args = [
	'post_type' 	   => 'post',
	'post_status'      => 'publish',
	'posts_per_page'   => 100,
	'offset'           => 0,
	'meta_key' => 'sub_header',
	'orderby' => 'meta_value',
	'order' => 'ASC',	
];

$posts = get_posts($args);
$query = new WP_Query($args);

Excerpt

Change The Default Excerpt Length
add_filter( 'excerpt_length', function($length) {
    return 100;
}, PHP_INT_MAX );

File

Access WordPress Functions in an External PHP File
include('wp-load.php');

//query user
if(is_user_logged_in() ) {
    $user = wp_get_current_user();
    echo "<p><b>Loggedin user:</b> {$user->data->user_login}</p>";
}
//query comments
global $wpdb;
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}comments"));
echo "<h3>Comments</h3>";
foreach ($comments as $key => $comment) {
    echo "<p>{$comment->comment_content}</p>";        
}

Shortcode

Get All Posts Containing a Shortcode
global $wpdb;
$results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[shortcode_name%'", ARRAY_N);

Troubleshoot

Fix “Installation Failed: Could Not Create Directory” Error
Login to your website's FTP via an FTP client like FileZilla
Choose 3 folders wp_content, wp_includes, and wp_admin then right-click to open the “File permissions…” option.
Set and tick these settings: Numeric value: 755, Recurse into subdirectories, Apply to directories only.
On the root of your WordPress installation, select all files and folders, right-click then choose the “File permissions…” option.
Set and tick these settings: Numeric value: 644, Recurse into subdirectories, Apply to files only.

Leave a Comment

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close