Tag Archives: coding

building

Improving WordPress Search Function: Plugin Fun

I recently decided to start sharing my reflections on my day-to-day as a postdoc in digital scholarship at Oxy. I welcome feedback on my work because at times I feel as if much of our work could go unnoticed in a world so dominated by conventional notions of research and learning. Anything alternative to traditional research (i.e. publishing) and teaching (within the curriculum) — or labeled as #altac — could get lost in the mix.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

One of the faculty projects associated with the CDLR (Center for Digital Learning + Research) is Lisa Wade’s Sociological Images, one of the most visible and influential public sociology sites in the blogosphere. To follow up on our last meeting with Lisa, I scoped out the use of a plugin as a possible solution to improve the search function on a WordPress site. Specifically I played with a WordPress search plugin called Relevansii because of its high ratings and robust documentation community.

On the backend, the admin can configure the setting of the search to enable a specific combination of AND/OR queries. For instance, it could run a AND query first and if no results return, it would run an OR search. It does a good job with phrase search with quotes (e.g. “hillbilly music”). And it has fuzzy matching (matching partial words if complete words don’t match).

With a bit of script tweaking, with the help from Relevansii’s documentation page, I got the plugin to work pretty well on my course blog for CSP 27 Race and Gender in Pop Music. I have it set so that it searches the post content, title, tags, and categories. I gave more weight to post titles than  tags and comments. Based on the configured algorithm, the search gives a “relevance score” which determines the order of the research results. I also got the result page to highlight the search terms in excerpts and indicate the number of hits.

To display the number of search results, I tweaked the one-line code that I found on the documentation page and inserted it into the line scripted to return the search term in the header section of the code in search.php. [The WP theme that I use for the blog is Twentyeleven.]

<?php printf( __( $wp_query->found_posts . ' Search Results for: %s', 'twentyeleven' ), '<span>' . get_search_query() . '</span>' ); ?>

One could further tweak the look of the search results; for instance, changing the length of the excerpts, font style of the search terms, etc.

What’s cool about this plugin is that one could do a category/tag filter in the search. This restricts the search to process only the documents labeled with a selected category/tag term [similar to the search on commercial sites like newegg.com or Amazon]. I tweaked the standard searchform.php using the example code snippet that I found on the documentation page. This is the bit of code that I ended using:

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
 <label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
 <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
 <?php wp_dropdown_categories(array('show_option_all' => 'All categories'));
     /*this is code snippet from relevansii for category filter */
  ?>
 <input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
</form>

On my blog, I have it set up so that the search results can be filtered by the existing post categories (namely, reading responses, assignment, etc). In the case of Sociological Images, it would be useful to restrict the search using the existing tags on the site. Alternatively, one could further refine the structure of the posts by assigning them with higher-level category terms.

Give it a shot and play around: http://cdlrsandbox.org/wordpress/racegenderpop/

 

 

building research

Back at it again: Tinkering with Maps

I played with Google Map API v.3 in OpenLayers today with the mission to fix a couple things broken on my Myspace Friendship Web Map. All of this is inspired by my recent revisit of my spatial (ethno)musicology at my UCLA talk on digital ethnography and the NCCSEM roundtable on alternative careers for ethnomusicology PhDs at Santa Clara University this past weekend. I also want to make some new visualizations for my talk at the upcoming EMP/IASMP meeting in New York in a couple of weeks.

More significantly, I’ve been preoccupied with how I can build on my music-map project. With the goal to develop tools to visualize the relationship between music and space, I want to go a step further than visualizing socio-musical communities to find a way to visualize the spatial patterns in the sound and lyrics of songs. This constellation of impulses and mini codling practices will contribute to my eventually goal of building a site, web hub for music scholars to rethink how the digital would enhance the spatial understanding of music and music-cultures.

I set aside a large chunk of time today to read and code today. I read Julie Meloni’s webcraft book to learn the syntax of JavaScript so that I could decipher the OpenLayers script that Joe and I worked on at the Scholars’ Lab. I read about DOM and reviewed some introduction to the basic structure, objects, and syntax of JavaScript. I also found out the some time early last year, Google came out with a Google Map API version 3. They are in the process of phasing out, or to use their language, “deprecating” version 2. So I set out to learn how to make the Google Map API version 3 work with the OpenLayers script that I came up with, while working with Joe (Gilbert) at the Scholars’ Lab.

I read the source code of the OpenLayers Google Map V.3 example and inserted parts of it into my original OL script. By the end of the day, I was able to get V3 to work in a new version [Kominasmap5.html]. In this version, Google Map layers download properly with the Spherical-Mercator projection. The loading time for the GM layers is much faster than better. The block-by-block layer download is replaced by a much faster download of the entire map. But the WMS layers (such as world_regions and muslim_majority) that the Scholars Lab created, however, are currently malfunctioning at this point. I took those parts of the script [var Layers] out to avoid brokenness.

I also learned how to center the map on specific lat-long coordinates. I decided to center the map with Singapore (103.8, 1.3667). I figured this way I can concentrate the friendship patterns in Asia, in particular South and Southeast Asia.

Here is the snippet of code that’s related to centering and projection:

new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 5);

I have no idea why “EPSG:4326″ is used in the OpenLayers Google Map v.3 example. This is especially puzzling when it explains in its comments that “Google.v3 uses EPSG:900913 as projection, so we have to // transform our coordinates.” In order to solve this puzzle, I may need to read about how projection works in OpenLayers.

In addition, the zoom is still broken (as it is in the previous version–kominasmap3–the public one hosted on beingwendyhsu.info) in the new version. The map can’t be zoomed all the out to the inter-continental level as before. I was not able to fix this in this iteration. A perhaps related problem is that the zoom bar on the left side of the map panel disappeared in this version.

I’m happy to be back at hacking, coding, and building again. It makes me feel incredibly productive. It feeds my inner child who loves to learn new things and imagine possibilities.

Goals for the next round:
– place a continent layer back into the map
– insert a zoom bar
– read parts of Julie M’s book on variables, functions, etc (ch 16 + 17)

Some OpenLayers zoom examples:
http://openlayers.org/dev/examples/fractional-zoom.html
http://openlayers.org/dev/examples/zoom.html
http://openlayers.org/dev/examples/fractional-zoom.html