1

I am trying to write a widget and I need to add the color picker to my widget form. I want to add the script only on widget.php page and not on all the admin pages.

Is there a way that I can detect the page inside the construct function of my widget? If not how I can include the script only when I'm on widget.php page?

1 Answer 1

2

You may use the global variable $pagenow to figure out if you are on a particular admin page, in your case this would be checking if you are on the widgets.php admin page:

<?php 

    global $pagenow;

    if( $pagenow === 'widgets.php' ) {
        ?>
        <script>

            // JavaScript goes here

        </script>
        <?php 
    }
 

Furthermore, it will be helpful for you to use the plugin Query Monitor so that you may easily find out what conditionals may be used on a particular instance.

1
  • Or just $GLOBALS['pagenow'], which has the benefit of fitting into one line, no need to declare the global access on a separate line. Commented Mar 1, 2024 at 16:27

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.