WordPress allowed me to get a better job & better projects in just a Year. I will give you tips, tricks and guide you into WordPress development so you can get more income. Sign up to my 7-day email course!
Did you ever want to show an article inside another article with a modal? Maybe something like a short introduction or a related article? In this tutorial we will create a new WordPress plugin that will create a modal window and show an article by clicking on a button inside another article.
Before we begin into creating the functionality we need to prepare our plugin. Firs, we will create a new folder inside the wp-content/plugin. You may call it as you want.
After that, create a file inside that folder with the same name but with the extension .php. Put this code inside that file:
Save the file and go to the WordPress administration and under the menu Plugins activate the newly added plugin. Now the plugin is active and we can start entering code:)
We will use Bootstrap 3 to show the modal. In case your theme does not use Bootstrap 3, we can easily add some styling using the default Bootstrap 3 style or another Bootstrap 3 themed style.
Add the next code to your file:
If we want to use the Bootstrap 3 Modal we need to add that modal into our page. The HTML needed for the Modal will be added on the bottom of the page where he function wp_footer is being invoked. We will use the WordPress filter wp_footer. So let’s add this code in our plugin file:
We have defined a regular function that only holds the HTML code for the Bootstrap 3 Modal and then we have added it in the WordPress filter wp_footer.
In this plugin, we will create a small shortcode that will show a button in the articles. This shortcode will create a hyperlink that will have Bootstrap 3 classes and also additional attributes that can be used to change the appearance of he button.
We have registered this shortcode under the name ibenic_post but you can name it as you want it. Just be sure to replace other parts of the code where ibenic_post will be used.
In this function we are passing the parameter ID that is the ID of our article. Then we are creating an AJAX call with the jQuery. In this AJAX call we are passing the next parameters:
If the AJAX call was a success and if we got the requested information, we will insert that content in the Modal parts such as the modal title and body. If the AJAX was a success but we did not get any article information (maybe because we provided an ID which is not an article) we will alert the user with the same Modal but with a different message. We are reading the JSON response and by validating it we can show an error message or the article content.
At the beginning we are adding our function to WordPress actions. They work like this:
add_action( ‘wp_ajax_our_action_name, ‘server_function’);.
Since we are passing the action name ibenic_show_post then we need to add our server function to our actions. One action also has the word nopriv and that means that our function will also be performed even if the user is not logged in.
We are then checking our security that we have passed in the AJAX call. After that we are getting the ID from the AJAX call and then we use the WordPress function get_post for getting the content of a post with the provided ID. If there is no article with the provided ID, the variable $post will be null. If the content is found, we are creating an array with the title and the content and then we are sending it as a JSON response. However, if there is no article found, we are also sending a JSON object but this time with an error response.
At the end we are also calling the wp_die() so that the AJAX request and reponse will end after our function is finished processing.
By default, shortcodes cannot be processed in widgets. But by adding this code we will enable that also:
Here are some examples how our newly added shortcode can be used:
Ony with an ID
ID of the article with some other Bootstrap 3 classes
[ibenic_post id=3692 class="btn btn-warning"]
ID of the article and additional styles
[ibenic_post id=3692 style="margin:0 auto;width:100%"]
Button with a different text
[ibenic_post id=3692 text="Click to show the article"]
You can use this shortcode even with every attribute we have defined in he shortcode function.
You also need to know that he content we are showing is not passed through the WordPress filter the_content so any other shortcode or similar will not be shown as the regular output of that shortcode.
By using this modal we can easily show any content. If we wanted to extend the functionality of this plugin we could add different post types, attachments and any other content. We could also create a regular WordPress Widget to show the modal button inside the sidebar or any other area where we can put widgets.
Have you ever created something similar or needed something like that? Let us know about your experience using Bootstrap, Modal or any pop-up related story in the comments below! 🙂
Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.