3

I have created a widget for wordpress. The form of the widget has some input boxes. Now, I want to add a custom input box which uses a javascript file I downloaded from http://jscolor.com/

Now, the problem is, it is not working. I have registered the javascript file like this in the form function of the WP_Widget class.

require_once('index.php');
add_action('admin_enqueue_scripts', 'pw_load_scripts');

The pw_load_scripts function is in index.php

function pw_load_scripts() {
    wp_register_script('custom-js', plugin_dir_url(__FILE__).'scripts/jscolor.js');
    wp_enqueue_script('custom-js');
}

After all these, it is not working. What is the correct way to do this task?

My error.log has this error

[Mon Oct 07 21:37:30.591896 2013] [:error] [pid 14853] [client 127.0.0.1:42453] PHP Fatal error:  Cannot redeclare _wp_menu_output() (previously declared in /var/www/wordpress/wp-admin/menu-header.php:36) in /var/www/wordpress/wp-admin/menu-header.php on line 36

Thank you

5
  • What does the PHP error log say, or what other visible errors do you see? Commented Oct 7, 2013 at 15:26
  • In widgets.php, only widgets till above and left of my widget can be seen. Others cannot be seen. Commented Oct 7, 2013 at 15:45
  • What does the PHP error log say, and also the Javascript console. Please check both and make sure there are no errors. Commented Oct 7, 2013 at 15:57
  • This is the error I get in error log ' [Mon Oct 07 21:37:30.591896 2013] [:error] [pid 14853] [client 127.0.0.1:42453] PHP Fatal error: Cannot redeclare _wp_menu_output() (previously declared in /var/www/wordpress/wp-admin/menu-header.php:36) in /var/www/wordpress/wp-admin/menu-header.php on line 36' Commented Oct 7, 2013 at 16:08
  • That's a Fatal Error, meaning that the site wouldn't be working... I guess it's not related, and it's dated from October anyway. Commented Oct 7, 2013 at 16:12

2 Answers 2

2

The correct hook is admin_print_scripts 1 to enqueue styles and scripts.

Note that admin_footer and admin_head also accept the same screen targeting 'actionname-$hook':

add_action( 'admin_print_scripts-widgets.php', 'admin_enqueue_so_19228543' );

function admin_enqueue_so_19228543()
{
    wp_enqueue_script( 
            'my-script', 
            plugins_url( '/my-script.js', __FILE__ ), 
            array(), // dependencies
            false, // version
            true // on footer
    );
    wp_enqueue_style( 
        'my-style', plugins_url( '/my-style.css', __FILE__ ) 
    );

}

1 The Codex is saying otherwise, but I was pretty sure this was an official stance. I'm researching and will report back.


update

I cannot find any reference to admin_print_scripts being the correct hook, although it works and I've seen used this way many times. For completeness, this is how admin_enqueue_scripts works:

add_action( 'admin_enqueue_scripts', 'admin_enqueue_so_19228543' );

function admin_enqueue_so_19228543( $hook )
{
    if( 'widgets.php' != $hook )
        return;

    wp_enqueue_script( 
            'my-script', 
            plugins_url( '/my-script.js', __FILE__ ), 
            array(), // dependencies
            false, // version
            true // on footer
    );
    wp_enqueue_style( 
        'my-style', plugins_url( '/my-style.css', __FILE__ ) 
    );

}

Related: Difference between do_action('admin_enqueue_scripts', $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax

Sign up to request clarification or add additional context in comments.

Comments

1

Try removing:

require_once('index.php');

I'm not sure where you got this from. It appears to be trying to reload wordpress' index.php file.

2 Comments

Hey thanks, the error got removed. But, the color picker is still not working. It is just a normal input box.
Does your input element look like: <input class="color">

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.