Category Archives: WordPress

Hide WordPress plugin from the plugin list.

If you want to hide a plugin name from the WordPress backend for whatever reason, you can use this piece of code in the functions file on your active theme:

	function hide_plugin_reallusion() {
	  global $wp_list_table;
	  $hidearr = array('plugin-folder/plugin.php');
	  $myplugins = $wp_list_table->items;
	  foreach ($myplugins as $key => $val) {
		if (in_array($key,$hidearr)) {
		  unset($wp_list_table->items[$key]);
		}
	  }
	}
	 
	add_action('hide_plugin_backend', 'hide_plugin_reallusion');

Or make this into plugin? Why not – plugins for everyone, let’s get covered in plugins.

WPML Translate get_theme_mod

If you are using WPML and you have a theme which customize strings are not detected you can use the fallowing code, you might need to modify it for your use case:

if (function_exists ( 'icl_register_string' )) {
    echo icl_translate('Theme Mod', 'text_in_wpml', get_theme_mod( 'theme_mod_name' ));
}

This basically register the string as it echo it.

And yeah – this will require you to have the strings translation plugin.

Enable WP_DEBUG to file

In your wp-config.php replace:

define('WP_DEBUG', false);

with:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will output any errors to file called debug.log in your wp-content directory.
It is far better to save errors to a file, otherwise they will be shown in your site.

Apply coupon if user bought a specific product

Recently I had to do discount in WooCommerce if a user is bought a specific product – in this case “Membership” product.
The fallowing code will check if a user is bought a product with given id, and if so it will apply specified coupon code.

Just place the fallowing at the end of your functions.php in your theme:

// Apply coupon programmatically if specific product is bought

add_action( 'woocommerce_before_cart', 'auto_add_coupon' );

function auto_add_coupon() {
    global $woocommerce;

    $coupon_code = 'membership-cc';

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

    $target_product = array(5183);

		$current_user = wp_get_current_user();
		if (woocommerce_customer_bought_product($current_user->user_email, $current_user->ID, $target_product)){

        $woocommerce->cart->add_discount( $coupon_code );
        $woocommerce->show_messages();
    }
    }

}

WooCommerce can’t remove items from cart on Nginx

Just finished moving client site from Apache to Nginx, and upon testing I noticed when I try to remove some product from the cart, it wasn’t working . All is happening is to reload the page.

It turns out that the problem is in my Nginx config (as I suspected).

A really simple fix.

If you have section like this in your config file:

    location / {
                try_files $uri $uri/ /index.php;
        }

you will need to add ?$args after index.php, so it is going to look like this:

    location / {
                try_files $uri $uri/ /index.php?$args;
        }

WordPress subdomain rewrite rules for Zeus web server

If you have to deal with the Zeus web server, using WordPress subdomain, you most probably noticed that the “pretty” permalinks include “/index.php/” whatever you do.

You will need to create file named “rewrite.script” in the root directory where WordPress is installed – pretty much the equivalent to “.htaccess” in Apache.

Use this in your rewrite.sript and replace the domain URL with your own.



RULE_3_START:
insensitive match IN:Host into % with ^www.sub.domain.com
if  matched then goto RULE_2_END
RULE_3_END:

RULE_2_START:
insensitive  match IN:Host into % with ^(.*).sub.domain.com
if matched then match URL  into $ with ^/(.*)
if not matched then goto RULE_2_END
set URL =  /%1/$1
RULE_2_END:


RULE_0_START:
# get the document root
map path into SCRATCH:DOCROOT from /
# initialize our variables
set SCRATCH:ORIG_URL = %{URL}
set SCRATCH:REQUEST_URI = %{URL}

# see if theres any queries in our URL
match URL into $ with ^(.*)\?(.*)$
if matched then
set SCRATCH:REQUEST_URI = $1
set SCRATCH:QUERY_STRING = $2
endif
RULE_0_END:

RULE_1_START:
# prepare to search for file, rewrite if its not found
set SCRATCH:REQUEST_FILENAME = %{SCRATCH:DOCROOT}
set SCRATCH:REQUEST_FILENAME . %{SCRATCH:REQUEST_URI}

# check to see if the file requested is an actual file or
# a directory with possibly an index. don?EUR(TM)t rewrite if so
look for file at %{SCRATCH:REQUEST_FILENAME}
if not exists then
look for dir at %{SCRATCH:REQUEST_FILENAME}
if not exists then
set URL = /index.php?q=%{SCRATCH:REQUEST_URI}
goto QSA_RULE_START
endif
endif

# if we made it here then its a file or dir and no rewrite
goto END
RULE_1_END:

QSA_RULE_START:
# append the query string if there was one originally
# the same as [QSA,L] for apache
match SCRATCH:ORIG_URL into % with \?(.*)$
if matched then
set URL = %{URL}&%{SCRATCH:QUERY_STRING}
endif
goto END
QSA_RULE_END:

After that you will need to go to the WordPress back-end and change your permalinks settings.

It might work with the main domain too – but I haven’t tested this.

Taxonomy Breadcrumbs

Add this function to the functions.php file in your current theme:

/* Taxonomy Breadcrumb */
function id_taxonomy_breadcrumb() {
// Get the current term
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
// Create a list of all the term's parents
$parent = $term->parent;
while ($parent):
$parents[] = $parent;
$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$parent = $new_parent->parent;
endwhile;
if(!empty($parents)):
$parents = array_reverse($parents);
// For each parent, create a breadcrumb item
foreach ($parents as $parent):
$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$url = get_bloginfo('url').'/'.$item->taxonomy.'/'.$item->slug;
echo '<a href="'.$url.'">'.$item->name.'</a> / ';
endforeach;
endif;
// Display the current term in the breadcrumb
echo $term->name;
}

And the call it in the temlate:

<?php id_taxonomy_breadcrumb(); ?>

Set WordPress permissions recursively with CLI in Linux

To easily change the permissions, just navigate to the WordPress install folder, and execute this two commands:

#Set all directories recursively to 755:
find . -type d -exec chmod 0755 {} +

#Set all files recursively to 644:
find . -type f -exec chmod 0644 {} +

If you are not in the current directory you will need to add the desired one after find, something like:

find /top/path/here -type d -exec chown www-data:www-data {} \;