I want to write some JavaScript and want to assign that to a custom capability, I explored a lot but unfortunately none of them worked, any help will be appriciated.

My codes are as below:

add_action('init', function() {
    if (current_user_can('disable_image'))
        //add javascript here
  • You want to load certain JavaScript on the page for certain user roles? Is that correct? What does the JavaScript do? Keep in mind that it's trivial for users to disable JavaScript, so if you want to use it to restrict a user's capabilities, it's likely not a good solution. May 19, 2021 at 5:25
  • 1
    This may depend on how exactly you have defined the custom capability. See this answer for the correct way: wordpress.stackexchange.com/questions/35165/…
    – cjbj
    May 19, 2021 at 7:21

2 Answers 2

function admin_footer_hook(){
    <script type="text/javascript">
add_action( 'admin_footer-post.php', 'admin_footer_hook' );

Javascript is a client-side technology - in other words, it only runs on the rendered browser page. It seems to me like you're looking for a server-side solution - ie, one that runs when the page is being built before it is delivered to the browser. So, you might want to rethink your approach.

However, it is possible to use javascript to make text fields, etc, uneditable, but how successful that would be depends on any existing JS which may also be affecting those fields. Either way, you would need to output your JS code to the page, and to do that you would need to use the right WordPress enqueue action, depending on your context.

So, if your form is on the user side of your site, you would enqueue your script using the wp_enqueue_scripts hook:

function my_enqueue_script() {
    if ( current_user_can('disable_image') )
        wp_enqueue_script( 'my_js', 'my-script.js', false );
add_action( 'wp_enqueue_scripts', 'my_enqueue_script' );

If your form is on the admin side, you would use the admin_enqueue_scripts hook:

function my_enqueue_admin_script( $hook ) {
    if ( 'edit.php' == $hook && current_user_can('disable_image') )
        wp_enqueue_script( 'my_admin_js', 'my-admin-script.js', array(), '1.0' );
add_action( 'admin_enqueue_scripts', 'my_enqueue_admin_script' );

(you would need to determine the exact admin page to apply this to - 'edit.php' is just an example)

You can find more info about both of these hooks from their respective Codex pages: wp_enqueue_scripts: https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/ admin_enqueue_scripts: https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/

Hope that steers you in the right direction.

  • 1
    I meant inline, in my functions
    – Ricky
    May 21, 2021 at 11:10
  • Then you're asking for something that simply isn't possible. PHP functions run on the server. You can't run JS on the server in PHP, you can only run JS on the client side in the browser.
    – Peter HvD
    May 22, 2021 at 12:05

Your Answer

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

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