• Resolved wintery01

    (@wintery01)


    Hello, I’m fairly new to wordpress (less than a week experience) and I’m trying to get a jvectormap to show up in my customizr child theme using a custom template.

    I am trying to make the vector map to load specific page’s content when a region is clicked. just like this test page
    and more info about it in
    https://github.com/bjornd/jvectormap/issues/59

    However, I can’t even get my map to show up in the page itself.

    Here is my code for the template that will have the map

    <?php
    /*
    Template Name: jvectormap
    */
    wp_deregister_script( 'jquery' );
    wp_enqueue_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), null, false ); 
    
    wp_enqueue_style( 'jvectormap-style', get_stylesheet_directory_uri() . '/js/jquery-jvectormap.css', array(), null, 'all' );
    wp_enqueue_script( 'jvectormap-script', get_stylesheet_directory_uri() . '/js/jquery-jvectormap.min.js', array( 'jquery' ),null , true );
    wp_enqueue_script( 'jvectormap-map', get_stylesheet_directory_uri() . '/js/jquery-jvectormap-world-mill-en.js', array( 'jquery' ), null, true );
    wp_enqueue_script( 'jvectormap-map_code', get_stylesheet_directory_uri() . '/js/map_config.js', array( 'jquery' ), null, true );
    
    ?>
    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>
    
    <div id="main-wrapper" class="<?php echo tc__f( 'tc_main_wrapper_classes' , 'container' ) ?>">
    
        <?php do_action( '__before_main_container' ); ##hook of the featured page (priority 10) and breadcrumb (priority 20)...and whatever you need! ?>
    
        <div class="container" role="main">
            <div class="row">
    
                <?php do_action( '__before_article_container'); ##hook of left sidebar?>
    
                    <div id="content" class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
    
                        <?php do_action ('__before_loop');##hooks the header of the list of post : archive, search... ?>
    
                            <?php if ( have_posts() ) : ?>
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
    
                                   <?php the_post(); ?>
    
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_article') ?>
    
                                <?php endwhile; ?>
    
                            <?php endif; ##end if have posts ?>
    
                        <?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
    
                    </div><!--.article-container -->
    
               <?php do_action( '__after_article_container'); ##hook of left sidebar ?>
    
            </div><!--.row -->
        </div><!-- .container role: main -->
    
        <?php do_action( '__after_main_container' ); ?>
    
    </div><!--#main-wrapper"-->
    
    <?php do_action( '__after_main_wrapper' );##hook of the footer with get_get_footer ?>

    Did I do anything wrong? Any help is appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • First question: why are you deregistering WP’s local jquery and loading an external one?
    Second, why aren’t you registering your scripts during wp_enqueue_scripts with an action hook placed in your functions.php?
    And lastly: didn’t you forget to add the function that outputs the map in your template?

    Thread Starter wintery01

    (@wintery01)

    Hey there, thanks a lot for your reply!
    As i have told you I am really new to all these web designing work, and am just trying my best to get things inside my blog in the way that it may look nice. Please do bear with me if I seem to make blatantly obvious and stupid mistakes.

    I must admit I just copied the codes given in the website example i included above. With a few tweaks here and there which seem logical for me.

    To answer your first question, since the code given by the example above, I thought that there may be problems with older versions of jquery, and hence proceeded to just leave the code be as it is.

    Second question, I don’t really understand what you mean, pardon me. Are you trying to say that the scripts should be registered inside the functions.php of my child theme instead? Because I’d just want this script to work for this specific page that I’m using this template on.

    Third question, could you guide me through on how to put that in? I am totally clueless.

    Thank you very much!

    Thread Starter wintery01

    (@wintery01)

    In addition to that, I thought that this script, “map_config.js” was supposed to be the script that adds the map to the template? or am i wrong?

    If i am wrong, can you show me which fields i should change? Thanks!

    (function($) {
    
    	$(document).ready(function(){
    
    		$.ajaxSetup({cache:false});
    
    		var pageLoaderID = 42;
    
    		$('#map').vectorMap({
    
    			map: 'world_en',
    
    			onRegionClick: function(event, code) {
    
    				var post_id;
    
    				switch(code) {
    
    				case "DE":
    
    					post_id = 30;
    
    					break;
    
    				case "US":
    
    					post_id = 54;
    
    					break;
    
    				case "FR":
    
    					post_id = 36;
    
    					break;
    
    				case "ES":
    
    					post_id = 56;
    
    					break;
    
    				default:
    
    					post_id = false;
    
    					break;
    
    				}
    
    				console.log('Country code: ' + code + '   Post ID: ' + post_id);
    
    				if(post_id !== false) {
    
    					$("#mapData").html("Loading data...");
    
    					$("#mapData").load("http://tests.andremeyering.de/jvectormap/wp/?page_id=" + pageLoaderID, { id: post_id });
    
    				} else {
    
    					$("#mapData").html("<p><strong>No Country data avaiable!<br></strong>Avaiable countries: Germany, USA, France and Spain.</p>");
    
    				}
    
    				return false;
    
    			}
    
    		});
    
    	});
    
    })(jQuery);

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    thought that there may be problems with older versions of jquery, and hence proceeded to just leave the code be as it is.

    Are you sure those problems are to do with you just not using noconflict wrappers?
    http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

    Thread Starter wintery01

    (@wintery01)

    I created two WordPress page templates in the main theme directory. The first template is used for the page that is containing the map. It loads the javascript/css files that are required for jVectorMap (please add them into the js folder) and also reloads jQuery, so it’s NOT loaded with noConflict mode because otherwise the downloaded map data (in this example world_en) isn’t loaded correctly.

    apparently the author of the test page said something about the noconflict mode, is this what you mean? If not, please elaborate, as I’m still learning slowly, thanks!!

    UPDATE : I read up on your noconflict wrappers, and it seems that the author did use one of the noconflict wrapper examples as seen in the map_config.js i posted above.

    Thread Starter wintery01

    (@wintery01)

    Thanks for all the effort to help guys, but i have succeeded in projecting the map out on my site.

    I simply forgot to add the div container for the map. rookie mistake it seems.

    Thanks for the help!

    That’s what I meant when I said “the function that adds the map” (i was supposing you’re getting it through php. I should have said “the placeholder for your map, be it a div with a certain id modifiable via jQuery or by a php function.

    Good to know you’re on track.

    As to your previous question, in order to allow so many different pieces of code to work together, most of them being developed by different people, WP has a logic and a flow they all respect. From the user request to the rendered page. Frankly I’m surprised to see your page works, because wp_enqueue_scripts is run during the output of the head section of the page. In our case it’s on this line:

    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>

    So you’re deregistering jquery before it has been registered, and my guess is your map runs on WP’s jQuery anyway (which is always up to date, btw). Or maybe WP checks to see if jquery is already registered and doesn’t duplicate it, i don’t know. Or maybe I’m wrong and the registering is done before any output and only the enqueuing is done in <head>. Never needed to take a closer look at this.

    Your method is prone to error. Hook your scripts on the proper hook as it’s certain they’ll still work in WP 24.5.2, too.

    Thread Starter wintery01

    (@wintery01)

    Hi acub thanks for the long explanation!

    I know this might be sidetracked, but could you perhaps give me an example on how to insert a placeholder for my map with jquery or php funtion? I would really appreciate that. My map is visible now, tho I have problems finding out how to make it automatically readjust its size for smaller screens.
    I’m always wondering if there is a more correct way of putting things in, because I’m more familliar to the html way of doing things.

    Any referrences to tutorials would be appreciated too! I’m excited to learn more.

    I’ll be sure to look into the jquery line, or maybe i should just take that line out? I’m learning about hooks to at the moment, and kind of confused which hooks do what in the template.

    Thanks for your help! 🙂

    Here’s how your scripts should be enqueued (this code goes in functions.php of your child theme):

    add_action( 'wp_enqueue_scripts', 'jvectormap_enqueue_scripts' );
    function jvectormap_enqueue_scripts() {
    	if (is_page_template('jvectormap.php')) {
    		wp_enqueue_style( 'jvectormap-style', get_stylesheet_directory_uri() . '/js/jquery-jvectormap.css', array(), null, 'all' );
    		wp_enqueue_script( 'jvectormap-script', get_stylesheet_directory_uri() . '/js/jquery-jvectormap.min.js', array( 'jquery' ),null , true );
    		wp_enqueue_script( 'jvectormap-map', get_stylesheet_directory_uri() . '/js/jquery-jvectormap-world-mill-en.js', array( 'jquery' ), null, true );
    		wp_enqueue_script( 'jvectormap-map_code', get_stylesheet_directory_uri() . '/js/map_config.js', array( 'jquery' ), null, true );
    	}
    }

    If it’s not obvious, you should remove all lines that enqueue scripts from your template.

    As for jvectormap’s responsiveness, you should try and get help from the author of the plugin/library.

    However, I do have one question. What does all of this have to do with Customizr? You’re using twentyeleven.

    Thread Starter wintery01

    (@wintery01)

    Hey acub, thanks a lot for the reply!

    I see, so i could just include these functions in my functions.php instead.

    I have actually succeeded in doing so through trial and error for the responsiveness part, thanks for the suggestion tho!

    I am actually using customizr theme, the website shown is actually not my website, its just an example page. My site still needs a lot of work, and its my first one ever!

    Thanks again for your help, sorry for bombarding you with my questions!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Inserting jvectormap through custom template’ is closed to new replies.