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
- Are there hooks for WordPress updates?
- Pass a php string to a javascript variable
- BuddyPress User Profile Menu
- Where to hook my plugin’s action
- Update post meta using pending_to_publish hook
- Pass info from functions.php to plugin
- What’s hook to use immediately after a user is authentcated [duplicate]
- Hook before posting via JSON REST API
- Problem in loading javascript in footer
- Help to change the text for new website notification (wpmu_welcome_notification)
- add_action for unknown custom post types
- Add parameter (time) to oembed
- Get log of function/method calls made by do_action() on WordPress initialization
- Plugin activation hook in an abstract class
- How to hook in after user’s registration email has been sent?
- How to find a callback attached to a bbpress hook?
- JQuery not working in my plugin [closed]
- Storing state between hook functions
- Remove rewrite rules generated by plugin during deactivation
- prettyPhoto media loading very slowly in Chrome
- How to prevent double execution of do_action statements
- Checking post format during xmlrpc_publish_post
- When is the {$new_status}_{$post->post_type} transition hook fired?
- How to hook into Add New Member in BuddyPress
- Event Calendar using insert_post_data after save_post and insert_post after post_transition draft_to_schedule
- How to update a meta field of type array in Gutenberg
- Change permalink structure hidden button edit
- How to load plugin after page is loaded – pagespeed issues
- Convert HTML Script to Shortcode [closed]
- Using wp_editor tinyMCE in metabox cause form alert on leaving page
- How to pull code snippet from functionality plugin?
- add mediaelement.js plugins into WordPress video player control bar
- Shortcode button dosent work for all posts. Work for first post only
- Get IDs of Images from Gallery Block in InnerBlocks of a Custom Gutenberg Block
- How to remove query string from current page URL?
- How to extend custom (non-core) blocks?
- Prevent a plugin from being automatically activated
- TinyMCE as comment editor – encoding issues
- Hook to change Logout url
- React JSX in WordPress Plugin Development
- Internationalization autocomplete JS variable
- Where (or when) should I register a new taxonomy?
- How do action and filter hooks understand where to look for the core function that we hooked our function to them
- Pulling data from custom plugin settings using PHP shortcode and Javascript
- How get a value from a plugin into another plugin through action/filter
- How to exclude a list of specific category in category widget in new post admin screen?
- How can I count post views of REST API calls and update them in an ACF field?
- Hooking into the init action will fire it too frequently?
- WordPress filter that hook after each action/filter hook
- When to use action hooks and plugins
- What’s the difference between “wp” and “wp_loaded”?
- Using custom JS plugins with WordPress?
- Method to find a hook
- Event-Driven Pattern vs MVC?
- Using hooks with extra parameters
- How to catch and modify custom field values when a page is updated
- Override Javascript in a Plugin?
- Genesis: How to add content after aside and before the content-sidebar wrap
- Understanding WP
- Plugins rewrite rules the right way
- Is it possible to change image urls by hooks?
- Hook for when a page template is changed
- Get post or page id early
- Hook (or other way) to find out when another plugin is activated / installed
- How to disable specific plugin in mobile version?
- Any hook for pre-plugin-update -either bulk or single plugin update
- Add custom message to the plugins page of WordPress
- WordPress capabilities and restricted categories access
- Gravity Forms plugin: How to use “gform_editor_js” action hook? [closed]
- new_to_publish fires multiple times
- Inserting above the comment template
- Insert new user with form submit ‘init’ hook
- Best approach to fetch data from wp options to js file or php file
- Implement Hooks Using Array
- What hook is used to display the admin_bar on the front end?
- How do I display only the latest post on my home page, while maintaining proper plugin hooks?
- Strange behaviour with add_{$meta_type}_metadata | add_post_metadata filter
- Hook when new CPT published AND postmeta inserted
- Plugin init hook
- Looking for a hook for post.php
- Can someone please tell me what is wrong with my plugin?
- Need to check is_archive during init
- Updating the Drag-To-Share eXtended share URLs?
- Hooking in to replace the Sidebar/Widget areas
- Scripts at the end of the page
- wordpress site | All-in-one-cufon IE Issue
- How to get all of the activate_plugin action parameters?
- Plugin admin panel JavaScript Broken
- Enqueue WordPress plugin scripts below all other JS
- How to add Internationalization in WordPress using Javascript/React?