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

API

add_action('rest_api_init', function () {
  register_rest_route( 'custom-rest/v1', 'flutter',[
            'methods'  => 'GET',
            'callback' => 'custom_rest_posts_by_types',
            'permission_callback' => function() {
		          return true;
		      }
  ]);

});

function custom_rest_posts_by_types($request){
	$types = ['type_a', 'type_b', 'type_c'];

	global $wpdb;
	$result = [];

	foreach ($types as $type) {
		//get post by type
		$posts_by_type =  $wpdb->get_results("SELECT DISTINCT pm.post_id FROM {$wpdb->prefix}postmeta as pm WHERE pm.meta_key LIKE 'component_type' AND lower(pm.meta_value) LIKE '%".strtolower($type)."%'");
		$post_ids = [];
		foreach ($posts_by_type as $key => $p) {
			$result[$p->post_id] = [
				'id' => $p->post_id,
			];
		}

		
	}
	return new WP_REST_Response($result, 200);

}

Customization

.entry-title {
    display: none;
}
post-ID .entry-title {
    display: none;
}

Database

$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

add_filter( 'excerpt_length', function($length) {
    return 100;
}, PHP_INT_MAX );

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

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

Troubleshoot

//Add these 2 lines to wp-config.php to show errors
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "IP Limit"
AuthType Basic
<LIMIT GET>
order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
allow from xxx.xxx.xxx.xxx
</LIMIT>
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. Required fields are marked *

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