• Resolved ana220

    (@ana220)


    Hi, I am not a developer but I trying to add a schema to my new website without a plugin and I want to add this code I read in an article:

    <?php
    
    add_action( 'wp_head', function () { ?>
    
    $schema = get_post_meta(get_the_ID(), 'schema', true);
     
    if(!empty($schema)) {
     echo $schema;
    }
    
    <?php } );

    But, when I try to do this in the code snippet plugin it doesn’t work, it shows me the code in the frontend like text. Could you please let me know what is wrong? Thank you for your time.

    * when I add this code manually in the theme editor file it works that’s why I don’t know why it doesn’t with the plugin 🙁

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You need to make sure you are using return to send your content instead of echo when working with shortcodes:

    add_action( 'wp_head', function () {
    
    	$schema = get_post_meta( get_the_ID(), 'schema', true );
    
    	if ( ! empty( $schema ) ) {
    		return $schema;
    	}
    	
    	return '';
    } );
Viewing 1 replies (of 1 total)

The topic ‘Adding schema code to header’ is closed to new replies.