Search code examples
phpwordpressgenesis

What's wrong with this Genesis PHP WordPress plugin code?


<?php
/*
* Plugin Name: Add Fontawesome Animation
* Plugin URI: ...
* Description: This plugin is used to animate Fontawesome icons
* Version: 1.0.0
* Author: ...
* Author URI: ...
* License: GPLv2
* * */

// Enqueue font-awesome-animation.css
add_action( 'wp_enqueue_style', 'add_fontawesome_animation' );
function add_fontawesome_animation() {
    wp_enqueue_style('font_awesome_animation', get_stylesheet_directory_uri() . '/css/font-awesome-animation.css' );
}

Objective: load font-awesome-animation.css so FontAwesome icons can animate.

Using: PhpStorm and just getting started with Genesis. I am using the genesis-sample child theme and for the most part all is going reasonably well publishing posts and learning to use code to mod the templates.

The FontAwesome icons will animate when I @import url("css/font-awesome-animation.css") into the style.css file else I have not been able to enqueue font-awesome-animation.css from functions.php or the plugin as shown above. I get no observable errors in the editor or in the page nor do I get the typical all white page when there are errors in the PHP. Something is FUBAR and I don't know how to do this type of debugging in WordPress yet so I need to know what may be wrong with the code and suggestions about how to debug this type of SNAFU. Is there some type of Response.Write I can use to show me pathing and such?


Solution

  • Try changing the hook you are using to wp_enqueue_scripts. From the codex:

    wp_enqueue_scripts is the proper hook to use when enqueuing items that are meant to appear on the front end. Despite the name, it is used for enqueuing both scripts and styles.

    Also get_stylesheet_directory_uri() will not work for you if you are making a plugin. That is only for use in themes/child themes. If you are calling this from your main plugin file you can use plugin_dir_url( __FILE__ ) instead. Again, from the codex:

    Gets the URL (with trailing slash) for the plugin FILE passed in

    In this case we are passing the PHP magic constant __FILE__ to get the uri for the main plugin file with a trailing slash.

    So your code would look like the following:

    <?php
    // Enqueue font-awesome-animation.css
    add_action( 'wp_enqueue_scripts', 'add_fontawesome_animation' );
    
    function add_fontawesome_animation() {
    wp_enqueue_style('font_awesome_animation', plugin_dir_url( __FILE__ ) . 'css/font-awesome-animation.css' );
    }