• Resolved Derda

    (@derda)


    Hi, I just started with Snippets and wrote this in a php:

    echo ‘<script type=”text/javascript”>’;
    echo ‘alert(“JS alert box in PHP”)’;
    echo ‘<script>’;

    This should get the alert box to appear in any creation or opening of a post or page, should it not? – However nothing happens.

    What am I doing wrong?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    If you want an action to occur when something happens, such as opening a post or page, you’ll need to use an action or filter hook. For example:

    add_action( 'wp_head', function () {
        echo '<script type="text/javascript">';
        echo 'alert("JS alert box in PHP")';
        echo '</script>';
    } );
    
    Thread Starter Derda

    (@derda)

    Ohh, thanks for advising me. In the sample snippets provided I did not see that. Where can i learn which action or filter hook I need to supply and how this works?

    Plugin Author Shea Bunge

    (@bungeshea)

    Thread Starter Derda

    (@derda)

    Thank you very much for your kind help!

    Thread Starter Derda

    (@derda)

    With all your kind help I still can not get this snippet to work. It should display an alert when i am in edit mode in a CPT called “city” – Does someone have an idea where i made a mistake? – Thank you very much from this newbie.

    // Get the current CPT.
    $current_post_type = get_post_type();
    // Check if the current CPT is the “city” post type.
    if ( $current_post_type == ‘city’ ) {
    // Add the code to the add_action action.
    add_action( ‘edit_post’, function() {
    // Display a custom alert.
    echo ‘<script type=”text/javascript”>’;
    echo ‘alert(“JS alert box inside PHP”)’;
    echo ‘</script>’;
    } );
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Snippets not working’ is closed to new replies.