Tag Archives: functions.php

Did you know that you can use WordPress built-in “mobile detect” function wp_is_mobile to create a simple shortcode that can hide certain parts of your content from mobile visitors and-/or desktop visitors. The wp_is_mobile function returns true when your site is viewed from a mobile browser. Using this function you can create adaptive-/responsive WordPress themes based on visitor device.

Method 1 instructions
Simply use IF conditions in your templates.

<?php  if( wp_is_mobile()){  ?>
// mobile stuff goes here
<?php } else { ?>
// desktop stuff goes here
<?php  } ?>

Method 2 instructions
This snippet will allow you to use shotcodes the [desktoponly] desktop content [/desktoponly] or [mobileonly] mobile content [/mobileonly] to determine what content should be returned to the visitor.

Add this code to your functions.php

<?php 
// [desktoponly] shortcode
add_shortcode('desktoponly', 'wp_snippet_desktop_only_shortcode');
function wp_snippet_desktop_only_shortcode($atts, $content = null){ 
    if( !wp_is_mobile() ){ 
        return wpautop( do_shortcode( $content ) ); 
    } else {
        return null; 
    } 
}
// [mobileonly] shortcode
add_shortcode('mobileonly', 'wp_snippet_mobile_only_shortcode');
function wp_snippet_mobile_only_shortcode($atts, $content = null){ 
    if( wp_is_mobile() ){ 
        return  wpautop( do_shortcode( $content ) ); 
    } else {
        return null; 
    } 
}
?>

Related links:
https://codex.wordpress.org/Function_Reference/wp_is_mobile

You can easily force specific pages to be secure in WordPress using the following simple little snippet. Just add the following snippet to the functions.php of your WordPress theme and specify the post/page ID desired.

Instructions
Add this code to your functions.php file.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    }
    return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

This WordPress snippet is very handy if you wish to add the category ID to the body or post-class for ie. targeting CSS styles and such.

Instructions
Add this to your functions.php file.

function category_id_class($classes) {
     global $post;
          foreach((get_the_category($post->ID)) as $category)
               $classes [] = 'cat-' . $category->cat_ID . '-id';
               
     return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');

This snippet will add a shortcut link to your WordPress theme editor.

Instructions
Add this to your functions.php file.

function admin_bar_theme_editor_option() {
	global $wp_admin_bar;
		if ( !is_super_admin() || !is_admin_bar_showing() )
		return;
		$wp_admin_bar->add_menu(
			array( 'id' => 'edit-theme',
			'title' => __('Edit Theme'),
                        'href' => admin_url( 'theme-editor.php')
		)
		);
	}
add_action( 'admin_bar_menu', 'admin_bar_theme_editor_option', 100 );

This snippet will change the default style of WordPress admin area by overriding the old stylesheet (CSS).

Instructions
1) Create a new css file in your theme folder, and then include it in WP-Admin using the admin_head hook.
2) Add this to your functions.php file.

function customAdmin() {
    $url = get_settings('siteurl');
    $url = $url . '/wp-content/themes/my-theme/styles/wp-admin.css';
    echo '<!-- custom admin css -->
          <link rel="stylesheet" type="text/css" href="' . $url . '" />
          <!-- /end custom adming css -->';
}
add_action('admin_head', 'customAdmin');

* Change the paths according to where you put your new/extra wp-admin.css file.
* Add whatever styles you want to override the defaults with, or copy the old stylesheet and use as a template for your new one. Location: /wp-admin/css/colors-fresh.css.