I know it's easy stuff, but I'm new to this and I can't understand which is the best approach.
Background
The template I'm working on is a dual theme Desktop/Mobile, UA Sniffing based. Now I just added responsiveness through enquire.js and ajax and everithing changed: I'm struggling in getting things to work properly, is the first time I'm dealing with ajax.
Scenario
My template is dynamically loaded through ajax in fact if you try to resize the width of the window below 1080px, the mobile template will show up. (it will show up on every mobile device too but this is not important for us atm)
So, responsiveness has been implemented with the help of enquire.js and ajax calls (see code below).
Originally, the template was static so at the moment, the whole section is still conditionally loaded through if statements in functions.php. (e.g. the video script should just load on certain pages of the desktop version)
Issues
- The mobile template -which has been styled with the mobile.css stylesheet- doesn't seem to have any effect. Should I change the way stylesheets and scripts are loaded due to the new ajax/enquire thing?
- the content i.e. the_content(), doesn't show up. Why? And how to load it in my scenario? found! -- document.write(data) was owerwriting everything! I added $("body").append(data); instead
Follows the code
functions.php
//Load Stylesheet
function add_desktop_styles() {
wp_register_style('reset', get_template_directory_uri().'/reset.css');
wp_register_style('style', get_template_directory_uri().'/style.css', array('reset') );
wp_enqueue_style('style');
//$mobile = mobile_device_detect();
//if ($mobile==true) {
if (wp_is_mobile()) {
wp_register_style('mobile', get_template_directory_uri().'/mobile.css', array('reset') );
wp_enqueue_style('mobile');
}
}
add_action('wp_head', 'add_desktop_styles', '1');
//UA Sniffing
function devicecontrol() {
require_once('_/inc/mobile_device_detect.php');
}
add_action('wp_loaded', 'devicecontrol', '2');
//AJAX
function your_function_name() {
wp_enqueue_script( 'function', get_template_directory_uri().'/_/js/my_js_stuff.js', array('jquery','enquire'), true);
wp_localize_script( 'function', 'my_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('template_redirect', 'your_function_name');
function get_mobile_template()
{
include('templates/pages/homepage-phone.php');
die();
}
add_action("wp_ajax_nopriv_get_mobile_template", "get_mobile_template");
add_action("wp_ajax_get_mobile_template", "get_mobile_template");
function get_desktop_template()
{
if (!wp_is_mobile()) {
include('templates/pages/homepage-computer.php');
} else {
include('templates/pages/homepage-phone.php');
}
die();
}
add_action("wp_ajax_nopriv_get_desktop_template", "get_desktop_template");
add_action("wp_ajax_get_desktop_template", "get_desktop_template");
//jQuery
if ( !function_exists( 'core_mods' ) ) {
function core_mods() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ), false);
wp_enqueue_script( 'jquery' );
}
}
add_action( 'wp_enqueue_scripts', 'core_mods','2' );
}
//Scripts Mobile
function add_mobile_scripts() {
wp_register_style('video-css', get_template_directory_uri().'/_/js/videojs/video-js.css');
wp_enqueue_style('video-css');
wp_register_script( 'video-js', get_template_directory_uri().'/_/js/videojs/video.js', null, false );
wp_enqueue_script( 'video-js' );
}
function isMobile() {
//$mobile = mobile_device_detect();
///if ($mobile==true)
if (wp_is_mobile()) {
add_mobile_scripts();
}
}
add_action( 'wp_enqueue_scripts', 'isMobile', '1' );
//Scripts Desktop
function addSlide() {
/*wp_register_script( 'modernizr', get_template_directory_uri().'/_/js/modernizr.js', null, false );
wp_enqueue_script( 'modernizr' );*/
wp_register_script( 'enquire', get_template_directory_uri().'/_/js/enquire.min.js', null, false );
wp_enqueue_script( 'enquire' );
wp_register_script( 'jwplayer', get_template_directory_uri().'/_/js/jwplayer.js', null, false );
wp_enqueue_script( 'jwplayer' );
wp_register_script( 'bootstrap', get_template_directory_uri().'/_/js/bootstrap.js', array('jquery'), false );
wp_enqueue_script( 'bootstrap' );
wp_register_script( 'spk_slide', get_template_directory_uri().'/_/js/slides.js', array('jquery'), false );
wp_enqueue_script( 'spk_slide' );
}
// Slider just on front page
function isSlideshowPage() {
if ( is_front_page()
|| is_page('Bankkaufmann')
|| is_page('Hochschulabsolvent')
|| is_page('Professional')
|| is_page('Die Prüfungsstellen')
|| is_page('Von Beruf Verbandsprüfer')) {
addSlide();
}
}
add_action( 'wp_enqueue_scripts', 'isSlideshowPage' );
Js script
this script at the moment loads on all pages, I will wrap it and call it from the page-template later
enquire.register("screen and (max-width:1080px)", {
// OPTIONAL
// If supplied, triggered when a media query matches.
match : function() {
jQuery.ajax({
url: my_ajax_script.ajaxurl,
data: ({action : 'get_mobile_template'}),
success: function(data) {
document.write(data);
}
});
},
unmatch : function() {$("body").empty();},
// OPTIONAL
// If supplied, triggered once, when the handler is registered.
setup : function() {},
// OPTIONAL, defaults to false
// If set to true, defers execution of the setup function
// until the first time the media query is matched
deferSetup : true,
// OPTIONAL
// If supplied, triggered when handler is unregistered.
// Place cleanup code here
destroy : function() {}
});
enquire.register("screen and (min-width:1081px)", {
// OPTIONAL
// If supplied, triggered when a media query matches.
match : function() {
jQuery.ajax({
url: my_ajax_script.ajaxurl,
data: ({action : 'get_desktop_template'}),
success: function(data) {
document.write(data);
}
});
},
unmatch : function() {$("body").empty();},
// OPTIONAL
// If supplied, triggered once, when the handler is registered.
setup : function() {},
// OPTIONAL, defaults to false
// If set to true, defers execution of the setup function
// until the first time the media query is matched
deferSetup : true,
// OPTIONAL
// If supplied, triggered when handler is unregistered.
// Place cleanup code here
destroy : function() {}
});
window.location.href
) but including an endpoind that can be 'mobile' or 'desktop'. An hook intotemplate_include
filter will do the rest. Hope it's clear.<head>
is outputted on first request and so SEO stuff (title, description) and all the plugings that alter head need a query is runned to work properly. So I thinked that to stop main query is not useful.. So, at least employ some caching, as you said, I think can be very useful. Any idea on how implement?