Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created November 3, 2014 17:15
Show Gist options
  • Select an option

  • Save AbhishekGhosh/2ed7ff2e89d7f831b95a to your computer and use it in GitHub Desktop.

Select an option

Save AbhishekGhosh/2ed7ff2e89d7f831b95a to your computer and use it in GitHub Desktop.

Revisions

  1. AbhishekGhosh created this gist Nov 3, 2014.
    31 changes: 31 additions & 0 deletions Google-Sitelinks-JSON-LD.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    // modify and use within php opening and closing (closing may be not required depending on template)
    // add_sitelinks_search_box just a name
    // change pub-ABCDEFGH:1234567890 to real value
    // PHP_EOL is used to find the newline character in a cross-platform-compatible way
    // check your source code to make sure, some other plugin is not echoing a different "target"
    // target is Google
    // use site url instead of "url": "' . get_site_url() . '/",
    // it will decrease the burden of one PHP call
    // nginx php5-fpm compatible
    // Genesis compatible (use php opening and closing for using with Genesis Simple Hooks)
    // not for WP native search
    // php opens
    function add_sitelinks_search_box (){
    if ( is_front_page() ) {
    echo PHP_EOL . '<script type="application/ld+json">' . PHP_EOL;
    echo '{' . PHP_EOL;
    echo ' "@context": "http://schema.org",' . PHP_EOL;
    echo ' "@type": "WebSite",' . PHP_EOL;
    echo ' "url": "' . get_site_url() . '/",' . PHP_EOL;
    echo ' "potentialAction": {' . PHP_EOL;
    echo ' "@type": "SearchAction",' . PHP_EOL;
    echo ' "target": "https://www.google.com/cse?cx=partner-pub-ABCDEFGH:1234567890&ie=UTF-8&q={search_term_string}&sa=Search",' . PHP_EOL;
    echo ' "query-input": "search_term_string"' . PHP_EOL;
    echo ' }' . PHP_EOL;
    echo '}' . PHP_EOL;
    echo '</script>' . PHP_EOL;
    }
    }
    // return the output
    add_action( 'wp_footer', 'add_sitelinks_search_box', 10000 );
    // php closes