htaccess server – Functions.php / Enque / Header / Footer

<?php

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}



76. Deactivate all plugins when not able to access the dashboard
Often, you may need to deactivate all plugins for troubleshooting. However, if for some unfortunate reason you are not able to log in to your dashboard, you can deactivate all plugins from FTP.


Go to wp-content/ directory and change the folder name from plugins to something else, such as wp-content-backup.

*/


   /*------------------------------------*
   SHOW ALL ACTIVE PLUGSIN
   *------------------------------------*/

/*


For maintenance purposes, you might want to get the list of active plugins on a specific WordPress install. Paste the following code to functions.php file, and you'll start seeing active plugins list on your dashboard.

*/

add_action('wp_dashboard_setup','wpse_54742_wp_dashboard_setup');

function wpse_54742_wp_dashboard_setup(){

wp_add_dashboard_widget('wpse_54742_active_site_plugins', __('Active Plugins'),'wpse_54742_active_site_plugins');} function wpse_54742_active_site_plugins(){

$the_plugs = get_option('active_plugins');

echo '<ul>';

foreach($the_plugs as $key => $value){

$string = explode('/',$value);// Folder name will be displayed

echo '<li>'.$string[0].'</li>'; }

echo '</ul>';}




   /*------------------------------------*
  INCREAE PHP MEMEORY
   *------------------------------------*/
/*

If you were activating a huge plugin and found an error that says memory exhausted just add the following line of code to your wp-config.php file.

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

The above code will increase the memory limit to 64M, but you can change the value to whatever your hosting server is able to support.
*/


   /*------------------------------------*
  CONTROL UPLOAD LIMITS
   *------------------------------------*/
/*


Increase/decrease maximum upload size through media uploader
Depending on the host, you’ll see a limit for the file size that you can upload through your Media uploader page in WP.

Add the below code to your .htaccess file to increase the upload limit to 64MB

php_value upload_max_filesize 64M

php_value post_max_size 64M

php_value max_execution_time 300

php_value max_input_time 300

*/

   /*------------------------------------*
   REDIRECT FOR MAINTANACE
   *------------------------------------*/

/*
Sometimes you may need to redirect the site to a maintenance page. Create a maintenance page and name it as maintenance.html. Upload it to the root directory. Add the below code to .htacess and redirect all traffic to maintenance.html

# Redirect all traffic to maintenance.html file

RewriteEngine on

RewriteCond %{REQUEST_URI} !/maintenance.html$

RewriteCond %{REMOTE_ADDR} !^123.123.123.123

RewriteRule $ /maintenance.html [R=302,L]

*/
   /*------------------------------------*
   CUSTOM ERROR PAGES
   *------------------------------------*/

/*

Create error pages for 403, 404 and 500 errors and upload it to your base WordPress installation. Then, add the following code snippet to your .htaccess file to enable the custom error pages.

# Custom error page for error 403, 404 and 500
ErrorDocument 404 /404-error.html
ErrorDocument 403 / 403-error.html
ErrorDocument 500 / 500-error.html

*/

   /*------------------------------------*
   CHANGE HOW LONG LOGIN
   *------------------------------------*/

/*



Of course, with this WordPress tip, you can choose whatever value you want, just find the number of seconds add replace the value.
*/

add_filter( 'auth_cookie_expiration', 'stay_logged_in_for_1_year' );
function stay_logged_in_for_1_year( $expire ) {
return 31556926; // 1 year in seconds
}


   /*------------------------------------*
   REDIRECT MOBILE USERS TO MOBILE SITE
   *------------------------------------*/

/*


This trick comes in handy if you like to keep a mobile version of your site to the responsive version. Add the following commands to the .htaccess file to redirect the mobile users to a mobile version of the site.

RewriteEngine On

# Check for mime types commonly accepted by mobile devices

RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC]

RewriteCond %{REQUEST_URI} ^/$

RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]

*/



   /*------------------------------------*
  DELETE ALL PINGBACKS
   *------------------------------------*/

/* 

Execute the below SQL query against your database to delete all the pingbacks you received. As usual, these WordPress tips where we delete stuff from the database can cause irreperable harm to your website. Take backups!


DELETE FROM wp_comments WHERE comment_type = 'pingback';

*/

 




   /*------------------------------------*
   TRACK FILE DOWNLOADS
   *------------------------------------*/

/* 

/*
Using Google Analytics’ event tracking functionality, you can track file downloads without a plugin. See the below example code for event tracking.
<a onclick="_gaq.push(['_trackEvent','Download','PDF',this.href]);" href="https://cdn.collectiveray.com/ebook.pdf" target="_blank" rel="noopener noreferrer">Download ebooks</a>

We have a full post on how to do this, either using Analytics or other plugins here.

Monetization Tricks
monetize wordpress tricks
WordPress tips tricks and tweaks for monetization of your website
Monetization is very important for most blogs, so why don't you follow these recommendations for making some extra cash off your WP site.
*/


   /*------------------------------------*
   AD SENSE ADS
   *------------------------------------*/

/* 

54. Display AdSense ads only to search engines
If you look at the total ad clicks you receive on your website, you'll find that search engines visitors are more likely to click the contextual ads like AdSense than any other visitor. So restricting your ad to display only to the visitors that come from search engine can help you increase the CTR of your ad.
To display ads only to search engine visitors, add the following code to your functions.php file.

$ref = $_SERVER['HTTP_REFERER'];

$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');

foreach ($SE as $source) {

  if (strpos($ref,$source)!==false) {

    setcookie("sevisitor", 1, time()+3600, "/", ".yourdomain.com"); 

    $sevisitor=true;

  }

}

function visits_from_searchengine(){

  global $sevisitor;

  if ($sevisitor==true || $_COOKIE["sevisitor"]==1) {

    return true;

  }

  return false;

}

Make sure you change yourdomain.com to your domain. Then, add the following code snippet to single.php file.

<?php if (function_exists('visits_from_searchengine')) {

  if (visits_from_searchengine()) { ?>

YOUR CODE HERE

<?php } } ?>

Don’t forget to add your AdSense code by replacing YOUR CODE HERE.

*/


SEO



   /*------------------------------------*
   REMOVE POST DATE STAMP FORM SERP
   *------------------------------------*/

/* 

If your content is not time sensitive, make sure you avoid displaying WordPress post date stamp on SERP, which will increase the CTR of your posts that displays on SERP.
Go to single.php file and find something like this

<?php the_time('F jS, Y') ?>

Replace it with the following code

<script language="javascript" type="text/javascript">document.write("<?php the_time('F jS, Y') ?>");</script>

*/

   /*------------------------------------*
   PREVENT IMAGE HOTLINKING
   *------------------------------------*/

/* 


Protecting your images from hotlinking will help you save lots of bandwidth by preventing other sites from displaying your images.
Simply go to .htaccess file and add the below code. Make sure you replace the ‘your-domain-name’ part with your actual domain name.

*/

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?your-domain-name [NC]

RewriteRule .(jpg|jpeg|png|gif)$ - [NC,F,L]


   /*------------------------------------*
  RESTRICT BOTS ACCESS
   *------------------------------------*/

/* 



By preventing vulnerable bots from accessing your website, you can tighten the security of your WordPress blog. Copy the following code to .htaccess file.

SetEnvIfNoCase User-Agent ^$ keep_out

SetEnvIfNoCase User-Agent (pycurl|casper|cmsworldmap|diavol|dotbot) keep_out

SetEnvIfNoCase User-Agent (flicky|ia_archiver|jakarta|kmccrew) keep_out

SetEnvIfNoCase User-Agent (purebot|comodo|feedfinder|planetwork) keep_out

Order Allow,Deny

Allow from all

Deny from env=keep_out
*/

   /*------------------------------------*
  DISABLE SEFL PINGBACKS
   *------------------------------------*/

/* 

Add the following code to your functions.php file to disable the self pingbacks.
*/

// Disable self pingbacks in WordPress

function disable_self_trackback( &$links ) {

  foreach ( $links as $l => $link )

        if ( 0 === strpos( $link, get_option( 'home' ) ) )

            unset($links[$l]);

}

add_action( 'pre_ping', 'disable_self_trackback' );

   /*------------------------------------*
   BLOCK DIRECTORIES USING ROBOTS.TXT
   *------------------------------------*/

/* 



Restrict search engines from crawling to the root directory by adding the code snippet below to robots.txt file.

User-agent: *

Disallow: /cgi-bin/

Disallow: /wp-admin/

Disallow: /wp-includes/

Disallow: /xmlrpc.php

Disallow: /wp-content/plugins/

Disallow: /wp-content/cache/

Disallow: /wp-content/themes/

Disallow: /trackback/

Disallow: /feed/

Disallow: /comments/

Disallow: /category/

Disallow: /trackback/

Disallow: /feed/

Disallow: /comments/

Disallow: /*?

Allow: /wp-content/uploads/

*/

   /*------------------------------------*
   REDIRECT WWW TO NON-WWW
   *------------------------------------*/

/* 

84. Redirect traffic from www to non-www

301 redirect all the traffic from www to non-www version by adding the following code to the .htaccess file.

# Redirect Non-WWW to WWW

RewriteEngine on

RewriteCond %{HTTP_HOST} ^yourwebsite.com

RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [R=301,L]

# END Redirect Non-WWW to WWW

# BEGIN WordPress

RewriteEngine On

RewriteBase /

RewriteRule ^index.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

# END WordPress

Remember to replace yourwebsite with your website URL. Additionally, in the general settings option, you will need to add WWW to the [WordPress Address (URL)]and [Site Address (URL)]
*/




   /*------------------------------------*
  RECENTLY MODIFIED FILES
   *------------------------------------*/
/*
See recently modified files
If you have SSH access to your server, sign in and run the command below to see the recently modified files. This command comes in handy especially if you are suspecting vulnerable access to your server without your consent.

The below command will show changes made in last 2 days in the specified directory.

find /home/yourdirectory/yoursite/ -mtime -2 -ls

*/





 /*------------------------------------*
DISABLE PLUGIN UPDATES
 *------------------------------------*/

/*

 Disable checking for plugin updates
WordPress automatically checks if plugins updates are available. The below trick comes in handy, in some cases such as if you worry that updating plugins might break your site. Paste the following code to your functions file and disable checking for plugin updates.

WARNING: Disabling plugin updates could lead to your WordPress website being compromised


*/

remove_action( 'load-update-core.php', 'wp_update_plugins' );

add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );









 /*------------------------------------*
IDENTIFY UNUSED TAGS
 *------------------------------------*/

/*

Identify unused tags

If you delete old posts manually from MySQL, the tags you used on the posts will remain unused. Run the following SQL query to identify such unused tags. This is one of those WordPress tips which needs a bit of attention, because deleting stuff which is necessary can break your site.

SELECT * From wp_terms wt

INNER JOIN wp_term_taxonomy wtt ON wt.term_id=wtt.term_id WHERE wtt.taxonomy='post_tag' AND wtt.count=0;

*/



/*///////////////////////////*/
/*//// SECURIITY ////////////*/
/*////////////////////////////*/

/*


 /*------------------------------------*
LIMIT ACCESS TO SPECIFIC IP
 *------------------------------------*/

If your ip addresses don’t change often, you can limit accessing to WordPress login page to that specific IP address only.

See the following code, replace the 128.128.128.128 with your own ip address (include the  to escape the fullstop in the address) and add the code to .htaccess file.

As with such WordPress tips, please make sure you have backups, just in case you make a small mistake and need to revert to the previous version.

ErrorDocument 401 /path-to-your-site/index.php?error=404

ErrorDocument 403 /path-to-your-site/index.php?error=404

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} ^(.*)?wp-login.php(.*)$ [OR]

RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$

RewriteCond %{REMOTE_ADDR} !^128.128.128.128

RewriteRule ^(.*)$ - [R=403,L]

</IfModule>

 /*------------------------------------*
BLOCK IP ADDRESSES
 *------------------------------------*/
Add the following lines to the .htaccess file to deny certain ip addresses from accessing your site.

# allow all except those indicated here

<Files *>

order allow,deny

allow from all

deny from xxx.xxx.xxx.xxx

</Files>

 /*------------------------------------*
CREATE MANAUAL DATABASE BACKUP
 *------------------------------------*/
Log in to phpMyAdmin, select the WP database you want to backup. Click export, choose a compression method and click execute. When your browser prompts you to download the backup, click yes.

 /*------------------------------------*
FORCE SSL TO LOGIN
 *------------------------------------*/
If you have an SSL certificate installed on your server, you can force your WP installation to use SSL mode for secure user log in.
For that, add the below code to wp-config.php file.
define('FORCE_SSL_LOGIN', true);

Incidentally, if you want to avoid all of the hassle associated with getting an SSL yourself, our host, InMotion, actually supports SSL hosting, via their InMotion shared accounts. Read our full review here: https://www.collectiveray.com/inmotion-hosting-review

66. Disable/Change log error message
Tweak your functions.php by adding the following snippet to disable WordPress log in error message.

function no_wordpress_errors(){

  return 'GET OFF MY LAWN !! RIGHT NOW !!';

}

add_filter( 'login_errors', 'no_wordpress_errors' );

 /*------------------------------------*
RESTRICT ACCESS TO WP-INCLUCES
 *------------------------------------*/
Again, add the code to .htaccess file to disable the access to wp-includes.php

# Block wp-includes folder and files

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^wp-admin/includes/ - [F,L]

RewriteRule !^wp-includes/ - [S=3]

RewriteRule ^wp-includes/[^/]+.php$ - [F,L]

RewriteRule ^wp-includes/js/tinymce/langs/.+.php - [F,L]

RewriteRule ^wp-includes/theme-compat/ - [F,L]

</IfModule>

 /*------------------------------------*
DISABLE DIRECTORY BROWSING
 *------------------------------------*/
If you want to disable external access to the root directory, add the following to .htaccess file

# Disable directory browsing
Options All -Indexes

 /*------------------------------------*
DISABLE ACCESS TO CERTAIN FILE TYPES
 *------------------------------------*/
Create a new .htaccess file, add the following code and upload the file inside wp-content folder.

# Disable access to all file types except the following

Order deny,allow

Deny from all

<Files ~ ".(xml|css|js|jpe?g|png|gif|pdf|docx|rtf|odf|zip|rar)$">

Allow from all

</Files>

This will disable accessing all the file types except the mentioned files in the code.

 /*------------------------------------*
DENY ACCESS TO .HTACCESS FILES
 *------------------------------------*/
Add the code to .htaccess to deny access to all of your .htaccess files

# Deny access to all .htaccess files

<files ~ "^.*.([Hh][Tt][Aa])">

order allow,deny

deny from all

satisfy all

</files>


*/



 /*------------------------------------*
DISABLE TEMPLATE FILE EDITING
 *------------------------------------*/

/*

Template file editing
template editing
Any WordPress user with administrator access can edit templates by navigating to Appearance > Editor.
You can disable template file editing by adding the following line of code to wp-config.php


define( ‘DISALLOW_FILE_EDIT’, true );

*/





 /*------------------------------------*

 *------------------------------------*/

/*



*/










Scroll to Top