WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add JS when post is published (3 posts)

  1. matoma16
    Member
    Posted 1 year ago #

    Hi,
    I am trying to add javascript when a post has been published so that it's more noticable for my user but I can't figure out what to use.
    I tryed with publish_post and save_post, but it doesn't work unless I put die; at the end of the code.
    I've tryed so hard to stay out of the wordpress core code, and I really don't want to have to because of this problem...
    here's my code :

    function run_when_post_published($post_ID){
    			echo '<script type="text/javascript">
    				alert("essai");
    			</script>';
    }
    add_action('publish_slider', 'run_when_post_published');

    Thanks for your help !

  2. markparolisi
    Member
    Posted 1 year ago #

    You should probably use admin_notices instead of throwing a JS alert.

  3. matoma16
    Member
    Posted 1 year ago #

    Hi, thanks for your answer, I just looked at admin_notice.
    Can you please explain how I would go about showing it only when the post is published / updated ? Should I use the $_GET['message'] in the URL ?

    I need to add JS when the post is published to add things (message desapearing after 3 sec for example) and didn't know how to add HTML without JS (before your answer). The alert in my code above is just the easiest way for me to see if a script is correctly linked or read :)

Topic Closed

This topic has been closed to new replies.

About this Topic