JQuery / PHP in WP plugin

I am trying to run this JQuery in a WP plugin...

<?php

function process_post(){

    wp_enqueue_script('namespaceformyscript', 'http://code.jquery.com/jquery-1.9.1.min.js', array('jquery'));

    ?>

    <script type="text/javascript" >
        jQuery(document).ready(function($) {

            alert('hello');
        });
    </script>

    <?php

}

add_action('init', 'process_post');


?>

This alert is just not happening...

What I'm I doing wrong?

Solutions

As for calling jQuery, this should be enough:

wp_enqueue_script('jquery');

This assures that even if multiple plugins will call jquery (and they likely will) - that it's not loaded multiple times.

Secondly, you're not using the right hook. From the docs about the init hook:

Runs after WordPress has finished loading but before any headers are sent. 

(http://codex.wordpress.org/Plugin_API/Action_Reference/init)

As a complete solution, try this (untested, should work though):

<?php


load_scripts() {
  wp_enqueue_script('jquery');

  wp_enqueue_script('yourcustomjavascript.js',
  WP_PLUGIN_URL.'/yourpluginfolder/yourcustomjavascript.js',
  array('jquery'));

} 

add_action('admin_enqueue_scripts', 'load_scripts');

By calling scripts like this, you've loaded jQuery and made sure it's not loaded multiple times (by your plugin and some other etc.) You've also made sure that jQuery is loaded before yourcustomjavascript.js as you told WP that it depends on jQuery.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Now...your jQuery code will work in both html templates, as well as yourcustomjavascript.js file which you can create.

Tags: Javascript / PHP / Jquery

Similar questions

TinyMCE plugin won't work with jQuery 3.5.1 (testing with "Test jQuery Updates")
since Wordpress seems to be moving towards a long due jQuery update, I began testing a few of my plugins (they're not published, just stuff I use for a few clients' websites) with the "Test jQuery Updates". Everything went smoothly until yesterday, when I ran into the following issue. I have this simple plugin that adds a few buttons to T...
jQuery conflict in WordPress Plugin - jQuery vs. WordPress Core
I have a custom developed WordPress plugin that is using jQuery 1.4 and for some reason it is conflicting with the core of the WordPress js code... not sure, but I think it's also jQuery, no? Anyway, I assumed it was this datepicker script I was using called "anytime.js" however, after debugging it turns out that the conflict was still happening af...
Jquery functions conflict with other plugin jquery function
I am using Same jquery function in two different WordPress plugins. Both Plugins working fine individually, the problem is if I activate both then the script is working for one plugin other one not working(2nd plugin not working). Can anybody give me a solution and tell me what the problem is?
JQuery Cookie Plugin not working - jQuery is not defined
I realize that there are a myriad of questions like this: bear with me. I'm not as good with JQuery as I am with HTML/CSS and I'm using cookies for the first time. I've got this website that has a green banner that is supposed to go away when the user clicks the 'X'. This is the broken JQuery that's driving me nuts: Essentially, I want to know why ...
Having a jquery error where my jquery loaded in the footer is affecting the functionality of a wordpress plugin
I have a website which is loading Maximage in the footer with a few other scripts. All these work well and have since I built my site, but a third party wordpress plugin from Axcelerate (an online student and courses api) comes up with an error, which I can only remedy by removing the jquery link at the end of the html page. The problem comes up wh...
Plugin jQuery conflicting with WordPress jquery
I have created a WordPress plugin but it's conflicting with the WordPress jquery. I have tried to remove my own jQuery and let it run on the WordPress jQuery but that did not work. I have also tried to deregister the WordPress jquery but without any luck. Any experts out there know the solution?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.