WordPress – Highlight search results

How to highlight search results?

Put the following code into your content loop:

$keys    = explode(" ", $s);
$content = get_the_content( '<span>'.__( 'Read more', 'mytextdomain' ).'</span>' );
$content = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-excerpt">\0</span>', $content);
echo $content;

Add to your style.css file:

.search-excerpt {
    background-color: yellow;
    color: black;
}

Resource:
http://www.wpbeginner.com/wp-tutorials/how-to-highlight-the-search-terms-in-results-in-wordpress/


Processing – How to detect Present Mode

How to detect if a sketch is running in the Present Mode or not?

// TRUE IF IN PRESENT MODE
boolean FULL_SCREEN = frame.isUndecorated();

if (FULL_SCREEN)
{  // WE ARE IN PRESENT MODE
   size(displayWidth, displayHeight, P2D);
   [... do stuff ...]
} else
{  // WE ARE IN NORMAL MODE
   size(800, 600, P2D);
   [... do other stuff ...]
}

 



Processing – How to find out if a sketch is in Java or Processing.js mode

A clever trick to find out if a sketch is currently running on a PC (Java mode) or in a browser (Processing.js)

// IS THIS INSTANCE RUNNING IN PROCESSING.JS OR PROCESSING JAVA?
boolean processingJS = true;
void setup()
{
   // DETERMINE IF THIS SKETCH IS RUNNING PROCESSING.JS OR PROCESSING JAVA MODE
   // PROCESSING.JS DOESN'T KNOW THE displayWidth VARIABLE AND WILL JUMP
   // TO THE EXCEPTION
   try {
       int dummy = displayWidth;
       processingJS = false;
   }
  catch (Exception e)
  {
  }
}

Processing – Center Point between two PVectors

How to calculate the center point between two (2D) PVectors?

PVector P1 = new PVector(150, 265);
PVector P2 = new PVector(60, 100);
float xcenter = abs(P1.x - P2.x) / 2 + min(P1.x, P2.x);
float ycenter = abs(P1.y - P2.y) / 2 + min(P1.y, P2.y);
PVector center = new PVector(xcenter, ycenter);

Unix & Perl – Code Snippets

Delete files older than 1 day:

$cmd = "find /tmp/*.jpg -type f -mtime +1 | xargs rm -f";   
$res = `${cmd}`;

Find files containing a specific phrase, in a directory tree:

find . -exec grep -i -l phrasetofind {} \;

WordPress – Directories and URLs

Home page of the web site:

$siteurl = site_url('/');
   => https://web20bp.com/kb/

URL of the current theme:

$template_url = get_bloginfo(‘template_url’);
   => CAGE Knowledge Base

Current CHILD theme directory:

$template_dir = get_template_directory();
   => /home1/webtwoze/public_html/kb/wp-content/themes/twentyeleven

Current CHILD theme URL:

$template_url = get_template_directory_uri();
   => https://web20bp.com/kb/wp-content/themes/twentyeleven

Physical path to the current directory:

$file = dirname(__FILE__);
   => /home1/webtwoze/public_html/kb/wp-content/plugins/rvg-all-tags

Plugins URL:

$url = plugins_url();
    => http://localhost/wp-content/plugin

Plugins directory:

$dir = plugin_dir_path(__FILE__);
    => C:\Rolf\wp-content\plugins\rvg-float-to-top-button/

Load a javascript file:

wp_enqueue_script('fttb-script',
plugin_dir_url( __FILE__ ).'/js/jquery.scrollUp.min.js',
array('jquery'), $this->fttb_version, true);


WordPress – Adding HTML to the header

Add HTML to the header of a WordPress page / post using a plugin

/**
 * Load jQuery + a theme specific javascript file
 */
function kb_load_js()
{
    // A trick to get the child theme directory...
    $child_theme_url = get_stylesheet_uri();
    $child_theme_url = str_replace('/style.css', '', $child_theme_url);
    $output = '<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>';
    $output .= '<script type="text/javascript" src="'.$child_theme_url.'/kb_js.js"></script>';
    echo $output;
}
add_action('wp_head','kb_load_js')

Used in:
Rocket Reader plugin

Resources:
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head


Processing Android – Wakelock (Keep Screen On)

Set the Android WAKELOCK (Phone won’t go into power save mode)
You’re sketch will need the ‘WAKE_LOCK‘ permission!

// For WAKELOCK
import android.os.Bundle; 
import android.view.WindowManager;

/*****************************************************************************************
 *
 *   SET WAKELOCK (PHONE WON'T GO INTO POWER SAVE MODE)
 * 
 *****************************************************************************************/
void onCreate(Bundle bundle)
{ 
  super.onCreate(bundle);
  getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} // onCreate()

Resources:
https://forum.processing.org/two/discussion/10486/how-do-i-get-the-android-view-for-my-sketch

Tested up to: Processing v3.1.2


Processing – Button Class

Button class for Java, Android and ProcessingJS modes:

Button btn;
btn = new Button(0, 0, btnWidth, btnHeight, color(255), color(255, 0, 0), "BUTTON");

/*****************************************************************************************
 * 
 *   BUTTON CLASS
 * 
 ****************************************************************************************/
class Button
{
  int x, y, w, h;
  color c;
  color cOver;
  String txt;
  int txtSize = 12;

  /****************************************************************************
   
   CONSTRUCTOR
   
   ****************************************************************************/
  Button (int _x, int _y, int _w, int _h, color _c, color _cover, String _txt)
  {
    x = _x;
    y = _y;
    w = _w;
    h = _h;
    c = _c;
    cOver = _cover;
    txt = _txt;
  }

  /****************************************************************************
   
   DISPLAY THE BUTTON
   
   ****************************************************************************/
  void display()
  {
    pushStyle();
    textAlign(CENTER);
    if (mouseOver())
      fill(cOver);
    else
      fill(c);
    stroke(100);
    strokeWeight(2);
    rect(x, y, w, h, 10);
    fill(0);
    textSize(txtSize);
    text(txt, x+w/2, y+h/2+txtSize/2);
    popStyle();
  }


  /****************************************************************************
   
   CHANGE THE TEXT ON THE BUTTON
   
   ****************************************************************************/
  void setText (String _txt)
  {
    txt = _txt;
    display();
  }

  /****************************************************************************
   
   IS THE MOUSE OVER THE BUTTON?
   
   ****************************************************************************/
  boolean mouseOver()
  {
    return (mouseX >= x && mouseX <= (x + w) && mouseY >= y && mouseY <= (y + h));
  }
} // Button