// [desktoponly] shortcode add_shortcode('desktoponly', 'shailan_desktop_only_shortcode'); function shailan_desktop_only_shortcode($atts, $content = null){ if( !wp_is_mobile() ){ return wpautop( do_shortcode( $content ) ); } else { return null; } } // [mobileonly] shortcode add_shortcode('mobileonly', 'shailan_mobile_only_shortcode'); function shailan_mobile_only_shortcode($atts, $content = null){ if( wp_is_mobile() ){ return wpautop( do_shortcode( $content ) ); } else { return null; } }
но мне этот код не подошел, т.к. нужно было еще детектить и планшеты. в итоге применил стороннюю библиотеку Mobile Detect и код выглядит таким образом:
// [desktoponly] shortcode add_shortcode('desktoponly', 'shailan_desktop_only_shortcode'); function shailan_desktop_only_shortcode($atts, $content = null){ include_once ( get_template_directory() . '/mobile_detect.php'); $detect = new Mobile_Detect; if( !$detect->isMobile() || $detect->isTablet() ) { return do_shortcode( $content ); } else { return null; } } // [mobileonly] shortcode add_shortcode('mobileonly', 'shailan_mobile_only_shortcode'); function shailan_mobile_only_shortcode($atts, $content = null){ include_once ( get_template_directory() . '/mobile_detect.php'); $detect = new Mobile_Detect; if( $detect->isMobile() && !$detect->isTablet() ) { return do_shortcode( $content ); } else { return null; } }
сам файлик mobile_detect.php копируем в папку с шаблоном