How to increase wordpress memory limit

When some plugin or php code needs more memory than allocated in apache server you get a error like

  Fatal error: Allowed memory size of xxxxxx bytes exhausted (tried to allocate xxxxxx bytes) in /home/youruser/public_html/wp-includes/plugin.php on line xxx

To fix this you need increase wordpress memory usage by adding the following line in wp-config.php. This is located in the root of wordpress installation. And you can use FTP client or you can browse the cpanel file manager to edit wp-config and add the code.

  define( 'WP_MEMORY_LIMIT', '256M' );

You can increase it to 64M at first and if you still see the error you can increase it to any number it requires but your server has a memory limit available.

How to create a wordpress plugin, Google Analytics Plugin for Example

To create a wordpress plugin navigate to the installation folder of the wordpress, then wp-content -> plugins folder create a folder with your desired name of the plugin “your-plugin”, then create a php file with same name “your-plugin.php” and add the following code in it.

<?php
/**
 * Plugin Name: Your Plugin Name
 * Plugin URI: https://www.yourwebsite.com/
 * Description: Your Plugin Description
 * Version: 1.0
 * Author: Your Author Name
 * Author URI: https://www.yourwebsite.com
 */

This is the basic code for a wordpress plugin. After that you can add your plugins php functionality like this. I am creating a legacy analytics plugin for the example

<?php
/**
 * Plugin Name: Your Plugin Name
 * Plugin URI: https://www.yourwebsite.com/
 * Description: Your Plugin Description
 * Version: 1.0
 * Author: Your Author Name
 * Author URI: https://www.yourwebsite.com
 */

add_action( 'wp_head', 'add_to_head' );
function add_to_head() {
  echo '';
}

The above code should add the analytics.js to the wordpress head. Then you can add the Analytics Legacy code to the footer like this

<?php
/**
 * Plugin Name: Your Plugin Name
 * Plugin URI: https://www.yourwebsite.com/
 * Description: Your Plugin Description
 * Version: 1.0
 * Author: Your Author Name
 * Author URI: https://www.yourwebsite.com
 */

add_action( 'wp_head', 'add_to_head' );
function add_to_head() {
  echo '';
}
add_action('wp_footer', 'add_to_footer');
function add_to_footer(){
  echo "
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-Your Analytics Number', 'auto');
    ga('send', 'pageview');
  </script>
  ";
}

How to add html code to wordpress Header and Footer

To add html code to the header, use the following code snippet

add_action('wp_head', 'add_html_to head');
function add_html_to head(){
  echo "HTML code goes here";
}

To add html code to the footer, use the following code snippet

add_action('wp_footer', 'add_html_to_footer');
function add_html_to_footer(){
  echo "HTML code goes here";
}

These functions can be called from funstions.php in the theme directory or from a plugin.

WordPress – How to load a plugin(LiveChat) after the theme

Load LiveChat on a specific or single site on multisite wordpress or any plugin you want

The following tip is a custom solution for wordpress LiveChat. By default live chat loads on every page and every website on a multisite wordpress installation. After wrapping the language check the in the LiveChat initiator file I found that the value was not available to the plugins php file. So I moved the LiveChat initiator code into a function and added the action
add_action( 'after_setup_theme', 'function_name' );

After adding this action I got the desired value for the $_GET[‘lang’] language check.
Following code may help someone that I used on LiveChat Plugin -> livechat.php

add_action( 'after_setup_theme', 'load_live_chat_last' );
function load_live_chat_last (){
 if ($_GET['lang'] == 'en-us') {
  if (is_admin()) {
   require_once(dirname(__FILE__).'/plugin_files/LiveChatAdmin.class.php');
   LiveChatAdmin::get_instance();
  }
  else {
   require_once(dirname(__FILE__).'/plugin_files/LiveChat.class.php');
   LiveChat::get_instance();
  }
 }
}

You can also load LiveChat plugin on a specific or single page by adding url path check as below,

function load_live_chat_last (){
 if ($_GET['lang'] == 'en-us') {
  if($_SERVER['REQUEST_URI'] == "/support/") {
   if (is_admin()) {
    require_once(dirname(__FILE__).'/plugin_files/LiveChatAdmin.class.php');
     LiveChatAdmin::get_instance();
   }
   else {
    require_once(dirname(__FILE__).'/plugin_files/LiveChat.class.php');
    LiveChat::get_instance();
   }
  }
 }
}