Forum Replies Created

Viewing 15 replies - 16 through 30 (of 33 total)
  • Thread Starter Nokaa

    (@nokaa)

    Thank you for thoses clean explanations, I better understand how the enqueue function is working now.

    However, I didn’t found yet how to resolve the main problem…
    In this example : https://codex.wordpress.org/Function_Reference/wp_localize_script#example all the functions are called in the same file. So it’s an easy way to present the issue. But here members let me know that it’s better to enqueue or register our script in the functions.php file.

    I tried to enqueue my script in the functions.php (footer false), and use the localize function in a custom-post-type file where I take my array of data from a wp_query. But it doesn’t work, and I presume that’s because the script is not enqueued in the same file where I use the wp_localize_script function…

    If I register my scripts in the same file like the example, it works, but apparently I am not supposed to proceed so… There’s where I’m really stuck.

    Thread Starter Nokaa

    (@nokaa)

    I tried some tricks by myself but still can’t get the logic about what you’re calling “callbacks” within wp_enqueue_script.

    To tell you the truth, I have never been comfortable with the theory… If you don’t mind, can you give me a quick example ? :/

    Thread Starter Nokaa

    (@nokaa)

    The localize and enqueue functions are both called from the same hook, so whether they are used in different callbacks (or even files) or the same doesn’t matter much, except the localize script must be called after the script tag has been established by registering. It’s best done in the same callback function so you can be sure of the correct order.

    So for example, if I’d well understood, I can process like that :

    In functions.php :

    [...]
    function metierville_enqueue_scripts(){
    	wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyAhE_CVUWwIOzXs-zD-vFdtgWBFWRBhQVY', array('jquery'), '1.0.0', true);
    	wp_register_script( 'map_metier_ville_script', get_stylesheet_directory_uri().'/assets/js/google_map_metier_ville.js', array('jquery', 'google-maps'), '1.0.0', true );
    	wp_enqueue_script( 'map_metier_ville_script');
    }
    [...]

    In single-customPostTypeName.php :

    
    [...]
    $artisans_params = {'my array of data...'}
    wp_localize_script( 'map_metier_ville_script', 'artisans', $artisans_params );
    [...]

    And it should work ?

    • This reply was modified 9 years ago by Nokaa.
    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    Thank you for your help but, I’m a bit embarassed, I don’t really get your answer … :/

    I need to “localize” an array that I create in my custom post-type file, from the result of a wp_query request. I read that we need to enqueue our script just after the function wp_localize_script to make it work, and I presume that I need to call this function where the array of datas is set (so in the custom post-type file), so I need to enqueue my script in this file too, no ?

    Thread Starter Nokaa

    (@nokaa)

    I agree it’s cleaner but, what about the localize function in this case ?

    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    I only need thoses scripts in a custom post-type template, that’s why I don’t want to put it in the functions.php. Is it possible to call them in that template file ?

    In fact, my manager want a fast website loading. the Google API and others scripts like social medias API seem to be the leading cause of slowdown of the page loading, that’s why I don’t really like the idea to call them where they are not needed…

    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    I figured out that I didn’t use a function to enqueue my scripts… my bad.
    I tried to write it like that, but in this way nothing is firing at all.
    I don’t have a single mention of my script or google map script in the logs.
    Seems that the add_action is not fired :

    function metierville_enqueue_scripts(){
    	wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyAhE_CVUWwIOzXs-zD-vFdtgWBFWRBhQVY', array('jquery'), '1.0.0', true);
    	wp_register_script( 'map_metier_ville_script', get_stylesheet_directory_uri().'/assets/js/google_map_metier_ville.js', array('jquery', 'google-maps'), '1.0.0', true );
    	wp_localize_script( 'map_metier_ville_script', 'artisans', $artisans_params );
    	wp_enqueue_script( 'map_metier_ville_script');
    }
    add_action( 'wp_enqueue_scripts', 'metierville_enqueue_scripts' );

    So, I’m always stuck. :/

    I also tried to enqueue my script without registered it, just add the params in the enqueue function.

    • This reply was modified 9 years ago by Nokaa.
    • This reply was modified 9 years ago by Nokaa.
    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    Hi,

    I finally resolved to enqueue my scripts like your example, and it randomly works.
    Most of the time the second script is loaded correctly, but sometimes (particular when refreshing the page) it is called after the API and I got the same error as before and need to refresh again until it works… I don’t really get how the dependance is working but it seems to not be very reliable.

    Is there any way to define a priority manually ?

    Thank you for your help by the way

    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    Finally found my error. The script need to be localized and enqueued in the shortcode function, like this :

    add_action('wp_enqueue_scripts', 'register_scripts');
    
    function register_scripts(){
    	wp_register_script( 'dpe_select_default_script', plugin_dir_url( __FILE__ ) . 'dpe-select-default.js', array('jquery'), '1.0', true);
    }
    
    function dpe_get_atts($atts){
    	extract(
    		$array_variable = shortcode_atts(array(
    			'id' => 'test',
    			'value' => 'testValue',
    		),
    		$atts
    		)
    	);
      
    	wp_enqueue_script('dpe_select_default_script'); 
    	wp_localize_script('dpe_select_default_script', 'dpe', $array_variable);
    }
    
    add_shortcode('dpe-sd', 'dpe_get_atts');
    Thread Starter Nokaa

    (@nokaa)

    Thank you ! I managed doing this with the same process.
    All is working. πŸ™‚

    Nokaa

    (@nokaa)

    add_action( 'wp_enqueue_scripts', 'my_deregister_scripts', 100 );
    
    function my_deregister_scripts() {
       if ( is_page('your_page') ) {
        wp_deregister_script( 'jQuery' );
    
         }
    }

    or simply do the opposite, frame the function you mentionned with a negative condition, it will be cleaner than enqueue and desenqueue a script :

    if ( !is_page('front-page') ) {
        wp_register_script( 'jQuery' );
    }
    Nokaa

    (@nokaa)

    https://codex.wordpress.org/WordPress_Taxonomy#wp_term_taxonomy

    As I understand it, the term_tanoxonmy_id is like a key which make the link between a taxonomy and a term. A term_id is the vertiable id of the term in its table.

    It enable to separate terms which have the same ID but are not registered in the same taxonomy (e.g. if you have a term ‘humour’ twice, one in ‘category’ and the other in ‘tags’).

    • This reply was modified 9 years ago by Nokaa.
    Thread Starter Nokaa

    (@nokaa)

    Thank you, I’ll make some research on it πŸ™‚

    Hm, don’t really like the method using in this tutorial…

    Better way to do it is to mix a div with 50% border-radius and the flex display.
    It’ll be easier to center your content inside the box.

    If you don’t want to got a streched image, you can use a background-image on the div and use the property of background-position (center) and background-size (cover).

    Also, if you realy want to keep an image for a SEO reason, you can manage that effect by position it with the same properties in the div, and a full height property (100%) and an auto width. And if you want to add text over it, just add an absolute span.

    Like this : https://codepen.io/anon/pen/EmVRRZ

    The link you mentioned leads to a 404 error (page not found), so the page doesn’t exist well (this sentence is strange). And there’s no ‘About’ button visible in your menu.

    I presume you already remove it, maybe try to clear your browser cache ?

    Edit: Passed when searching the different problems on your website. πŸ™‚

    • This reply was modified 9 years, 1 month ago by Nokaa.
    • This reply was modified 9 years, 1 month ago by Nokaa.
Viewing 15 replies - 16 through 30 (of 33 total)