Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #1200310

    i’m trying to figure out how to add custom css &/or javascript/jquery to single pages.

    if i have some custom JS, i know i can add it to the “Code Block” – but the problem with that is, sometimes it takes up space in the page. for some reason, when putting in a codeblock after certain other blocks (like the “Grid Row” for example), it doesn’t just add <script> etc, but instead, it puts whatever i add there inside it’s own container_wrap & section etc – which adds height & whatever other attributes is in css for it.
    this is tolerable for small one-off scripts for something.

    but what about adding more substantial scripts & styles that only needs to be used on one page?
    i found function options to add scripts to certain pages, but, then that code is being processed still for every page because it’s in the main functions.php file – right?
    same principle, i don’t want to put my custom code for a single page in the header/footer php files either.

    or am i not understanding how that works? if i use that “is_page(####)” function option, does the other pages skip reading whatever code i have in that section?

    in a different direction, i searched for plugins that might help with this, but i’m not sure if that would be wise or not. would that just load unnecessary bloat also?
    for css i found https://wordpress.org/plugins/postpage-specific-custom-css
    for js i found https://wordpress.org/plugins/simple-embed-code
    IF using a plugin to add custom css & js fields in a page edit is a good idea, would these be the best ones?

    advice appreciated.


    Hey SyberKnight,

    The easiest option is likely the code block option, if you don’t want to put the code in functions.php. I’m not sure exactly how that is processed, but it will stop processing if the is_page() function returns false. That means it won’t be added to any other pages than the ones you specify in the function.

    Best regards,


    @Rikard, thank you!

    so, just to be clear – to make sure i completely understand…

    when the pages load, & the functions.php file gets read, everything inside the function with “is_page(###)” gets skipped for all pages that are not that particular page, & so avoiding any performance hits that code may cause – right?

    okay, so, i followed the above link to get the “if(is_page( 3739 ))” bit. is that correct? where do i find the number associated with the pages?

    next, i followed the link provided there to https://kriesi.at/documentation/enfold/add-custom-js-or-php-script and find this code (& i added the “if is page” part)…

    function my_custom_function(){
    if(is_page( 3739 )){
            // Your function here
                console.log('Hello World!');
    add_action('wp_footer', 'my_custom_function');

    …if i’m adding my code to the main Functions.php file, then why is there the “add_action(‘wp_footer’)” part there? do i change that be ‘wp_functions’ or something?

    also, where in the page load does the function.php parts come in – the head or foot?
    if i need to add some external script references and some js form validation and some css for the form, would that be better put in the header.php file or the functions.php? it looks like, according to the linked doc page, that the same code is used for either file.

    and lastly, later in that doc page it talks about needing to use “use strict” for custom js. i’m confused by this because i’ve seen other js code added to functions like this that do not use strict mode (like even in the examples before that one). what are the criteria for needing to use this strict part?

    i also noticed inconsistencies with using “jQuery” vs the dollar symbol – is that simply interchangeable?

    inside a function, between the closing ?> & rebeginning of <?php, is that just an open html area? so conceivably, i could do something like this…

    function my_custom_function(){
    if(is_page( 3739 )){
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
             $(document).ready(function() {
    	     $('form').each(function() {
    form {
    /*css styles*/
    add_action('wp_head', 'my_custom_function');

    …or do i have to download the cdnjs file(s) & put them in my child theme/js folder & use this example from the doc page?…

    function my_custom_scripts() {
            get_stylesheet_directory_uri() . '/js/my_custom.js',
            array( 'jquery' )
    add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

    …if so, then what does this “wp_enqueue_scripts” thing?

    sorry, but as i got going, i realized that i’m still confused on the more detailed points.


    okay, after a LOT of reading, i believe i better understand this. here’s what i’ve come up with, for reference…

    first – the inconsistencies i see in the Enfold documentation are just that, inconsistencies.
    from what i understand, we should add the “use strict” to all our added jquery/js.

    second – if we add “jQuery” instead of “$” to the main function wrapper, then the other dollar signs won’t cause conflicts in WP.

    thirdly – the proper way to add external JS & CSS is with the wordpress “enqueue” options. as you can see in my final code below, there’s “wp_enqueue_script” to add js & “wp_enqueue_style to add css. and it’s recommended to add a js & css folder in your child theme for such files.
    note, the “get_stylesheet_directory_uri” is correct for both css & js files. but also note, it’s a little different if your referencing files outside of your child theme folder.

    and lastly – the “is_page()” part is what makes this code in the functions file to be skipped except for that particular page id. and the way to find that page number is when you go and “edit” the page, the look at the url, it’s after “post=”.

    function paypal_forms() {
    if(is_page( 2132 )){
        wp_enqueue_script('name-of-library-here', 'https://cdnjs.cloudflare.com/ajax/libs/path-to-desired-library.js', array('jquery'), 1.19, false);
        wp_enqueue_script('name-of-my-script-here', get_stylesheet_directory_uri().'/js/filename-of-my-script.js', array('jquery'), 1.0, false);
        wp_enqueue_style('name-of-my-style-here', get_stylesheet_directory_uri().'/css/filename-of-my-style.css', array(), '1.1', 'all');
    add_action( 'wp_enqueue_scripts', 'paypal_forms' );

    hope that helps,

    • This reply was modified 3 years, 1 month ago by SyberKnight.

    Hi SyberKnight,

    Glad you got it working for you and thank you for sharing! :)

    If you need further assistance please let us know.

    Best regards,

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.