0

I am new to wp, fair warning :)

I have added a new blank page by adding a php file in the twentytwenty theme folder with:

<?php
/* Template Name: charles */ 

?>

Then, I have created a new page by choosing the charles template in the admin.

Now In my new blank page I can see the contents of the new plugin I am creating which is what I want. So great till here.

But now, I need to add a css custom file, a js custom file and a call to the jquery cdn url. I've tried by changing twentytwenty functions.php adding:

function wpdocs_charles_scripts() {
    wp_enqueue_style( 'myStyle', get_stylesheet_uri() );
    wp_enqueue_script( 'myScript', get_template_directory_uri() . '/myFiles/myScript.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_charles_scripts' );

But it does not work for my http://localhost/wordpress/charles/ Also I've tried using Simple Custom CSS and JS but this changes the styles of the whole page but not the styles in charles.

My last try in my new plugin php:

add_action('init', 'register_script');
function register_script() {
    wp_register_script( 'myScript', plugins_url('/myFiles/myScript.js', __FILE__), array('jquery'), '2.5.1' );

    wp_register_style( 'myStyle', plugins_url('/myFiles/myStyle.css', __FILE__), false, '1.0.0', 'all');
}

add_action('wp_enqueue_scripts', 'enqueue_style');

function enqueue_style(){
   wp_enqueue_script('myScript');

   wp_enqueue_style( 'myStyle' );
}

Please, help I think I am missing something basic.

Thanks a lot

3
  • 1
    wp_enqueue_scripts hook is call in wp_head() function. So if your template doesn't call wp_head() function wp_enqueue_scripts will never be call.
    – ZecKa
    Commented May 14, 2020 at 10:08
  • How does your plugin deliver the content to the page? If you look at the source of the page, are there any other css/js files enqueued (like jquery)?
    – cjbj
    Commented May 14, 2020 at 10:19
  • Thanks ZecKa, but if I add get_header() I don't get a total blank page anymore. Any way to go avoid it? Thanks!!
    – ADM
    Commented May 14, 2020 at 10:42

2 Answers 2

0

In my theme functions.php at the end works for me:

function themesCharles_enqueue_style() {
    wp_enqueue_style( 'my-theme', get_template_directory_uri() .'/myFiles/myStyle.css', false );
}

function themesCharles_enqueue_script() {
    wp_enqueue_script( 'my-js', get_template_directory_uri() .'/myFiles/myScript.js', false );
}

add_action( 'wp_enqueue_scripts', 'themesCharles_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themesCharles_enqueue_script' );
-1

You should try this function. This will run only when the page template is charles.

if ( is_page_template( 'charles.php' ) ) {
    // Enqueue the script and CSS here
}

Please try this and let me know.

5
  • thanks a ton!! but where, in which file should I try it? charles.php ??
    – ADM
    Commented May 14, 2020 at 11:02
  • 1
    Try to add it in functions.php call a hook and embed that function inside that hook. Commented May 14, 2020 at 11:31
  • thanks for the tip on functions.php but the if you suggested did not work. Thanks again!
    – ADM
    Commented May 14, 2020 at 14:03
  • Can I have a look at the code that you applied in functions.php? Commented May 14, 2020 at 14:12
  • I posted it bellow my question
    – ADM
    Commented May 14, 2020 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.