3

I am working on this project and I am trying to execute a jquery or javascript just before saving the post, I have tried the following:

 function do_something_with_a_post($id) {

 <script>alert('hello');</script>
 }
 add_action('pre_post_update', 'do_something_with_a_post');

but it gives a php error... I am really unsure on how to do this.

Does anyone has an idea? Thanks!

5
  • 1
    You can't place javascript inside a php function and expect it to run. Javascript happens in browser, and should be triggered by some user-related event (think clicks, browsing, scrolling, etc). Php happens server side. You need to use php in that function.
    – tao
    Feb 3, 2015 at 11:19
  • Did you see wp_enqueue_scripts codex.wordpress.org/Function_Reference/wp_enqueue_script Feb 3, 2015 at 14:58
  • @AndreiGheorghiu You could place javascript in a PHP function and expect it to run such as admin_head or the likes, it just needs to be escaped properly with ending PHP tags ?> <script> alert( 'here' ); </script> <?php ( or even echoed out really ). That being said, since this hook does run behind the scenes you would never see the alert.
    – Howdy_McGee
    Feb 3, 2015 at 16:27
  • @Howdy_McGee Technically you're right. But why would anyone want to run javascript during php execution and not inside a webpage? Can you give me a useful example?
    – tao
    Feb 3, 2015 at 18:23
  • @AndreiGheorghiu You're right, during a hook like save_post the JS is useless because it's being run behind the scenes, but JS could useful in other PHP functions such as admin_head or admin_footer. You may want to do this just for testing purposes ( versus creating a separate script file then enqueueing it ) or maybe it's such a small piece of code that it doesn't necessarily need it's own script ( initializing a datepicker to a css class ). I'm not suggesting it's a good practice to get into I just wanted correct the statement that it can't be done inside a PHP function.
    – Howdy_McGee
    Feb 3, 2015 at 18:48

1 Answer 1

3

The Javascript you have will never run because WordPress is running this in the background as purely PHP, there is HTML to associate with your Javascript. If we look at the documentation on the pre_post_update hook it states:

Called just before $wpdb->update()

Which is run purely in PHP. If it's easier to see what I mean, here's the full code on Trac, if you scroll the top top you'll notice there's no HTML Document associated to it. I believe what you need is what @Brad Dalton suggested and to create your own script and enqueue it on your post-type ( which if dealing with WooCommerce would be product ).

For example, we can add this to our functions.php file:

/** Admin Enqueue **/
function admin_queue( $hook ) {
    global $post; 

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'product' === $post->post_type ) { 
            wp_enqueue_script( 'custom-title-here', get_bloginfo( 'template_directory' ) . '/scripts/custom-script.js', 'jquery', '', true );
        }
    }
}
add_action( 'admin_enqueue_scripts', 'admin_queue' );

For more information on how to use wp_enqueue_script, View Codex. Depending on what you're trying to do you could run your JS on the submission of the form:

jQuery( document ).ready( function( $ ) {
    $( '#post' ).submit( function( e ) {
        alert( 'Stopping Form From Submitting.' );
        return false;
    } );
} );
2
  • 2
    Maybe this is new, but at least WP 4.6.1 declares the form with submit as its name attribute, making this solution fails with message theForm.submit is not a function (see stackoverflow.com/questions/833032/…). A workaround is to use on. For example: $(document).on('submit', postForm, function() {...});
    – philippe_b
    Sep 16, 2016 at 14:15
  • @philippe_b Thanks, it looks like there was an error in the initial script, I've tested it and fixed it for 4.6.1
    – Howdy_McGee
    Sep 16, 2016 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.