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?
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>’;
} );
}