im Trying to Add scripts on custom post add/edit pages but Not Working
PHP Code ,(it’s working and Get Js Code)
if(strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/page-new.php')) {
wp_enqueue_script('myscript', 'http://exemple.com/wp-content/themes/theme/js/rt_api.js');
The Probleme is Js Code Not Give Result :
There are a few things you’ll want to do to make you include work and in generally better. Here is the complete code:
admin_enqueue_scripts
to call our function to enqueue the script at the correct time for the admin area$screen
object which WordPress creates for the admin area to signal what page is currently being viewed. It has a bunch of attributes on it. In our case, I’m checking$screen->id
for if it is'post'
or'page'
, and$screen->action
for if it is'add'
.get_template_directory_uri()
to get the URL to the theme folder. This way we can use our theme on any WordPress setup without breaking the URL.Using jQuery in Your Script
When you use jQuery in your scripts, you can’t use the
$
right off the bat as the version of jQuery included with WordPress disables the$
shorthand forjQuery
. This is because back in the day it is common for multiple Javascript libraries to use$
.You have two options in your Javascript to use jQuery.
The first is to use
jQuery
instead of$
:The second is to wrap the code of your script in a self-calling function. You’ll pass
jQuery
in as a parameter and then we will name the argument$
:Debugging Tips
make sure that your ‘myscript’ is printed out by viewing the page source
check that the URL printed is correct (it is easy to have a typo)
make sure the Javascript is not without errors
is another script also using the handle
myscript
creating a conflict