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)
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to load css in the footer [duplicate]
- How to add stylesheets only to pages with specific shortcode?
- Custom wordpress gallery option
- Modifying the media-template.php file, the right way?
- Responsive Images
- Add `datetimepicker` to form
- Set default link type to “file” for image galleries
- What can I hook into after_setup_theme?
- $wp_styles->registered not giving ALL the styles?
- Dynamically Load Styles and Scripts from Theme Functions.php
- How do I load custom scripts and styles for a page?
- Problem using is_single() to enqueue script from functions.php
- Media gallery – inserting full size images without link
- Getting the image title/alt attribute from the gallery shortcode
- Can I attach an image to a different post?
- Enqueued scripts and styles loading in WordPress Dashboard as well
- conditional shortcode not working
- Enqueue styles properly in a child theme and stylesheets location
- iris color picker in widget – click event doesn’t fire when initially when widget is first created
- Enqueued Stylesheets Effecting Admin Styles
- How can I load script and style in specific page in the back-end?
- How to determine if a post has attached images?
- How can I make wp default gallery responsive?
- How to get clean code for a gallery?
- Attach custom gallery to a page
- How To Put Multiple Image Galleries In A Single Post?
- Add new images to top of existing gallery not bottom
- Using multiple versions of jQuery while still calling it like WP likes
- Enqueue Style for a page/pages only
- Is my approach to enqueue styles inefficient?
- Deregistering a script in WordPress seems impossible
- Enqueue never runs
- Large Media Library
- Enqueue script/style if certain widget has certain value
- How to force that styles are enqueued in the header?
- Can I nest a element within tags from default wordpress?
- Strange issue with gallery when displays 3 columns
- enqueue styles for only mobile wp
- Where is the right place to register/enqueue scripts & styles
- Custom links or pages from Media Gallery
- My “Description” in gallery doesn’t accept formatting/html
- How to show private flickr group on a WordPress site?
- Is it possible to import NextGen Galleries into WordPress Galleries and convert embeds in all posts? [closed]
- Gallery Shortcode – only show first x thumbnails
- Image Gallery links to medium image instead of FULL size image
- Load same stylesheet for front end and wp-admin
- How to make universal gallery and improve admin side of managing galleries?
- How to remove all enqueued assets from the active theme?
- Should we explicitely enqueue jQuery and our theme’s main stylesheet?
- Enqueuing scripts and styles in custom plugins
- Where is image gallery information saved?
- Display number of latest images from wordpress gallery in your homepage
- Loading Scripts For Specific Template
- All of my Scripts and Enques are getting file not found
- How to check via conditional tags for a single plugin page?
- Change default values of Gallery Block settings
- WordPress WebSite is jumps while scrolling bottom [closed]
- enqueing Javascript and CSS
- Theme JS is available but theme CSS isn’t
- Including CSS files in HEAD based on page content
- How to retrieve media images urls in different sizes?
- Including Styles and JS files to work ON my plugin interface
- FullCalendar in WordPress is not working
- WP Gallery showing captions twice
- How do you load js and style resources from CDN with local fallback using wp_enqueue_scripts?
- How can we create a wallpapers gallery with different resolution?
- gallery shortcode exclude not working
- Slideshow with thumbnails
- Ipad app: Need uploaded images to go into “Gallery” as well as “Media Library”
- Can I create a NextGen gallery from a WordPress gallery
- Making the caption & click through link more visible on a gallery photo
- Find out the reason that the Styles and scripts in the theme are loading properly in localhost but Not in server after deploying
- Enqueue assets from multiple directories using add_action/do_action
- Call a single function on two different methods with hooks
- How do I ensure I can loop through every enqueued script and CSS?
- Enqueue script with version number not working
- how do i can make a preview of image gallery
- I need to ‘wp_dequeue_script’ and ‘styles’ and ADD a bunch of other css and js
- Page WYSWIG only allowing slides
- How to refresh the Gallery images?
- Most optimal way of starting fresh, without using other theme or dropping old files?
- next_image_link Displaying All Images Attac
- the_content() won’t load galleries on homepage
- How to display images that was uploaded in the media gallery and not by the current post?
- How to show the image caption on custom post type …?
- admin_print_styles incuding $_SERVER[‘DOCUMENT_ROOT’]
- Multiple Galleries in WordPress 3.3.1
- How to create a custom image gallery in the footer?
- How to only Load scripts on variable products page
- Looking to implement a scrolling gallery in my theme
- Hover/Zoom on Lightbox gallery
- Need help making Gallery slideshow with lightbox work correctly
- How to upload multiple images per single post and display them in a gallery?
- Should I use wp_register_style(), wp_enqueue_style, or both?