• Sorry for a silly question – but I am defeated.

    If I have a simple plugin myplugin.php, WordPress will run this file at some point, and will wp_enqueue_script and add_action etc.

    But when exactly does it run? Does it run every time WordPress wishes to load a new page? Is this before header/ body is rendered? If so, will $GLOBALS[‘post’]->post_name; be reliably set at this point?

    Is it run at any other time ever? (yes I know that functions within it are called by the framework).

    Thanks – I really can’t find an answer to this basic issue anywhere else!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    But when exactly does it run? Does it run every time WordPress wishes to load a new page? Is this before header/ body is rendered?

    When you add a filter or action it all goes onto a queue to be processed. You can set the $priority when you define the add_filter or add_action to determine when in the queue your code gets placed.

    http://codex.wordpress.org/Function_Reference/add_filter
    http://codex.wordpress.org/Function_Reference/add_action

    The default priority is 10.

    For example if you had a filter to modify the_content and you wanted it to run after the other filters which presumably run at the default priority of 10 then you would add the filter like so:

    add_filter( 'the_content' , 'mh_my_amazing_function' , 15 );

    See that 15? That queues the filter up after the default filters for the the_content. The 15 is arbitrary and any number greater than 10 would do the same thing. If you wanted to run before the default filters then you can try a number lower then 10 such as 5.

    The same applies to add_action() as well. Depending on the filter or action depends on when and where it gets run.

    Thread Starter drking

    (@drking)

    Thanks for the reply – but this is an even more basic question than you have answered I’m afraid. For example I have a file myplugin.php which contains (at the top, not in a function):
    // enqueue and localise scripts
    wp_enqueue_script( ‘my-ajax-handle’, plugin_dir_url( __FILE__ ) . ‘ajax.js’, array( ‘jquery’ ) );

    (and a whole lot of other stuff of course). It works – I can do clever things with Jquery. But at some point WordPress must open up the file myplugin.php, find the line wp_enqueue_script(…) and execute that. When does it do that?

    Almost certainly I have misunderstood some basic concept that everyone else knows…

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Basic plug-in question – when myplugin.php runs’ is closed to new replies.