Get attachment image from media library

wp_get_attachment_image function can accept four values as you can see:

wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

Examples

<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), false, array( "class" => "img-fluid" ) ); ?>
<?php echo wp_get_attachment_image( 5151, 'medium', false, array( "class" => "img-fluid" ) ); ?>

Get Featured Image

Display the featured image of a post in a tag.

echo get_the_post_thumbnail();

Additionally if you want to grab a specific size for the featured image you can fill out the second argument with an image size. Also can add class onto image.

echo get_the_post_thumbnail( get_the_ID(), 'medium', array('class' => 'img-fluid') );

Return the URL of the featured post image.

echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );

WordPress Data Sanitization/Escaping

Sanitization: Securing Input

Sanitization is the process of cleaning or filtering your input data. Whether the data is from a user or an API or web service, you use sanitizing when you don’t know what to expect or you don’t want to be strict with data validation.

The easiest way to sanitize data is with built-in WordPress functions.

The sanitize_*() series of helper functions provide an effective way to ensure you’re ending up with safe data, and they require minimal effort on your part:

Questions for interview

1. You’re the lead developer on a new project that requires a CMS (content management system).  What tech stack would you use and why?

Generally I would make sure that whatever stack I chose didn’t create issues with updates in the future. Who will be doing future updates? What technologies do those employees already know? I would get an overview of what they are doing now and where they want to be in the future technology wise. So I’d first follow the direction of the current environment around me.

Otherwise because I know the LAMP stack, I’d be choosing this option since I can follow in more detail.

There are two major CMS I would choose from: Drupal or WordPress

If the project needs to have a customized website that needs to handle a lot of data I’d choose this as WordPress we are usually installing ACF to deal with fields. Drupal has a better method built into their CMS.

WordPress I’d choose for most other websites as you can still customize everything and there is a lot of documentation online and many people who know how to develop in WordPress.

For retail websites I may choose Shopify since they focus on ecommerce only.

2. (scenario) : We’re creating a blogging platform for teams in government departments who want to publish articles on a regular basis. We are using a WordPress multisite installation so that we can create a new instance per team and let them self-administer. We want to improve the flow for adding new users. The following questions relate to the end-to-end developer experience of creating a new flow for adding users (eg, user roles, database structure, data input, testing changes). They are oriented towards WordPress but are generic enough to be relevant to any CMS framework.

3. (scenario): We have a small web application which is missing functionality and has some bugs.  We need your help. The current page looks like https://codesandbox.io/s/pedantic-fast-2pcw9

Figure out the database structure of multisites

Do unit tests for WordPress and React

Child Themes and Theme Structure

How to create a child theme

  • Create a folder in your themes directory giving it the name of new theme.
  • Create style.css file and print this inside where template references the name of the parent theme that’s referenced in enqueue:
Theme Name:   Theme Name Here
Theme URI:    https://www.themeurl.com/
Description:  A Parent child theme 
Author:       Christine Wilson
Author URI:   https://www.christinewilson.ca
Template:     twentytwentyone
Version:      1.0.0
Text Domain:  themenamehere
  • Create a file called functions.php and add enqueue scripts info:
/* enqueue scripts and style from parent theme */
    
function main_scripts() {
    wp_enqueue_style( 'themenamehere', get_stylesheet_uri(),
    array( 'twenty-twenty-one-style' ), wp_get_theme()-&gt;get('Version') );
}
add_action( 'wp_enqueue_scripts', 'main_scripts');

Theme structure

Here’s all the possibilities inside your theme:

Possible folder structure (taken from WordPress)

assets (dir)
      - css (dir)
      - images (dir)
      - js (dir)
inc (dir)
template-parts (dir)
      - footer (dir)
      - header (dir)
      - navigation (dir)
      - page (dir)
      - post (dir)
404.php
archive.php
comments.php
footer.php
front-page.php
functions.php
header.php
index.php
page.php
README.txt
rtl.css
screenshot.png
search.php
searchform.php
sidebar.php
single.php
style.css

How to optimize WordPress site performance?

  • Use a CDN
  • Use a caching plugin or edit .htaccess to cache different file types for different lengths of time and use gzip, etc
  • Use a simple theme/framework
  • Split comments, products and long posts into smaller pages
  • Use less plugins and only use well established ones
  • Keep WordPress updated
  • Host images on a subdomain

  • A separate domain prevents your domain's cookies from being sent for every image request, where they're not needed. Reduces the bandwidth overhead for each request.
  • Most browsers will only make a certain number of HTTP requests at one time to a particular domain, so serving images and other static content off a second domain potentially doubles the number of HTTP requests at one time.
  • You can conceivably use a different, lighter weight webserver like nginx/lighttpd to serve the static content.