And here’s a jQuery solution (as Mike suggested in his first comment).
function add_my_scripts() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'my_init_script', SCRIPTSRC, 'jquery', '1.0' );
}
add_action( 'init', 'add_my_scripts' );
Then add a script to your plug-in that does this:
jQuery.noConflict();
jQuery(document).ready(function($) {
init();
});
This will start jQuery in no conflict mode (if it isn’t already) and add a call to the init()
method when the document is ready. It’s a safer method to use than body onready()
because the onready()
function can only call one thing … so no one else can hook anything to that or add custom script. It’s better to make your plug-in as unobtrusive as possible so other plug-ins don’t interfere or vice-versa.
Related Posts:
- Are there any hooks that alter the 404 logic?
- How to include code only on specific pages?
- Why doesn’t wp_enqueue_script() work when including a JavaScript file with TEMPLATEPATH?
- Is there an earlier hook than login_head or login_enqueue_scripts?
- Calculate price and display on woocommerce product single page under price (simple price, variable price) [closed]
- Execute JavaScript in WordPress Hook
- Deactivate JS Script in Plugin Shortcode
- How do I add some javascript validation to the admin interface form’s onsubmit?
- How to use a WooCommerce action hook?
- front end editor creation for Restropress plug in – displaying information from a WP admin area, on a different URL
- How to extend WordPress Bookly plugin
- Changing the “Plugin Activated” Message Default
- force enqueue script to be first in order of prominence
- Modifying a JS file with data from plugin settings
- Is there a WordPress core & plugins update action hook?
- How to save the values of checkbox to the register setting?
- Hook into wp_head(); in a plugin
- Use $query->set multiple times with pre_get_posts?
- Enqueue style inside shortcode but its loaded at the bottom of page (before footer scripts)
- How can I hook into the post editor title field in order to change the HTML?
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- How to add extra attributes to the script tag added via wp_localize_script()
- Searching hook to set cookies before and avoid “headers already sent”
- Hide WordPress Plugin Deactivation Links
- Hook before inserting user into database [duplicate]
- Does the ‘nav_menu_css_class’ filter accept 2 or 3 arguments?
- how would I include a js file with tags into wordpress?
- do_action and hook methods
- the_posts hook, which set of posts?
- Is it possible to add an action to the currently running action?
- Google Maps and Yandex Maps doesn’t work correctly
- Prevent CSS/JS loading when plugin is not used
- wp_redirect() not working on form submission with init hook
- OOP: Display warning and deactivate the plugin if PHP version is less than 5.4
- Creating an Exit Confirmation Popup
- How to prevent action in ajax request, when in admin side?
- Easy Digital Downloads (EDD) – Purchase Receipt emails couldn’t be logged
- Reposition Woocommerce Message
- Using wp_editor tinyMCE in metabox cause form alert on leaving page
- Prevent a plugin from being automatically activated
- How get a value from a plugin into another plugin through action/filter
- Any hook for pre-plugin-update -either bulk or single plugin update
- Need to check is_archive during init
- When WP_CLI runs WordPress from the command line, are hooks called?
- Modifying the comments section through a plugin regardless of theme
- wp_get_post_terms is difference obj model than the one in rest api response
- Trying to run a compile command while saving post
- Get Time Taken By Each Action Hook in WordPress
- Best place for short bio,image and button [closed]
- The Hook registration_errors was not called
- Is there a loading priority for login_enqueue_scripts?
- Delay JavaScript files from loading
- I don’t understand how add_action and do_action work in tandem. The former executes the code already…what is do_action for?
- remove_action not removing add_action from constructor
- Hook Adding content after title
- Fatal error: Call to a member function get_queried_object_id()
- “admin_enqueue_scripts” hook or $_GET[‘page’]?
- Pass user role to javascript code inside body
- How to add content to suscriptor dashboard frontend?
- Access post title from custom meta box on title change
- Action hook save_post for newly created posts – $post object data is all empty
- add_filter to the_content from plugin function
- Can not using get_header() cause trouble (later on)?
- Dropdown menu on click change
- How to set Media Box form elements default value?
- Hooks to run after a core upgrade?
- How do I override all stylesheets and scripts without a plugin
- Send Array from JS to php file then insert it in wp database through a plugin
- How to override Plugin javascript function : $(‘body’).on(‘change’,’.class’)
- Connect / Login WordPress Hosted Website or Blog with NodeJs
- How to fetch WordPress database values in JavaScript widget
- Possible way of having multiple Gutenberg instances for a page?
- Woocommerce add products to cart
- Show popup on click
- Is there no concise way, a library maybe, to help with unhooking class functions and so on?
- Debug a WP install: how to find which functions write post updates during a process (a woocommerce checkout in my case)
- Woocommerce – Change variations output
- Problem with call_user_func_array front end
- WordPress ajax error 400 bad request for sending data to remote site [duplicate]
- Stylesheet is getting removed/deregistered automatically
- How to override get_avatar() function?
- Customizer preview URL not updating when settings reset to default
- WordPress Boilerplate Plugin doesn’t see callback functions for add_settings_field and add_settings_section
- What is the correct entry point for capturing coverage data
- wp_enqueue_scrpts seems not to get called
- Change user role if it’s orders count more than
- How to create a dashboard for logged-in users with custom functionality? [closed]
- Creating a user ‘add custom field’ section
- Javascript not working
- How wordpress plugin hooks works? [duplicate]
- Post curl function save twice using plugin hook function wordpress
- WordPress shortcode plugin not working
- FacetWP: Plugin breaks button/modal functionality inside searchable content area
- TypeErrors with Yoast, jQuery in conflict?
- Count div with class (.length) only shows 0 [closed]
- Hooks not working on live server
- Callback API for getting response from fygaro payment gateway
- Can’t use updated variables in handle function
- Changing login url
- Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only