Check the type of the queried object on the page and the post type:
function function_that_returns_js_string() {
$obj = get_queried_object();
$type = is_a($obj,'WP_Post');
if (true === $type && 'post' == $obj->post_type) {
echo 'your string';
}
}
add_action('wp_head', 'function_that_returns_js_string');
However, I am concerned that you are inserting a script directly into the head of the page rather than enqueueing it, which is more standard practice. The same trick will work to conditionally enqueue:
function function_that_returns_js_string() {
$obj = get_queried_object();
$type = is_a($obj,'WP_Post');
if (true === $type && 'post' == $obj->post_type) {
wp_enqueue_script('jcrop');
}
}
add_action('wp_enqueue_scripts', 'function_that_returns_js_string');
Related Posts:
- What does l10n.js do in WordPress 3.1? And how do I remove it?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- Move all the JS files to the bottom|footer, the right way
- Removing auto versioning of JS and loading to header
- Adding code to head to all pages manually
- how can i send this to wp_head – escape problem
- Put dynamic Javascript in header after doing operations
- How to get all place details from a Place Details Request using Google Places API Web Service?
- How do I check whether a checkbox is checked in jQuery?
- SyntaxError: Unexpected token o in JSON at position 1
- What does the function then() mean in JavaScript?
- Understanding unique keys for array children in React.js
- Heroku: “No default language could be detected for this app” error thrown for node app
- Loop inside React JSX
- Can’t resolve module (not found) in React.js
- Is there a difference between /\s/g and /\s+/g?
- How to read an external local JSON file in JavaScript?
- Setting a backgroundImage With React Inline Styles
- Filter array to have unique values
- How do I chop/slice/trim off last character in string using Javascript?
- What is causing this error – “Fatal error: Unable to find local grunt”
- Escape quotes in JavaScript
- “Uncaught (in promise) undefined” error when using with=location in Facebook Graph API query
- Format JavaScript date as yyyy-mm-dd
- Programmatically change the src of an img tag
- Unable to preventDefault inside passive event listener
- JavaScript variable assignments from tuples
- jQuery $.cookie is not a function
- Setting background-image using jQuery CSS property
- What is ESNext?
- SyntaxError: Cannot use import statement outside a module
- npm WARN … requires a peer of … but none is installed. You must install peer dependencies yourself
- How do I check if string contains substring?
- What does “export default” do in JSX?
- Cannot find module ‘internal/fs’ after upgrading to node 7
- How can I select an element by name with jQuery?
- How can I group data with an Angular filter?
- jQuery disable/enable submit button
- Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop
- node-request – Getting error “SSL23_GET_SERVER_HELLO:unknown protocol”
- TypeError: ‘undefined’ is not an object
- Uncaught TypeError: Cannot read property ‘appendChild’ of null
- javascript setTimeout() not working
- JQuery issue “TypeError: $.getJSON is not a function”
- Why is marquee deprecated and what is the best alternative?
- For loop for HTMLCollection elements
- Retrieve the position (X,Y) of an HTML element
- command not found: jest
- How to access Session variables and set them in javascript?
- Google Maps JS API v3 – Simple Multiple Marker Example
- HTML onchange (this.value)
- SyntaxError: Use of const in strict mode
- socket.io – ReferenceError: io is not defined
- What is the difference D3 datum vs. data?
- How to check if a number is between two values?
- How to determine if Javascript array contains an object with an attribute that equals a given value?
- Download a file by jQuery.Ajax
- What does “export default” do in JSX?
- Error: Couldn’t find preset “es2015” relative to directory “/Users/username”
- Using Angular 2 without TypeScript Transpiler
- How to intercept already localized scripts
- Customizer JS API
- Loading different JS for http and https
- How to get the ToggleControl Gutenberg component working for a PHP Block
- How to append a URL parameter to all outbound URLs
- Loading Javascript Only When Specific Content Existing in a Post?
- Javascript Helpers
- wp_register_script unable to link bootstrap JS
- Rest API invalid nonce with Backbone Client
- How do I change number of columns in a proper way in twentyfourteen child theme?
- Gutenberg block “This block appears to have been modified externally” on save
- How do I embed a Javascript form directly into a page?
- Using google graph api with wordpress
- Enqueue script if screen is greater than 500
- window.send_to_editor throws unterminated string literal error when adding image with multi line description
- Gutenberg get core data – search
- How to get the title of the item that is clicked on
- Correct way to include extra styles & script files?
- Correctly enqueue scripts of type=text/paperscript (PaperJs Library)
- useSelect second parameter
- How can I allow HTML in Gutenberg UI elements help text?
- How do I make my wordpress post appear in a mouse hover preview?
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- UnitControl – Block has encountered an error
- Should I manually resolve WP Core File security issues or await a subsequent WP release?
- jQuery UI Tools: Scrollable: More than 1 instance possible?
- imporing HTML+javascript from REST API
- Uncaught TypeError: switchEditors.switchto is not a function
- Check if user can in javascript
- Rest API and how to deal with it in server side
- Is it possible to override only a part of another plugin’s / theme’s js (asset) file?
- VisualComposer/WPBakery Page Editor: Is any JS event triggered after the Edition pop-in is shown?
- How do I integrate a codepen?
- How do I add my own scripts to the add_action(‘wp_enqueue_scripts’) function?
- Post title and url not showing on google map marker [closed]
- Including js script at end of header
- How to modify a class in load-scripts.js ?
- Trigger a function during onload
- Add Javascript/Html into a WordPress
- Google AdSense Ad not showing up in WordPress Blog [closed]