- Navigate to the WordPress plugin directory of your development installation.
- Navigate to the ch9-pop-up-dialog directory and then edit ch9-pop-up-dialog.php.
- Find the ch9pud_load_scripts function and add the following highlighted lines of code:
function ch9pud_load_scripts() {
// Only load scripts if variable is set to true
global $load_scripts;
if ( $load_scripts ) {
wp_enqueue_script( 'jquery' );
add_thickbox()
}
}
- Locate the ch9pud_footer_code function and modify the code, adding the following highlighted lines of code to the function body:
function ch9pud_footer_code() {
// Only load scripts if keyword is found on page
global $load_scripts;
if ( $load_scripts ) { ?>
<script type="text/javascript">
jQuery( document ).ready( function() {
setTimeout(
function(){
tb_show( 'Pop-Up Message',
'<?php echo plugins_url(
'content.html?width=420&height=220',
__FILE__ ); ?>', null );
}, 2000 );
});
</script>
<?php }
}
- Add the following line of code to register a function that will filter post and page contents before any other parsing and formatting is performed:
add_filter( 'the_posts',
'ch9pud_conditionally_add_scripts_and_styles' );
- Append the following block of code to provide an implementation for the
ch9pud_conditionally_add_scripts_and_styles function:
function ch9pud_conditionally_add_scripts_and_styles( $posts ) {
// Exit function immediately if no posts are present
if ( empty( $posts ) ) {
return $posts;
}
// Global variable to indicate if scripts should be loaded
global $load_scripts;
$load_scripts = false;
// Cycle through posts and set flag true if
// keyword is found
foreach ( $posts as $post ) {
$shortcode_pos = stripos( $post->post_content,
'[popup]', 0 );
if ( $shortcode_pos !== false ) {
$load_scripts = true;
return $posts;
}
}
// Return posts array unchanged
return $posts;
}
- Insert the following function call to declare a new shortcode along with a function responsible for replacing it with content:
add_shortcode( 'popup', 'ch9pud_popup_shortcode' );
- Add the following code block to provide a simple implementation for the ch9pud_popup_shortcode function:
function ch9pud_popup_shortcode() {
return;
}
- Save and close the plugin file.
- Visit the website's front page and you will notice that the pop-up dialog is no longer displayed.
- Create a new page and insert the [popup] shortcode in the page contents.
- View the new page to see that the new pop-up dialog appears, while the [popup] shortcode is not shown.