Sample NGINX config files

Many times NGINX conf reference comes handy. Here I am copying couple of NGINX conf setups for future reference.

Single WordPress

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        #server_name _;
        server_name example.com www.example.com;

        location / {
                #try_files $uri $uri/ =404;
                #try_files $uri/ index.php$args;
	        try_files $uri /index.php$is_args$args;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

	location /wp-admin/ {
		index index.php;
		try_files $uri $uri/ /index.php$args;
	}
}

WordPress multi site

Please not the additional details at top and before closing bracket of server block. For installation of multiple please check post How to Install WordPress Multisite

map $http_host $blogid {
    default 0;
    include /var/www/yourwebsite/wp-content/uploads/nginx-helper/map.conf;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        #server_name _;
        server_name example.com www.example.com;

        location / {
                #try_files $uri $uri/ =404;
                #try_files $uri/ index.php$args;
	        try_files $uri /index.php$is_args$args;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

	location /wp-admin/ {
		index index.php;
		try_files $uri $uri/ /index.php$args;
	}
	
	location ~ ^/files/(.*)$ {
	  try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
	  access_log off; log_not_found off; expires max;
	}

	location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
		expires 24h;
		log_not_found off;
	}

	location ^~ /blogs.dir {
		internal;
		alias /var/www/fintrekking/wp-content/blogs.dir ;
		access_log off; log_not_found off;      expires max;
	}


	if (!-e $request_filename) {
		rewrite /wp-admin$ $scheme://$host$uri/ permanent;
		rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
		rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
	}
	
}

How to Create Custom Query for WordPress

WordPress has a default query which fetches posts and display it in descending manner. If you are working on any WordPress customisation, you would come across a requirement to fetch WordPress posts in customised manner such as Fetching last 10 posts, Fetching post having specific category or tag.

Fortunately, we dont have to write MySQL level queries to get required data (and that what makes WordPress special), we can call a WP_Query function by passing required fields as input, we can get required data. This logic has two parts.

  1. Query : The query will select WordPress posts from the MySQL database based on our instructions.
  2. Loop : It will output the post information on the page. Each instance of output array will have attributes such as post thumbnail, title, date, and author etc.

Here is simple structure to help you understand custom WP_Query

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

 

You can customized the query as per your requirement.

<?php

// Creating array
$args = array( 
'posts_per_page' => 15, 
'orderby' => 'date', 
'order' ='ASC'
);
// Initiate the custom query
$custom_query = new WP_Query( $args );

?>

By Default, it will search for posts, if you want to search for specific custom post type, you can simply add a parameter in array

<?php

// Creating array
$args = array( 
'post_type' => 'vehicle',
'posts_per_page' => 15, 
'orderby' => 'date', 
'order' ='ASC'
);
// Initiate the custom query
$custom_query = new WP_Query( $args );

?>

How to restrict plugin access to multisite

WordPress multi-site is a great tool, it helps in creating network blogs with much easy. However, you need to be careful when allowing user full control. WordPress plugins can be misused by sub sites.

If you want to disable plugins, you can disable is very easily. Login using super admin and go to

Network Admin –> Settings –> Network Settings

Scroll all the way to down and un-check the plugin check box

How to get post tags and categories

Many times you might need to display post tags or categories along with the post. This can be achieved by using following code


<?php
echo get_the_tag_list('<p>Tags: ',', ','</p>');
?>

If you are using custom taxonomy, you can use following code


<?php echo get_the_term_list( $post->ID, 'modernquotetaxonomy', 'Tags: ', ', ' ); ?>

Useful WordPress Functions

WordPress provide may useful functions. Many time we don’t have to reinvent the wheel. Here is quick summary of important functions

 

Function Remark
<?php echo get_theme_root_uri(); ?> This gives theme directory
<?php echo get_template_directory_uri(); ?> This gives parent theme
<?php echo get_stylesheet_directory_uri(); ?> This gives child theme

 

How to show breadcrumb navigation in WordPress

Breadcrumb generally do not get their due importance in many themes. Once of the reason being most of the users misuse categories and tags. Unless you have clear understanding of how to use Categories and Tags, you wont be able to understand importance of Breadcrumb.

Breadcrumb helps yours users to navigate across your websites in most intuitive way.  Here is an example. This site has list of tourist places and Breadcrumb makes this very intuitive

Please follow below mention steps

Step#1 Enable Breadcrumbs using Yoast SEO plugin

Go to SEO–>Search Appearance

Please note, if you are using custom post type, you will get an option to choose which taxonomy needs to be used as Breadcrumb

Setp#2 Add the code

You can add below code where you need Breadcrumbs needs to be displayed. It varies across different themes. For most of the themes, if you copy this at the end of header.php file, it should work.

Alternatively, you can copy below code just above single.php just above the line which displays post title.

 <?php if ( function_exists('yoast_breadcrumb') ) 
 {yoast_breadcrumb('<p id="breadcrumbs">','</p>');} ?>

If breadcrumbs are still not visible, please let me know.

How to redirect HTTP to HTTPS in Nginx

When I had installed Letsencrypt SSL, it was working fine. Some of the sites were automatically getting redirected from http to https however some some sites, it was not happening. Here is the server block code. Simply copy paste this before your existing server block and it will redirect http to https.


server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}

Please do let me know if this addressed your requirement.

How to Add a Favicon to Your WordPress Blog

Favicon gives website a professional look. Most of the reputed websites use facicon and its displayed in browser as below:

Another reason for adding favicon is that it makes your website more trsutful. Adding favicon in WordPress is very easy.

Step#1 Decide the Favicon

Favicon can be logo, character or any image that would be your websites identity. Mostly favicons are websites logo.

Step#2 Upload favicon to WordPress website

Go to Appearance–> Customise–>Site Identity

Here, click on Change image, select whichever image you have finalised in Step#1 and click on Publish.

Thats all, you websites favicon/identity is updated

Troubleshooting:

Even after publishing favicon was not getting updated for me.I opened website in another tab, still issue persisted. To refresh favicon follow following steps

  1. Hover over tab
  2. Right Click
  3. Select reload
  4. Your favicon should now be refreshed

 

Documentation for DW Question & Answer Plugin

This is quick notes for DW Questions & Answer Plugin

Short codes:

Ask Question page : [dwqa-submit-question-form]

List of Questions:  [dwqa-list-questions]

using shortcode in php code:

<?php echo do_shortcode(‘[dwqa-popular-questions number=”3″ title=””]’); ?>

 

Code:

List of categories

<ul><?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&show_count=1&orderby=id&title_li=');?></ul>

 

Custome Post type is

dwqa-question

dwqa-answer