How to Add Popup in WordPress Without Plugin
Creating a popup in WordPress without a plugin can seem daunting, but with the right code and a bit of know-how, it's entirely achievable. By manually adding a popup, you maintain complete control over its design and functionality without relying on a third-party plugin.
Popups are excellent tools for engaging users, promoting opt-ins, and increasing conversions. Here's a step-by-step guide to help you add a popup functionality to your WordPress site without using additional plugins.
Understanding the Popup Structure
Before diving into the code, let's familiarize ourselves with the basic structure of a popup. A popup typically consists of a trigger (what initiates the popup), content (the information displayed in the popup), and a close mechanism (how users can dismiss the popup).
To create a popup in WordPress, we'll be using a combination of PHP, HTML, and CSS. We'll also need to use the WordPress' add_action function to add the popup on the desired page.
Creating the Popup Content and Styling
First, you need to create the HTML and CSS for your popup. Here's a simple example:
<div id="popup">
<h2>Welcome to My Site!</h2>
<p>This is a popup window.</p>
<a href="#" id="close-popup">Close</a>
</div>
Add this CSS to style your popup:
#popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 999;
}
#popup h2 {
color: #fff;
}
#close-popup {
display: block;
margin-top: 10px;
text-decoration: none;
color: #fff;
}
Adding the Popup Script
Once you've created the content and styling, you'll need to add some JavaScript to show and hide the popup. Here's a simple example using jQuery:
jQuery(document).ready(function($) {
$('a[href="#pop-up"]').click(function() {
$('#popup').show();
});
$('#close-popup').click(function() {
$('#popup').hide();
});
});
In this example, clicking an anchor tag with the href "#pop-up" will display the popup. The close button (with the id "close-popup") will hide the popup again.
Adding the Popup to Your WordPress Site
Now that you have the popup ready, you need to add it to your WordPress site. You'll need to use the add_action function with the 'wp_footer' hook:
The PHP code to add this popup would look something like this:
add_action('wp_footer', 'add_popup');
function add_popup() {
echo '<a href="#pop-up" style="display:none;">Show Popup</a>';
get_template_part('templates/popup');
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>';
echo '<script>(function($) { hourly_button(); })(jQuery);</script>';
}
In this example, the popup is added to the footer of every page with the CSS styling set to 'display: none;'. When the 'Show Popup' anchor is clicked, the popup will be displayed.
Adding the Popup Template
The 'get_template_part' function is used to include a template file named "popup". Create this template file in your theme folder under 'templates/popup.php' and copy the HTML, CSS, and JavaScript code from earlier into this file.
Remember to adjust the code and styles to match your website's design. You can also add form functionality, animation, or more advanced features with additional scripts.
Adding a popup to your WordPress site without a plugin requires some coding knowledge, but with the structured approach outlined above, it's well within your reach. Enjoy the freedom of creating tailored, unique popup experiences for your users!