1

My JS script runs on every page, but I want it to run only on the post editor page.

function sm_validate_title() {
        echo "
        <script>
        console.log('script working')
        ...code to validate title and content
        </script>";
}

add_action('admin_footer', 'sm_validate_title_client_side');

Maybe I should try a different hook? or add some condition on the callback function? I don't know really which hook would work, there are so many, and no idea also on how to check if page == post page with JS.

2
  • The classic editor or the block editor? Nov 16, 2022 at 13:13
  • On the right I can see "Post" and "Block" tabs, so I'm guessing it's a block editor?
    – MrFacundo
    Nov 16, 2022 at 13:58

1 Answer 1

0

The function get_current_screen gets the current screen object.

I used a condition inside my callback function to return a script only if the screen id == post:

if( $currentScreen->id === "post" ) {
        echo "
        <script>
        </script>";
    }

https://developer.wordpress.org/reference/functions/get_current_screen/

Your Answer

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

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