The add_thickbox()
function in WordPress PHP is used to enqueue the default ThickBox JavaScript and CSS files. If you need to modify the settings, you can do so using another JavaScript file, similar to media-upload.js.
Usage
To use add_thickbox()
, simply call it in your WordPress PHP code. However, it’s important to note that it should be used with a hook to prevent PHP warnings if debug mode is on. Here’s an example:
function enqueue_thickbox() { add_thickbox(); } add_action('wp_enqueue_scripts', 'enqueue_thickbox');
Parameters
This function does not require any parameters.
More information
See WordPress Developer Resources: add_thickbox()
Remember, the use of add_thickbox()
should be hooked into wp_enqueue_scripts
, admin_enqueue_scripts
, or login_enqueue_scripts
to avoid errors and warnings when WordPress debug mode is on.
Examples
Basic Usage
// Define the function to enqueue ThickBox function enqueue_thickbox() { add_thickbox(); } // Add the function to the wp_enqueue_scripts hook add_action('wp_enqueue_scripts', 'enqueue_thickbox');
This code simply adds the ThickBox JavaScript and CSS to your WordPress site by calling add_thickbox()
within the enqueue_thickbox
function, which is then hooked into wp_enqueue_scripts
.
Adding ThickBox to Admin Area
// Define the function to enqueue ThickBox in admin area function admin_enqueue_thickbox() { add_thickbox(); } // Add the function to the admin_enqueue_scripts hook add_action('admin_enqueue_scripts', 'admin_enqueue_thickbox');
This example enqueues ThickBox in the admin area of your WordPress site.
Adding ThickBox to Login Screen
// Define the function to enqueue ThickBox in login screen function login_enqueue_thickbox() { add_thickbox(); } // Add the function to the login_enqueue_scripts hook add_action('login_enqueue_scripts', 'login_enqueue_thickbox');
In this example, we’re adding ThickBox to the login screen of your WordPress site.
Conditional Enqueue
// Define the function to conditionally enqueue ThickBox function conditional_enqueue_thickbox() { if (is_page('gallery')) { add_thickbox(); } } // Add the function to the wp_enqueue_scripts hook add_action('wp_enqueue_scripts', 'conditional_enqueue_thickbox');
This code will enqueue ThickBox only on a page with slug ‘gallery’.
Add ThickBox to a Specific Post Type
// Define the function to enqueue ThickBox in specific post type function cpt_enqueue_thickbox($hook) { global $post; if ($hook == 'post-new.php' || $hook == 'post.php') { if ('my_post_type' === $post->post_type) { add_thickbox(); } } } // Add the function to the admin_enqueue_scripts hook add_action('admin_enqueue_scripts', 'cpt_enqueue_thickbox');
This example enqueues ThickBox in the admin area but only for a specific custom post type ‘my_post_type’.