I'm trying to get a widget to only load javascript on a page where the widget is present.
I've tried adding the add action in the 'showWidget' didn't work.
What am I doing wrong?
PHP
wp_register_sidebar_widget('MyWidget','MyWidget', 'showWidget');
add_action('wp_enqueue_scripts', 'addScript'); //now the script appears on every page
function addScript()
{
wp_register_script('MyWidgetJs', plugins_url( '/script.js' , __FILE__), array('jquery'));
wp_enqueue_script('MyWidgetJs');
}
function showWidget($args)
{
// add_action('wp_enqueue_scripts', 'addScript'); //I tried this but it doesn't work :(
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}
Seems that the new version of WordPress supports this: http://codex.wordpress.org/Version_3.3
Register your script, but don't enqueue it. In your widget PHP, add in the wp_enqueue_script('your_script_name'); and it'll load it only when your widget is used and place it in the footer.
You are going to upgrade to 3.3, right? :)
SO, something like this should work just fine:
wp_register_sidebar_widget('MyWidget','MyWidget', 'showWidget');
function showWidget($args) {
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}
You want to use is_active_widget() conditional to do something like:
<?php
if ( is_active_widget('MyWidget') ) {
add_action('wp_enqueue_scripts', 'addScript');
}
?>
using your above code the final widget may look like:
<?php
if ( is_active_widget('MyWidget') ) {
add_action('wp_enqueue_scripts', 'addScript');
}
function addScript()
{
wp_register_script('MyWidgetJs', plugins_url( '/script.js' , __FILE__), array('jquery'));
wp_enqueue_script('MyWidgetJs');
}
function showWidget($args)
{
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}
Related
I'm trying to create a plugin for shortcodes. But my activation hook is not working. Plugin is activated in my wp-admin/plugins page but nothing works which is in my code like:
My enqueue & shortcode functions are not working. I've tried plugin_dir_path(__FILE__) & plugin_dir_url(__FILE__) b ut nothing works.
Here is my code:
if (!class_exists( 'DiliviBlocks' )) {
class DiliviBlocks {
public function __construct() {
$this->setup_actions();
$this->plugin_dir = plugin_dir_path(__FILE__);
}
/**
* Setting up Hooks
*/
public function setup_actions() {
register_activation_hook( plugin_dir_path(__FILE__), array( $this, 'activate_plugin' ) );
}
/**
* Activate callback
*/
public function activate_plugin() {
add_action( 'wp_enqueue_scripts', array($this, 'dilivi_blocks_scripts') );
add_shortcode( 'vans_search_form', array($this, 'vans_search_form_shortcode') );
}
public function dilivi_blocks_scripts() {
wp_enqueue_style( 'dilivi-blocks-plugin-css', plugin_dir_path(__FILE__) . '/assets/css/styles.css');
wp_enqueue_script( 'dilivi-blocks-plugin-jquery', plugin_dir_path(__FILE__) . '/assets/js/jquery.js' );
wp_enqueue_script( 'dilivi-blocks-plugin-js', plugin_dir_path(__FILE__) . '/assets/js/scripts.js' );
}
public function vans_search_form_shortcode($atts) {
return 'Hello World';
}
}
$diliviBlocks = new DiliviBlocks();
}
Please help me. I'm stuck
The documentation states that you only need the plugin directory name, and the name of the initial file. You should not include the whole path to it.
If your plugin contains only the file, than just __FILE__ would suffice.
In your case, it will probably be something like dilivi-blocks/divili-blocks.php.
Also, be aware that functions will only be executed once -- when your plugin is activated. If it is already active, the functions will not execute.
You should know that enqueueing stylesheets and the like will have to be done every time a page loads, so binding them to a single-use hook will not work. The wp_enqueue_scripts hook should be used for that.
I have a page speed issue, the theme I bought is really crappy but i cannot change it now. I use WP_rocket, server have HTTP2 but still it is to many resources to load. So i try to reduce numbers of styles by wp_deregister_style and load it only when need. For example contact-form-7 front-end style I need only in .../contact page. It good idea ? Or it could be harmful?
function rs_deregister_css () {
global $wp;
$url = home_url( $wp->request);
$contakt = strpos($url,'contakt');
if (!$contakt) {
wp_deregister_style('contact-form-7');
}
}
add_action( 'wp_print_styles', 'rs_deregister_css', 99);
Yes, its a very good idea since you only use the contact form in the contact page, don't forget to deregister the javascript file, too
if (!$contakt) {
wp_deregister_style('contact-form-7');
wp_deregister_script('contact-form-7');
}
Yes, it would be a good idea for your load time. But I suggest you to load style on specific instead of checking the URL and unload it every time.
Try to read Conditional Tags
function my_enqueue_stuff() {
if ( is_front_page() ) {
/** Call landing-page-template-one enqueue */
wp_enqueue_style( 'your-style-handle', get_stylesheet_directory_uri() . '/yourfile.css' );
} else {
/** Call regular enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
EDIT
Since you only want to the style or javascript load for specific pages, then add this code to your functions.php:
function remove_wpcf7_extras() {
remove_action('wp_print_scripts', 'wpcf7_enqueue_scripts');
remove_action('wp_print_styles', 'wpcf7_enqueue_styles');
}
if( !is_page('contact') ) {
add_action('wp_head', 'remove_wpcf7_extras');
}
I have a theme that is adding a custom, full-screen background image via jQuery. The theme is doing this via a class object called td_background. Within the class is a function called wp_head_hook(), and within this hook, a filter is being added for the custom bg. It looks something like this:
class td_background {
// ..some stuff
function __construct() {
add_action('wp_head', array($this, 'wp_head_hook'), 10);
}
function wp_head_hook() {
add_filter( 'td_js_buffer_footer_render', array($this, 'add_js_hook'));
}
function add_js_hook($js) {
// Custom JS added here for background image
return $js
}
}
new td_background();
I'm now trying to de-register the add_js_hook in a custom plugin I'm writing, but am having trouble wrapping my mind around how to do it with all this nesting. I've tried a few things, such as:
<?php
// This...
remove_filter( 'wp_footer', array($td_background, 'td_js_buffer_footer_render'));
// ...and this
remove_filter( 'wp_footer', 'td_js_buffer_footer_render');
// ...and even
remove_filter( 'wp_footer', 'add_js_hook', 100);
?>
I've also tried changing the above to wp_head.
Thoughts? My end goal is to de-register this JavaScript in the footer, so that I can add my own in place of it.
As it's being instantiated anonymously, we have to use the handy function remove_anonymous_object_filter() from WPSE, it would be something like:
// Run this from a plugin
add_action( 'template_redirect', 'kill_anonymous_example', 0 );
function kill_anonymous_example() {
remove_anonymous_object_filter(
'wp_head',
'td_background',
'wp_head_hook'
);
}
I tested killing wp_head as I don't have a td_js_buffer_footer_render running.
I was wondering if anyone could help me by explaining if it is possible to enqueue a javascript file only if for example homepage.php is included.
what i have tried to do inside homepage.php:
class homepage_js {
static $add_script;
static function init() {
add_action('init', array(__CLASS__, 'register_script'));
add_action('wp_footer', array(__CLASS__, 'print_script'));
}
static function register_script() {
wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
}
static function print_script() {
if ( ! self::$add_script )
return;
wp_print_scripts('my-script');
}
}
homepage_js::init();
Thanks in advance for help.
If this is for your wordpress home page you can do this in functions.php
add_action( 'wp_enqueue_scripts' , my_enqueue_scripts );
function my_enqueue_scripts(){
if ( is_home() ) {
wp_enqueue_script( ... );
}
}
How to get following to work properly in Wordress Theme functions.php file?
I haven't figured out how to make the top function available to the bottom function within the theme's functions.php file. I'm not grasping how to setup hooks so they can work together. Thank you.
Filter/helper/whateveryoucallit function:
function poohToPee( $pooh_log )
{
switch( $pooh_log )
{
case 'gross-poop':
$pee_equivalent = 'Grossest of Pees';
break;
case 'ok-poop':
$pee_equivalent = 'Bland Snack Pee';
break;
case 'shang-tsung-plop':
$pee-equivalent = 'Random U-Stream';
break;
}
return $pee_equivalent;
}
Ajax handler function:
function screw_loose()
{
if( isset($_REQUEST['pooh_log']) )
{
echo poohToPee( $_REQUEST['pooh_log'] );
}
}
add_action('wp_ajax_priv_screw_loose', 'screw_loose')
The add_action usually calls the function you are passing through at the point that hook is called.
Since you are using some sort of ajax hook are you really able to make sure your function isn't being called? It wouldn't be echo-ing anything out to the screen since it is running in the background.
Normally any function you define in functions.php is readily available to use within the theme.
It is obviously normally best to organize and have classes, in which case you'd pass the method to the hook as an array, for example add_action( 'admin_init', array( $this, 'someFunction' ) ); and that add_action I just did would be put in the __construct function of the class.
For instance you could do this:
class helloWorld
{
function __construct()
{
add_action( 'admin_init', array( $this, 'echoItOut' ) );
}
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
Alternatively you could also do this:
class helloWorld
{
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
add_action( 'admin_init', array( $helloWorld, 'echoItOut' ) );
Or simply:
function echoItOut()
{
echo 'Hello World';
}
add_action( 'admin_init', 'echoItOut' );
If you put any of these blocks of code I provided in your functions.php file and go to your Dashboard you will see 'Hello World' printed out at the top under the admin bar most likely (might vary from theme to theme if the dashboard has custom styling).