Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Last active January 25, 2020 00:20

Revisions

  1. itzikbenh revised this gist Aug 5, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions process-get-companies.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    <?php
    //NOTE - function.php expects it to be inside an api folder.

    //I'm returning the post title inside of a link. Do it however you wish and adjust accordingly
    //in the typeahead.js file
    function get_companies(WP_REST_Request $request)
  2. itzikbenh revised this gist Aug 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion process-get-companies.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    //I'm returning the post title inside of a link and also the thumbnail. Do it however you wish and adjust accordingly
    //I'm returning the post title inside of a link. Do it however you wish and adjust accordingly
    //in the typeahead.js file
    function get_companies(WP_REST_Request $request)
    {
  3. itzikbenh revised this gist Aug 5, 2016. 2 changed files with 3 additions and 6 deletions.
    3 changes: 1 addition & 2 deletions process-get-companies.php
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,8 @@ function get_companies(WP_REST_Request $request)

    foreach( $companies as $company )
    {
    $company_logo = get_the_post_thumbnail($company->ID);
    $permalink = get_post_permalink($company->ID);
    $name = "$company_logo<a class='company-link' href=$permalink>$company->post_title</a>";
    $name = "<a class='company-link' href=$permalink>$company->post_title</a>";
    array_push($companies_names, $name);
    }

    6 changes: 2 additions & 4 deletions typeahead.js
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,8 @@ $('.typeahead').typeahead({
    return item;
    },
    updater: function (item) {
    //This is how I dig out the href attribute. If you change the PHP code then it will probably break this
    //so make sure to adjust it.
    var url = $(item)[1];
    window.location.href = $(url).attr("href");
    //redirect user on click
    window.location.href = $(item).attr("href");
    },
    source: function (query, process) {
    return $.get(theme_data.site_url + 'wp-json/companies/v1/companies', { q: query }, function (data) {
  4. itzikbenh revised this gist Aug 4, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion process-get-companies.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    //in the typeahead.js file
    function get_companies(WP_REST_Request $request)
    {
    //return $request['q'];
    $companies = query_posts( array(
    "post_type" => "company",
    "s" => $request['q']
  5. itzikbenh renamed this gist Aug 3, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. itzikbenh revised this gist Aug 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@ function theme_js()
    //Download from here - https://github.com/bassjobsen/Bootstrap-3-Typeahead
    wp_enqueue_script( 'typehead__boostrap_js', get_template_directory_uri() . '/js/typeahead-bootstrap.js', array('jquery'), '', true );
    wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'jquery-ui-datepicker'), '', true );
    wp_localize_script( 'theme_js', 'theme_data', array('site_url' => network_site_url( '/' )) );

    }
    add_action( 'wp_enqueue_scripts', 'theme_js' );

  7. itzikbenh revised this gist Aug 3, 2016. No changes.
  8. itzikbenh created this gist Aug 3, 2016.
    28 changes: 28 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    function theme_styles()
    {
    wp_enqueue_style( 'boostrap_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' );
    wp_enqueue_style( 'theme_css', get_template_directory_uri() . '/css/theme.css' );
    }
    add_action( 'wp_enqueue_scripts', 'theme_styles' );

    function theme_js()
    {

    wp_enqueue_script( 'bootstrap_js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js', array('jquery'), '', true );
    //Download from here - https://github.com/bassjobsen/Bootstrap-3-Typeahead
    wp_enqueue_script( 'typehead__boostrap_js', get_template_directory_uri() . '/js/typeahead-bootstrap.js', array('jquery'), '', true );
    wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'jquery-ui-datepicker'), '', true );
    }
    add_action( 'wp_enqueue_scripts', 'theme_js' );

    include( get_template_directory() . '/api/process-get-companies.php' );

    //Endpoint for getting companies
    function water_endpoints()
    {
    register_rest_route('companies/v1', '/companies/', array(
    'methods' => 'GET',
    'callback' => 'get_companies'
    ));
    }
    add_action( 'rest_api_init', 'water_endpoints' );
    3 changes: 3 additions & 0 deletions post-type=page.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    //I will leave the rest for you.

    <input class="typeahead form-control" type="text" placeholder="Search..." data-provide="typeahead">
    29 changes: 29 additions & 0 deletions process-get-companies.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php
    //I'm returning the post title inside of a link and also the thumbnail. Do it however you wish and adjust accordingly
    //in the typeahead.js file
    function get_companies(WP_REST_Request $request)
    {
    //return $request['q'];
    $companies = query_posts( array(
    "post_type" => "company",
    "s" => $request['q']
    ) );

    $companies_names = [];

    foreach( $companies as $company )
    {
    $company_logo = get_the_post_thumbnail($company->ID);
    $permalink = get_post_permalink($company->ID);
    $name = "$company_logo<a class='company-link' href=$permalink>$company->post_title</a>";
    array_push($companies_names, $name);
    }

    return $companies_names;


    }



    ?>
    21 changes: 21 additions & 0 deletions typeahead.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    (function($) {

    $('.typeahead').typeahead({
    //So we can render HTML inside the list
    highlighter: function(item) {
    return item;
    },
    updater: function (item) {
    //This is how I dig out the href attribute. If you change the PHP code then it will probably break this
    //so make sure to adjust it.
    var url = $(item)[1];
    window.location.href = $(url).attr("href");
    },
    source: function (query, process) {
    return $.get(theme_data.site_url + 'wp-json/companies/v1/companies', { q: query }, function (data) {
    return process(data);
    });
    }
    });

    })(jQuery);