You will need global $post
to get the $post
variable into the scope of that function.
function doraemon_scripts() {
if( is_single() ) {
global $post;
if (strpos($post->post_content,'[gallery') === false){
} else {
// wp_enqueue_style( ... );
// wp_enqueue_script( ... );
}
}
}
add_action( 'wp_enqueue_scripts', 'doraemon_scripts' );
Using the $post
global outside of the Loop is not always reliable, but WordPress will set that variable to the first post in the result set which will be the correct post on single post pages, but won’t be right on other pages.
Of course, that won’t work if something else has altered the global, and you can’t always predict what a plugin will do and where.
You might be safer using …
global $wp_query;
$post = $wp_query->posts[0];
Related Posts:
- How to enqueue CSS and JS only on specific template?
- using conditionals on enqueue styles
- Enqueue scripts not working with if is page conditional tag in functions.php
- How to add conditionals for IE browsers between certain versions?
- How to: Conditionally Enqueue JS and Stylesheets, for Custom Post Type (Single and Archive Templates)
- How do I Enqueue styles/scripts on Certain /wp-admin Pages?
- Where is the right place to register/enqueue scripts & styles
- Conditionally Loading JavaScript/CSS for Shortcodes
- How do I dequeue a parent theme’s CSS file?
- Check if a script/style was enqueued/registered
- wp enqueue style on specific page templates
- How to load css in the footer [duplicate]
- How can I get a list of all enqueued scripts and styles?
- Load CSS/Javascript in frontend conditionally if block is used
- Prevent Version URL Parameter (?ver=X.X.X) on Enqueued Styles & Scripts
- How to load Widget javascript + css files only if used?
- How to load scripts/styles specific for a page
- How can I de-register ALL styles all at once? And same with Javascript?
- How to add stylesheets only to pages with specific shortcode?
- Creating a photo gallery in WordPress [closed]
- How can I alter the [gallery] markup?
- Enqueue custom font file with rel=”preload”
- How do I force wp_enqueue_scripts to load at the END of ?
- Custom wordpress gallery option
- Make a really simple gallery structure
- Best approach when modifying the Media Manager
- Load js/css files only on specific admin UI pages
- Get list of scripts / styles and show file which enqueued them
- Gallery Inside One Post
- How do i add class=”fancybox” to the default gallery?
- How to show description under an inserted image?
- Adding jquery and thickbox to WordPress theme
- Why I can’t add a CSS style in this WordPress theme?
- Removing specific style from wp_head
- How to dequeue / deregister any theme styles and scripts
- Modifying the media-template.php file, the right way?
- deregister scripts on certain page
- The difference between calling wp_enqueue_scripts to load scripts and styles in custom theme
- Notice that the wp_enqueue_style is not being called correctly!
- Responsive Images
- First three images in post excerpt
- Hyphens vs. periods in the script slug in wp_register_script?
- How can I dequeue a Plugin Stylesheet?
- Use wp_enqueue_scripts, not wp_print_styles?
- How do I conditionally enqueue script for CPT single post type with plugin?
- how do i create a downloadable gallery with image descriptions?
- Get the list of enqueued/registered scripts for a specific post?
- Add `datetimepicker` to form
- Set default link type to “file” for image galleries
- Add option to “Gallery Settings” section
- What can I hook into after_setup_theme?
- How I can get image description/title/alt for gallery image?
- Trying to load different syles for 404.php page
- What determines the order of enqueued styles?
- $wp_styles->registered not giving ALL the styles?
- Get info (url) from already enqueued styles
- Dynamically Load Styles and Scripts from Theme Functions.php
- is_front_page not working in functions.php
- WP_Editor – Remove TinyMCE Toolbars
- Images Missing from built in WP Gallery
- How to combine multiple CSS files and concatenate JavaScripts if WordPress recommends enqueuing them?
- How to use wp_enqueue_style() and wp_enqueue_script() only when needed?
- How can I remove or change a border/outline on a picture when it is in a gallery page?
- Preload key requests using wp_enqueue
- How do I load custom scripts and styles for a page?
- conditional shortcode not working
- Add size in Gallery Settings in Media Library
- Post photo galleries [closed]
- Remove a script from a template file using wp_dequeue_script
- Define size for `get_post_gallery_images`, they seem to have been resized to 150×150
- Proper Way to Remove Thumbnail Links In Gallery
- Problem using is_single() to enqueue script from functions.php
- Remove specific CSS and JS from the head
- Media gallery – inserting full size images without link
- Dequeue Scripts and Style for Mobile not working?
- Disable Linked Gallery Images If Mobile Browser
- Possible to add same caption to all photos in a gallery?
- Enqueue less file not working in child theme?
- How to dequeue / deregister parent theme style
- How to Add a Custom Colum on Thickbox Media Gallery Tab?
- Why does wp_enqueue_script ignore my ‘wp_head’ hook?
- WordPress 3.5 Gallery Menu Order not set?
- Getting the image title/alt attribute from the gallery shortcode
- How to get post attachments in gallery post format template
- Can I attach an image to a different post?
- Enqueued scripts and styles loading in WordPress Dashboard as well
- conditional shortcode not working
- NextGen – Display Image Count Per Gallery
- Add a checkbox in Media Upload window
- Enqueue styles properly in a child theme and stylesheets location
- have WP Gallery display the title instead of caption
- Changing Gallery images size?
- How do I use two different images, one as a thumbnail and one as a linked image?
- iris color picker in widget – click event doesn’t fire when initially when widget is first created
- remove featured image from gallery woocommerce [closed]
- Add last modified time as version to css and js
- Loading scripts & styles from a meta box callback function
- Enqueue script o style only if a template part is loaded
- How to get ID of images used in gallery?
- Enqueued Stylesheets Effecting Admin Styles