Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Remove redundant loading of project-specific JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
dotherightthing committed Jan 28, 2021
1 parent e8dc35d commit f3c0ea4
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions src/class-wpdtrt-anchorlinks-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,39 +262,18 @@ public function render_html( DOMNode $n, $include_target_tag = false ) : string
/**
* Add project-specific frontend scripts
*
* @version 0.0.1
* @since 0.7.1
* Use this function to:
* - load scripts in addition to js/frontend-es5.js (via wp_enqueue_script)
* - add keys to wpdtrt_anchorlinks_config (via wp_localize_script)
*
* Don't use function this to:
* - add ES6 scripts requiring transpiling (load them using frontend.txt instead)
*
* @see wpdtrt-plugin-boilerplate/src/Plugin.php
*/
public function render_js_frontend() {
$attach_to_footer = true;

// init
// from Plugin.php + extra dependencies.
wp_enqueue_script( $this->get_prefix(),
$this->get_url() . 'js/frontend-es5.js',
array(
// load these registered dependencies first:.
'jquery',
),
$this->get_version(),
$attach_to_footer
);

// from Plugin.php.
wp_localize_script( $this->get_prefix(),
$this->get_prefix() . '_config',
array(
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// but we need to explicitly expose it to frontend pages.
'ajaxurl' => admin_url( 'admin-ajax.php' ), // wpdtrt_foobar_config.ajaxurl.
'options' => $this->get_options(), // wpdtrt_foobar_config.options.
)
);

// Replace rather than extend, in order to specify dependencies:
// parent::render_js_frontend();.
public function render_js_frontend() { // phpcs:ignore
// If editing this function, remove this line to replace the parent function.
parent::render_js_frontend();
}

/**
Expand Down

0 comments on commit f3c0ea4

Please sign in to comment.