avatar

Niki Tech

受託開発のほかにも、独自に開発しているアプリもあります🚀

🛠Display custom webview in mobile app without header, footer and unneccessary part with Wordpress, theme Blogus and Flutter apps

2024-06-05

Mobile app will load the website with some custom for good display

  1. Remove Header
  2. Remove Footer
  3. Remove Ads
  4. Remove Sidebar
  5. Remove Related posts

Environment

  1. Wordpress with AWS
  2. Blogus theme
  3. App by Flutter

Step

Custom webview load in mobile app side in Flutter

class _WebViewComponentState extends State<WebViewComponent> with VMState<WebViewViewModel, WebViewComponent> { void onVMReady(WebViewViewModel viewModel, BuildContext context) { webViewController ..setUserAgent('custom_app_id') ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setNavigationDelegate( NavigationDelegate( onPageStarted: (_) { viewModel.setIsWebLoading(isLoading: true, shouldNotify: true); }, onPageFinished: (_) { viewModel.setIsWebLoading(isLoading: false, shouldNotify: true); }, onNavigationRequest: (NavigationRequest request){ final url = request.url.toString(); if (url.contains('tag') ||url.contains('category') || !url.contains('custom_domain_url')) { return NavigationDecision.prevent; } else if (url == 'custom_domain_url_top_page'){ viewModel.navigationService.pop(); } return NavigationDecision.navigate; }, onWebResourceError: (_) => _handleWebError(), ), ) ..loadRequest(Uri.parse(widget.url)); }

Add function is_mobile_device_with_user_agent() in functions.php

function is_mobile_device_with_user_agent() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $specific_app_id = 'custom_app_id'; if (stripos($user_agent, $specific_app_id) !== false) { return true; } return false; }

Hide header and ads by check is_mobile_device_with_user_agent() in header.php

<?php wp_head(); //Line 16 if(!is_mobile_device_with_user_agent()){ ?> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxx" crossorigin="anonymous"></script> <?php } ?>
<?php if(!is_mobile_device_with_user_agent())do_action('blogus_action_blogus_header_type_section'); ?> //Line 49

Hide footer, sidebar and related_post by check is_mobile_device_with_user_agent() in single.php

<?php if(!is_mobile_device_with_user_agent()){?> <!-- featured_post -->//Line 132 //Content here <?php }?> <!--End bs-realated-slider -->//Line 184
<?php if(!is_mobile_device_with_user_agent()) get_sidebar();?>//Line 195
<?php if(!is_mobile_device_with_user_agent()) get_footer(); //Line 205