0

Writing my first Wordpress plugin that uses a Class approach.

It is being called on a page via a shortcode. In testing, the shortcode is working and the shortcode specific php file is being loaded.

However, the 2 add_action calls are not firing:

//Register Styles
add_action( 'wp_enqueue_style', array( $this, 'register_styles' ) );

//Register Scripts
add_action( 'wp_enqueue_script', array( $this, 'register_scripts' ) );

When I test with var_dump, the register_styles/scripts functions are not even running, yet all other functions called in __contruct() are.

Here is the full code:

<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

add_action( 'plugins_loaded', array ( 'Lipstick_Consultation', 'init' ));

if (!class_exists('Lipstick_Consultation')){

class Lipstick_Consultation{

    public static function init() {
        $class = __CLASS__;
        new $class;
    }

    /**
     * @since 1.0
     */
    public function __construct() {

        //get contstants
        $this->setup_constants();

        //get file includes
        $this->includes();

        //Register Styles
        add_action( 'wp_enqueue_style', array( $this, 'register_styles' ) );

        //Register Scripts
        add_action( 'wp_enqueue_script', array( $this, 'register_scripts' ) );

        $this->register_shortcodes();

        return $this;


    }

    /**
     * Include our Class files
     *
     * @access private
     * @since 1.0.0
     * @return void
     */
    private function includes() {

        /****SHORTCODES****/
        //[slider_person]
        require_once LC_DIR . 'inc/shortcodes/slider.php';
    }

    /**
     * Setup plugin constants
     *
     * @access private
     * @since 1.0.0
     * @return void
     */
    private function setup_constants() {

        // Plugin information
        define( 'LC_VERSION',       '1.0.0' ); // Current plugin version
        define( 'LC_URL',           plugin_dir_url( __FILE__ ) );
        define( 'LC_DIR',           plugin_dir_path( __FILE__ ) );
    }

    /**
     * Register Styles
     *
     * @access public
     * @since 1.0.0
     * @return void
     */
    public function register_styles() {

        //main style file
        wp_register_style( 'consultation_style', LC_URL . "assets/css/style.css", array(), time(), 'all' );
        wp_enqueue_style( 'consultation_style' );

        //styles for full page plugin
        wp_register_style( 'lc_full_page_style', LC_URL . "assets/css/jquery.fullPage.css", array(), time(), 'all' );
        wp_enqueue_style( 'lc_full_page_style' );

    }

    /**
     * Register Scripts
     *
     * @access public
     * @since 1.0.0
     * @return void
     */
    public function register_scripts() {

        //Script that makes full width/height/page divs
        wp_register_script( 'lc_full_page', LC_URL . "assets/js/jquery.fullPage{SUFFIX}.js", array( 'jquery' ), time() );
        wp_enqueue_script( 'lc_full_page' );

    }

    private function register_shortcodes(){
        add_shortcode("lipstick_person","shortcode_person_slider");
    }

}
}?>
1

There is no action called wp_enqueue_script, it's wp_enqueue_scripts, plural, and both scripts and styles should be enqueued on that action.

0
<?php

if( !class_exists('SiteOrigin_Installer_Theme_Admin') ) {
    class SiteOrigin_Installer_Theme_Admin {

        function __construct(){
            add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
            add_action( 'admin_menu', array( $this, 'add_admin_sub_page' ), 15 );
            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 15 );
            add_action( 'wp_ajax_so_installer_theme_headers', array( $this, 'theme_headers_action' ) );
        }

        static function single(){
            static $single;
            if( empty($single) ) {
                $single = new SiteOrigin_Installer_Theme_Admin();
            }

            return $single;
        }

        /**
         * Add the theme admin page
         */
        function add_admin_page() {
            if ( empty ( $GLOBALS['admin_page_hooks']['siteorigin'] ) ) {
                add_menu_page(
                    __( 'SiteOrigin', 'siteorigin-installer' ),
                    __( 'SiteOrigin', 'siteorigin-installer' ),
                    false,
                    'siteorigin',
                    false,
                    plugin_dir_url( __FILE__ ) . '/img/icon.svg',
                    66
                );
            }
        }

        function add_admin_sub_page(){
            add_submenu_page(
                'siteorigin',
                __('Install Themes', 'siteorigin-installer'),
                __('Install Themes', 'siteorigin-installer'),
                'install_themes',
                'siteorigin-themes-installer',
                array( $this, 'display_themes_page' )
            );
        }

        /**
         * @param $prefix
         */
        function enqueue_scripts( $prefix ){
            if( $prefix !== 'siteorigin_page_siteorigin-themes-installer' ) return;
            wp_enqueue_style( 'siteorigin-installer-themes', plugin_dir_url(__FILE__) . '/css/themes.css', array( ), SITEORIGIN_INSTALLER_VERSION );
            wp_enqueue_script( 'siteorigin-installer-themes', plugin_dir_url(__FILE__) . '/js/themes.js', array( 'jquery' ), SITEORIGIN_INSTALLER_VERSION );
        }

        /**
         * Display the theme admin page
         */
        function display_themes_page(){

            if( !empty( $_GET['install_theme'] ) && !empty( $_GET['theme_version'] ) ) {
                // The user is installing a theme
                $this->display_install_page();
                return;
            }

            $themes = SiteOrigin_Installer::single()->registered_themes();
            $latest_versions = get_transient( 'siteorigin_installer_theme_versions' );
            if( empty($latest_versions) ) $latest_versions = array();
            $updated = false;

            foreach( $themes as $slug => $theme ) {
                if( !empty( $latest_versions[$slug] ) ) continue;

                $version = SiteOrigin_Installer::single()->get_theme_version( $slug );
                if( $version === false ) continue;

                $latest_versions[$slug] = $version;
                $updated = true;
            }

            if( $updated ) {
                // Cache for 12 hours
                set_transient( 'siteorigin_installer_theme_versions', $latest_versions, 43200 );
            }

            // We need to know the current theme
            $current_theme = wp_get_theme();

            // Increase the current themes weight
            foreach( $themes as $slug => $theme ) {
                if( $slug == $current_theme->get_stylesheet() ) $themes[$slug]['weight'] = 999;
            }

            // Sort the themes by weight
            uasort( $themes, array( $this, 'sort_theme_compare' ) );

            include plugin_dir_path( __FILE__ ) . '/tpl/themes.php';
        }

        /**
         * Comparison function for sorting
         *
         * @param $a
         * @param $b
         *
         * @return int
         */
        function sort_theme_compare( $a, $b ){
            if( empty($a['weight']) || empty($b['weight']) ) return 0;
            return $a['weight'] < $b['weight'] ? 1 : -1;
        }

        /**
         * Display the plugin install page
         */
        function display_install_page(){
            check_admin_referer( 'siteorigin-install-theme' );
            require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes

            $slug = !empty( $_GET['install_theme'] ) ? $_GET['install_theme'] : false;
            $version = !empty( $_GET['theme_version'] ) ? $_GET['theme_version'] : false;

            if( empty($slug) || empty($version) ) {
                wp_die( __('Error installing theme', 'siteorigin-installer') );
            }

            ?>
            <div class="wrap">
                <?php
                // This is where we actually install the theme

                $theme_url = 'https://wordpress.org/themes/download/' . urlencode( $slug ) . '.' . urlencode( $version ) . '.zip';

                $title = sprintf( __('Installing Theme: %s'), $slug . ' ' . $version );
                $nonce = 'install-theme_' . $slug;
                $url = 'update.php?action=install-theme&theme=' . urlencode( $slug );
                $type = 'web'; //Install theme type, From Web or an Upload.

                $upgrader = new Theme_Upgrader( new Theme_Installer_Skin( compact('title', 'url', 'nonce', 'plugin', 'api', 'type') ) );
                $upgrader->install( $theme_url );

                ?>
            </div>
            <?php
        }

        function theme_headers_action(){
            if( empty($_GET['_sononce']) || !wp_verify_nonce( $_GET['_sononce'], 'theme_info' ) ) exit();
            if( empty( $_GET['theme_slug'] ) || empty( $_GET['theme_version'] ) ) exit();

            // header('content-type: application/json');
            $data = SiteOrigin_Installer::single()->get_theme_data( $_GET['theme_slug'], $_GET['theme_version'] );
            echo json_encode( $data );
            exit();
        }
    }
}
SiteOrigin_Installer_Theme_Admin::single();
  • 1
    Hi Jeffery, Welcome to WPSE! It would be great if you could explain how the code answers the question. Just posting some code won't be helpful. – bravokeyl Jun 29 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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