• Resolved Insomnia88

    (@insomnia88)


    I have an add_action of a plugin currently running with the priority of 10.
    Inside this action I want to add a <script type="application/ld+json"> tag to the head of the site but I don’t know how to do it.
    I tried it with using another add_action, with different positions and priorities, anonymous functions but nothing worked so far except with a hacky way that looks like this

    wp_register_script('custom-script', '');
    wp_enqueue_script('custom-script');
    wp_add_inline_script('custom-script', '</script><script type="application/ld+json">'.$code.'</script><script>');
    • This topic was modified 4 years, 5 months ago by Insomnia88.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    If I’m right in understanding the question, you have a script tag that you’re trying to paste into the header of the site? If that’s right and that is the entire tag in your question (though looks like it’s missing a closing </script> tag and some content…) but you can do this with an anonymous function.

    Normally you’d want to use wp_enqueue_script but not sure you can change the type to application/ld+json and also not sure what is going in here in terms of content.

    Otherwise you can just do:

    add_action( 'wp_head' , function() {
        echo '<script type="application/ld+json"></script>';
    }, 10 );

    Cheers,

    Thread Starter Insomnia88

    (@insomnia88)

    Thanks for the reply.

    As I stated I tried an anonymous function before and it doesn’t work.
    The script I posted is missing an opening and closing tag because it’s a hacky/dirty way but it works.
    wp_add_inline_script simply wraps your code inside a <script type="text/javascript"> tag. That’s why I close it at the beginning and open it afterwards. So the rendered code looks like this.

    <script type="text/javascript"></script>
    <script type="application/ld+json">my_code</script>
    <script type="text/javascript"></script>
    • This reply was modified 4 years, 5 months ago by Insomnia88. Reason: typo

    OK I think I had perhaps already started reading and writing a response before the last edit you made to your question there.

    Not sure I’m totally clear on what it is you want to do here, is that code output what you want to achieve?

    When you say using the anonymous function doesn’t work, doesn’t work in what way? There’s no reason why that shouldn’t just output anything from that function straight into the <head></head> section of the page.

    Thread Starter Insomnia88

    (@insomnia88)

    My goal is to place a <script type="application/ld+json"></script> with my json code in the head of a site.
    When I say the anonymous function doesn’t work it means that there is no output in the head. In this case it’s not even rendered anywhere else.
    I guess the special circumstance is that I am already in an (anonymous) function of an add_action hook and this function returns something that is mandatory.
    The complete segment looks like this

    add_action( 'some_action', function( $content, $args ){
    	if ($some_condition) {
    		add_action( 'wp_head' , function() {
    		    echo '<script type="application/ld+json">{"foo":"bar"}</script>';
    		}, 10 );
    	}
    	return $content;
    }, 10, 2);

    The first add_action works as it should.

    • This reply was modified 4 years, 5 months ago by Insomnia88. Reason: typo
    • This reply was modified 4 years, 5 months ago by Insomnia88. Reason: further explaining

    OK, that’s interesting, any reason for trying to wrap the wp_head action in a function attached to another action?

    Depending on what action that is, it may be why it’s not working, since if that action fires after wp_head, then wp_head will already have fired without your function hooked to it.

    I just tried this using the init hook with a wp_head anon function inside and it worked OK, so in your case this would be:

    add_action( 'init', function( $content, $args ){
    	if ($some_condition) {
    		add_action( 'wp_head' , function() {
    		    echo '<script type="application/ld+json">{"foo":"bar"}</script>';
    		}, 10 );
    	}
    	return $content;
    }, 10, 2);

    But as I say, anything before wp_head won’t work, there’s a reference here that shows the order the core hooks firs in, if it’s from a plugin or a custom hook you’d need to check yourself when it runs:

    1. https://codex.www.ads-software.com/Plugin_API/Action_Reference

    Is there any reason you can’t do this:

    add_action( 'wp_head' , function() {
    	if ( $some_condition ) {
    		echo '<script type="application/ld+json">{"foo":"bar"}</script>';
    	}
    }, 10 );

    Perhaps you are relying on whatever is coming through in those $content and $args params?

    If you have to rely on that hook, and it is after wp_head, could you consider adding the script to the footer with wp_footer?

    Also, I’m sure you have already, but worth double checking your condition is in fact being met…

    Thread Starter Insomnia88

    (@insomnia88)

    Thanks for the answer.
    The action is used by a plugin but it seems you are right. Since I don’t know better and cant’t find the order I guess it’s just initialised too late.
    Now I use another work around where I insert the <script> directly in the body (that’s where the plugin action does its magic)

    • This reply was modified 4 years, 5 months ago by Insomnia88. Reason: typo

    Yes I don’t know of a GREAT way of determining the order unless are able to track through the plugin code to see when that code is run.

    There are some hacky ways of doing it, like if you have access to the error log for the server you can do stuff like:

    add_action( 'some_hook' , function() {
        error_log( 'some_hook' );
    });
    
    add_action( 'wp_head' , function() {
        error_log( 'wp_head' );
    });

    And see what order those show up in the logs…

    Adding it to the body like you say will work, but the footer is probably better, since I believe it’s generally recommended to add scripts to the footer where possible anyway so that they don’t delay the rendering of a page, and most of the time you want to wait until the document ready event fires before executing more code anyway.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘insert script (application(ld+json) from inside of action?’ is closed to new replies.